I've got a very frustrating situation using the Azure CLI and attempting to replace the content of an Automation Account.
I am attempting to update it via a Azure DevOps pipeline AzureCLI#2 task. This is the script line i am calling
az automation runbook replace-content --debug --automation-account-name "${{ parameters.automationAccountName }}" --resource-group "${{ parameters.resourceGroup }}" --name "Schedule Summary" --content "`#temp.txt"
The issue i am having is the automation account runbook is updated, but the text is truncated. The contents of temp.txt is this -
Param(
[string]$resourceGroup ='',
[string]$UAMI ='',
[string]$serverInstance ='',
But the script that ends up in the runbook is simply
Param(
Its clearly breaking on CRLF but i can't figure out how to fix it. If i remove all CRLF then it appears as one line and the script then doesn't run.
I can tell where the problem is? Is the AzureCLI, powershell? or the devops task.
I've tried in my environment by adding Devops CLI extension in Azure bash and it worked for me successfully with the same parameters as yours.
I created a PS file in Az cloud itself and saved with the .ps1 extension as set the runbook type to PowerShell and updated the script as follows:
az automation runbook replace-content --content "#runbook.ps1" --automation-account-name "xxxxautomation" --name "xxxxrunbook" --resource-group "xxxxRG"
vi runbook.ps1:
Content replacement done in runbook:
If still the issue persists: In Azure DevOps, call a webhook with parameters and then start a runbook that imports the Azure DevOps runbooks.
But when you are dealing with Azure DevOps, I suggest you create or update runbooks via API instead of PowerShell modules which is efficient.
I run the script in windows hosted agent and reproduce your issue.Its clearly breaking on CRLF. Because windows can't identify the CRLF. You should run the script in Linux agent.
breaking on CRLF based on windows agent
alter to linux agent in pipeline
pool:
vmImage: ubuntu-22.04
Related
I tried different ways to deploy my local logic app from VS Community using powershell but none of them are working.
Any solutions for this?
Logic App deployment successful from Visual Studio (local) through PowerShell Command
The following command worked for me
New-AzResourceGroupDeployment -ResourceGroupName <Azure-resource-group-name> -TemplateUri https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.logic/logic-app-create/azuredeploy.json
and here is the output:
Here is the process that I followed before running the above command.
Process Flow:
I have found that there are 2 ways of deploying logic app through PowerShell i.e., Azure RM Template Commands and AZ Modules Commands
To work with any of the following commands, we are required to install related package files:
To deploy using Azure RM Template Commands - Run this code on PowerShell:
Install-Module -Name AzureRM -AllowClobber
To deploy using AZ module commands - Run this code on PowerShell:
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force
Checkout the following links to play with versions (uninstalling old and installing new), setting up the policies, connecting of Azure Accounts, etc in these:
Azure RM PowerShell Commands and Azure Az Module PowerShell Commands
Following is the process of which I have tried deploying the Local Visual Studio Logic App to Azure in PowerShell using Azure RM PowerShell Script commands:
Before deploying:
Create a Logic App in Visual Studio (File > New Project > Select 'Azure Resource Group' > Logic App)
Your solution will be ready in a few seconds, you will be able to see two json files. As you select prameters.json file you will be able to see json schema for logic app deployment.
To Connect your logic with your Azure Account, right click on LogicApp.json and select 'Open with logic app designer'. If you don't find this option, then install this extension in your project:
Right click on project and click on validate
Select your resource group, subscription and click on edit parameters - give custom name to your logic app. Otherwise, it will throw you error of null in logicAppName and shows template is invalid.
After validation, it shows the template is valid. It can be automatically validate during deployment process also, but it's good way to validate to get rid out of errors.
PowerShell Commands to deploy your logic App from visual studio (local):
Install this Power Shell provided by Microsoft Cloud.
Run in Administrator Mode > Login with Connect-AzureRmAccount command (it takes you to browser and connect with Azure account) > after that it imports your subscription and resource group details in the shell like below:
Use the following command to deploy your logic app
New-AzResourceGroupDeployment -ResourceGroupName "AzureAccountMail" -Templatefile "C:\LogicAppTemplats\template.json" -TemplateParameterFile "C:\LogicAppTemplats\parameters.json"
Check this following documentation for this command overview.
Done! Deployment Succeeded.
Now Check out in the azure portal:
Note:
Here you can find the template file and parameters.json file under your project:
Make Sure you given your logic app name value here in the parameters.json file:
And Provide the logic app name, deployment location values in the template.json file (LogicApp.json)
Could Azure CLI upload/update a script which was code by azure cli and powershell?
e.g
there's a demo.ps1:
$json = az group list;
sendemail --body $json --to "xxxx#google.com" --title "xxx";
I'd like to upload demo.ps1 & create a taskjob on azure like
az somecommand create task --time "every 3 hour" --file demo.ps1
upload taskjob like :
az somecommand update task --time "every 3 hour" --file demo.ps1
What I've tried & know :
I tried automation runbook but it can't support azure cli
I tried azure logic but it only support javascript
I can create a ubuntu VM and use crontab to do it, but it's too heavy for light job
It's meaningless to upload scripts scheduled, but it meaningful to run scripts scheduled.
As you know, Azure automation runbook don't support Azure CLI.
But power shell con do most jobs CLI do, you can try using power shell command in runbook, and link schedules to run the scripts.
I am trying to SysPrep a VM in Azure via Azure DevOps YAML pipelines. In the same pipeline (previous stage), I am deploying the VM via Azure Resource Manager (ARM) templates and copying scripts and files to the machine. I want to automate the whole process so RDP'ing into the machine is not an option. When I run SysPrep on the VM, I am getting the following error message:
##[error]The remote session query failed for <insertMyFQDN>.eastus.cloudapp.azure.com with the following error message: Access is denied.
From my repo I have a couple of files, including a PowerShell script that SysPreps the machine (sysPrepvm.ps1) - taken from Run SysPrep remotely through commands from Azure powershell. The script works when I am logged in on the machine and run in manually.
sysPrepvm.ps1
$sysPrepPath = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$arguments = '/generalize /oobe /shutdown /quiet'
Invoke-Command -ScriptBlock {param($sysPrepPath,$arguments) Start-Process -FilePath $sysPrepPath -ArgumentList $arguments} -ArgumentList $sysPrepPath,$arguments
I am using the built-in task in Azure DevOps, "Powershell on Target Machines", with which I am able to invoke other commands or scripts, so I am confident that the task works.
My Stage in the YAML Pipeline looks like this:
- stage:
displayName: SysPrep
variables:
azFQDN: $[stageDependencies.Connect.connect.outputs['SetEnvVars.azFQDN']]
jobs:
- job: SysPrepVhD
steps:
- task: PowerShellOnTargetMachines#3
inputs:
Machines: '$(azFQDN)' # FQDN on the machine
UserName: '$(adminUser)'
UserPassword: '$(adminPw)'
ScriptType: 'FilePath'
ScriptPath: 'C:\Windows\System32\Sysprep\fishtank\sysPrepvm.ps1'
Is there a limitation of running this remotely? I haven't been able to find a work-around yet, so any answer is greatly appreciated.
Edit
I have also tried running the script with -Verb RunAs, as an Inline script instead of File, as well as tried the accepted answer on the previous post:
Sysprep an Azure VM using PowerShell task in a pipeline
I managed to find a way using Custom Script Extension instead. I didn't think of doing this at first, since the ARM template I am using already has a Custom Script Extension on the machine to enable WinRM, and cannot have multiple Custom Script Extensions installed at the same time. Also, I didn't want to execute SysPrep as soon as the machine booted because it had to run other scripts and settings first. In my YAML pipeline, I managed to remove the existing one to redeploy a new Custom Script Extension with the script included.
I posted the entire solution on GitHub - https://github.com/Crytlig/azsysprep - for anyone in interested. It obviously needs a bit of cleaning but it works as is.
I am using Azure CLI azure-devops extension to manage my projects and git repositories in dev.azure.com.
az extension add --name azure-devops
To make code more readable and configurable I would like to use: (it does not matter if the values are between quotation marks or not)
az devops configure --defaults organization=https://dev.azure.com/myname
az devops configure --defaults project=f514a982-...
Those commands executed without error (powershell) but it seems they has no effect on the following commands.
For example
az repos list # lists an other project's repos, not the required f514a982
However executing
az repos list --project f514a982....
gives the expected result.
Missing I something or is this a bug?
Actually I figured it out, hopefully spares time someone in the future:
There is a --detect parameter which is on by default and if it is on then overrules the --defaults settings.
So if you run anywhere the command under a git repo folder tree and you do not explicitly use the --detect off, it will use that devops project (as default) which that git repo belongs to regardless of the previously executed az devops configure --defaults command
I get this error in windows CMD "stat: path too long for Windows" whenever I execute this command below in my azure cli in windows CMD.
az group deployment create -g "testacsengine" --template-file azuredeploy.json --parameters #azuredeploy.parameters.json
In windows, if you already install the Azure CLI model, you can execute the CLI command in CMD or Windows PowerShell ISE.
For the deployment of Azure template, you can just use the command az group deployment create -g "charlesTest" --template-file template.json in the directory of your template.
Or use the absolute path like this az group deployment create -g "charlesTest" --template-file D:\template.json. Of curse, you need to get authentication with the command az login first.
If you still get the error when you do this, the error must be inside your template. You should check your template correctly.
For more details about template deployment, see az group deployment create.