Facing issues with Invoke-AzVMRunCommand
Hi,
I have been trying to learn Azure Automation, and wanted to test running commands on Azure VMs using Invoke-AzVMRunCommand.
My goal is to get the status of multiple services that are saved in a list. Below is the code.
$VMName = "VMName"
$RGName = "ResourcegroupName"
$SubId = "XXXX-XXXX-XXXX-XXXX"
$serviceslist = ("netlogon", "dhcp")
#Create Script
echo "$serviceslist | foreach-object{ get-service $_}" >script.ps1
$out = Invoke-AzVMRunCommand -ResourceGroupName $RGName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptPath ".\script.ps1"
$out
The error:
PS C:\Users\username> $out
Value[0] :
Code : ComponentStatus/StdOut/succeeded
Level : Info
DisplayStatus : Provisioning succeeded
Message :
Value[1] :
Code : ComponentStatus/StdErr/succeeded
Level : Info
DisplayStatus : Provisioning succeeded
Message : netlogon : The term 'netlogon' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandWindows\1.1.11\Downloads\script32.ps1:1 char:1
+ netlogon dhcp | foreach-object{ get-service }
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (netlogon:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Status : Succeeded
Capacity : 0
Count : 0
Posted Mar 2, 2022, 12:00 PM UTC
The issue here is that the line where you're creating the "script.ps1" ends up creating the output
netlogon dhcp | foreach-object{ get-service }Which isn't valid syntax for what you're trying to do. Create your script such that it accepts parameters, e.g.
Param ([Array]$ServiceList)ForEach ($ServiceName in $serviceList) {Get-Service -Name $serviceName}Then use the -Parameter argument to pass your array e.g.
Invoke-AzVMRunCommand -ResourceGroupName $RGName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptPath ".\script.ps1" -Parameter @{"ServiceList" = $serviceList}Thanks a lot for this suggestion... I will try this tomorrow and report back
Hey, I have tried running the script as you suggested and get the below error.
Value[0] :Code : ComponentStatus/StdOut/succeededLevel : InfoDisplayStatus : Provisioning succeededMessage :Value[1] :Code : ComponentStatus/StdErr/succeededLevel : InfoDisplayStatus : Provisioning succeededMessage : get-service : The specified wildcard character pattern is not valid: System.Object[]At C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandWindows\1.1.11\Downloads\script55.ps1:5 char:1+ get-service $servicename+ ~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (:) [Get-Service], WildcardPatternException+ FullyQualifiedErrorId : RuntimeException,Microsoft.PowerShell.Commands.GetServiceCommandStatus : SucceededCapacity : 0Count : 0Seems to be an issue with passing an array as the parameter. I'm not sure if it's PowerShell, the Azure API, or the VM extension that's dropping the ball and I don't have time to troubleshoot at the moment, but when faced with a situation like this "quick and dirty" gets the job done. This is tested and works (unless your service name happens to have a semicolon in it, in which case pick a different character to join/split on)
script.ps1
Param ([string]$ServiceList)$services = $ServiceList -split ";"ForEach ($ServiceName in $services) {Get-Service -Name $serviceName}
Invoke:
Invoke-AzVMRunCommand -ResourceGroupName $RGName -VMName $vmname -CommandId 'RunPowerShellScript' -ScriptPath '.\script1.ps1' -Parameter @{"ServiceList" = $($serviceList -join ";")}Wow.... I wasted around 3-4 hours of my time yesterday trying multiple combinations to make it work. I never thought Microsoft could fuck up such a simple thing.
Your explanation makes sense, and I did make it work in another quick and dirty way.
$serviceslist = @("netlogon","dhcp")$list = $serviceslist[0]for($u = 1; $u -lt $serviceslist.length; $u++){$list = $list + ","+$serviceslist[$u]}
and passing the $list string type parameter as argument.
Hope Microsoft gets this fixed, as I assume this will be a headache for someone facing it for the first time
Thanks for all your help!!!