I have a simple script such that
Param(
[string] $test
)
Write-Output "Hello $test"
and when I run it from my pipeline, this is the command I am running:
az vm run-command invoke --command-id RunPowerShellScript --name my-vm-win -g myRG --scripts "C:\test.ps1" --parameters test=Peter
My output:
{
"code": "ComponentStatus/StdOut/succeeded",
"displayStatus": "Provisioning succeeded",
"level": "Info",
"message": "Hello \n",
"time": null
}
Clearly, I am not able to pass the parameter test and this is stopping me from going forward. I have tried the suggestion in this question as you can see but it is not working.
I have reproduced in my environment and I have got expected results as below and I followed Microsoft-Document:
In VM i have stored this ps1 file:
test.ps1 :
param(
[string]$arg1,
[string]$arg2
)
Write-Host This is a sample script with parameters $arg1 and $arg2
Now run the below command:
az vm run-command invoke --command-id RunPowerShellScript --name "vm-name" -g "resourcegrp-name" --scripts "C:\test.ps1 rithwik bojja"
Output:
Here in scripts parameter give the string you want to pass as an argument as I did and you will get output as I got.
[Here rithwik in command is argument 1 and bojja is the argument 2.]
Related
I am running an Azure CLI command in a Windows 10 Professional PowerShell script and I receive this error:
(Conflict) Run command OS type 'Windows' does not match the target OS Linux.
PowerShell version:
Major Minor Build Revision
----- ----- ----- --------
5 1 19041 1237
The failing PowerShell script:
$ResourceGroup= "Development"
$VmName = "ubuntu-test"
az vm run-command invoke `
--resource-group $ResourceGroup `
--name $VmName `
--command-id RunPowerShellScript `
--scripts "ufw disable"
Note: The ` character is the backtick. The one on the same key as the tilde ~
The same command without line continuation backticks works:
az vm run-command invoke --resource-group Development --name ubuntu-test --command-id RunShellScript --scripts "ufw disable"
If I do a Write-Host the output is a single line with the correct arguments minus the quotes around the --script command.
Write-Host az vm run-command invoke `
--resource-group $ResourceGroup `
--name $VmName `
--command-id RunPowerShellScript `
--scripts "ufw disable"
az vm run-command invoke --resource-group Development --name ubuntu-test --command-id RunPowerShellScript --scripts ufw disable
The documentation for the AZ CLI invoke command mentions nothing about setting the OS Type.
az VM run-command invoke
I think the use of line-continuations (` at the very end of lines) is incidental to your problem.
Apart from the use of variables vs. literals, the crucial difference between your multi-line command and your working single-line command is:
--command-id RunPowerShellScript vs. --command-id RunShellScript.
It looks like the VM you're targeting is a Linux machine, and that --command-id RunPowerShellScript isn't supported there, whereas --command-id RunShellScript is.
az vm run-command list ... can apparently be used to discover supported --command-id values.
I'm a total Powershell newb and still learning.
I am trying to get a list of Azure Webjobs that has been stopped in a webapp. I understand when i run the command az webapp webjob continuous list, id get a large set of array data.
I am having troubles with splitting it up, could someone advise how would i split it up as individual jobs and their properties?
i tried $webjobs = ($webname.Split('}')) and it wont split up the giant array.
This is my current code
$groups = get-AzResourceGroup | where{$_.ResourceGroupName -like "Dev"}
foreach($group in $groups){
Write-Host -ForegroundColor Cyan "processing resourceGroup" $group.ResourceGroupName
$webApps = Get-AzWebApp -ResourceGroupName $group.ResourceGroupName
foreach($webApp in $webApps){
write-host -ForegroundColor Yellow $webApp.Name
$webname = (az webapp webjob continuous list --name $webApp.Name --resource-group $group.ResourceGroupName --query '[].name' -o tsv)
$webstatus = (az webapp webjob continuous list --name $webApp.Name --resource-group $group.ResourceGroupName --query '[].status' -o tsv)
Write-host $webname
Write-host $webstatus
}
}
The result i get is...
processing resourceGroup dev
hub-dev-app-hub
hubv3-dev-app-hub/Data hubv3-dev-app-hub/ProcessToolData hubv3-dev-app-hub/ToolDataNotifications hubv3-dev-app-hub/UploadLasToSftp hubv3-dev-app-hub/ApplicationInsightsProfiler3 hubv3-dev-app-hub/DaaS
Running Running Running Running Running Stopped
hubv3-dev-app-authservice
hubv3-dev-app-authservice/ApplicationInsightsProfiler3
Running
hubv3-dev-app-api
hubv3-dev-app-api/ApplicationInsightsProfiler3
Running
What i am hoping to achieve is
hub-dev-app-hub
hubv3-dev-app-hub/Data Running
hubv3-dev-app-hub/ProcessToolData Running
hubv3-dev-app-hub/ToolDataNotifications Running
hubv3-dev-app-hub/UploadLasToSftp Running
hubv3-dev-app-hub/ApplicationInsightsProfiler3 Running
hubv3-dev-app-hub/DaaS Stopped
Any help would be greatly appreciated.
The result you get is expected for the code written. Taking into account tsv docs, means that $webname and $webstatus are arrays. When you write Write-Host $webstatus it will print all the data in the array, in this case:
Running
Running
Running
Running
Running
Stopped
What you could do is the following:
$webname = (az webapp webjob continuous list --name $webApp.Name --resource-group $group.ResourceGroupName --query '[].name' -o tsv)
$webstatus = (az webapp webjob continuous list --name $webApp.Name --resource-group $group.ResourceGroupName --query '[].status' -o tsv)
for ($i = 0; $i -lt $webname.length; $i++) {
Write-Host $webname[$i]
Write-Host $webstatus[$i]
}
But what you should do is use another query: [].[name, status]. Have a look at jmesPath.
This is better because you only do 1 request instead of two, and it should have the format that you would expect, i.e. array of name and status.
$webjobInfo = (az webapp webjob continuous list --name $webApp.Name --resource-group $group.ResourceGroupName --query '[].[name, status]' -o tsv)
Write-Host $webJobInfo
I am new to ARM templates and Azure CLI
This may seem a really stupid question, but I am using the tutorial here
It contains the following command
templateFile="my template file"
az deployment group create \
--name blanktemplate \
--resource-group myResourceGroup \
--template-file $templateFile
I am running Azure Cli via a command prompt
How can I run this? As there are multiple lines
Paul
Replace the backslashs ( \ ) with backticks ( ` ) at the end of each line and you should be able to run it. Your sample code with the backticks:
templateFile="my template file" `
az deployment group create `
--name blanktemplate `
--resource-group myResourceGroup `
--template-file $templateFile
for example, the following code will execute in the cloud shell if you just copy paste
Write-Host `
Hello, `
World!
There's another way. You can create a temporary PowerShell script file in cloud shell. Paste all the commands there as necessary and run the script file from the cloud shell.
I am trying to run a "local-exec" command on Terraform 0.13.5 that runs a script inside a CentOS VM. This script (we'll call it utility-script.sh) just creates a log file and installs dnf. At the end of the script there is a exit 0 line. Also tried killall -u ${username} but it still is stuck on "Still creating..". The az vm run-command command just hangs. This was working before but suddenly it just hangs now.
Expectation: To finish executing this command and move onto the next.
Actual result: Console shows "Still creating..." until timeout which is 90 mins on Azure.
resource "null_resource" "run-utility-script" {
# Script execution happens after download
depends_on = [
null_resource.script-download
]
# Create for each centos VM
count = length(var.centos-vm-ids)
triggers = {
current_instance_id = var.centos-vm-ids[count.index]
}
provisioner "local-exec" {
command = "az vm run-command invoke --command-id RunShellScript --name ${var.centos-vm-names[count.index]} -g ${var.resource_group} --scripts \"echo ${var.password} | sudo -S chmod +x ${local.utility_script} ; echo ${var.password} | sudo bash ${local.utility_script}\""
}
}
How can I fix this hanging issue?
Recommend you use the custom script extension.
I've been trying to run a script on an Azure VM that requires parameters passed to it like so;
az vm run-command invoke -g <resource group> -n <vm name> --command-id RunPowerShellScript --scripts "#....\Desktop\write-host.ps1" --parameters First Second
I have done this succesfully using the AzureRM modules in the following way;
Invoke-AzureRmVMRunCommand -ResourceGroupName <resource group> -VMName <vm name> -CommandId "RunPowerShellScript" -ScriptPath "....\Desktop\write-host.ps1" -Parameter #{ "first" = "First"; "second" = "Second; }
The write-host.ps1 script is very simple and is as follows;
param(
[string]
$first,
[string]
$second
)
Write-Host "$first and $second"
I cannot get the Azure CLI command to find the parameters. I've tried reading the documentation here, I've tried passing it in in a whole manner of different ways, some of which involve;
--parameters [first=]First [second=]Second
--parameters "[first=]First [second=]Second"
--parameters "`"First`" `"Second`""
--parameters #{"First" = "first"; "second" = "Second"}
The only time I can get it to semi work is when I pass in the variables like follows;
--parameters "`First`" `"Second`" `"Third`""
--parameters "First Second Third"
In which case it only prints out "Second and Third", it seems to ignore "First"
I want to execute these in a PowerShell script using AzureCLI commands but I've failed to execute it both in a Command window and in PowerShell.
Is any one able to tell me how to successfully pass in parameters, named or otherwise, into a PowerShell script using the AzureCLI run-command command?
in this case using the second suggested notation worked:
--parameters first=xxx second=yyy
although according to the docs both ways should be fine