I'm trying to execute the Invoke-AzVMRunCommand in PowerShell and pass parameters to the script that needs to be executed.
Before I Run the Runbook I can define the parameter name: Define Parameter
Code from the Runbook:
param(
[Parameter(Mandatory=$true)]
[string]$name
)
Write-Output "Connecting to azure via Connect-AzAccount -Identity"
Connect-AzAccount -Identity
Write-Output "Successfully connected with Automation account's Managed Identity"
Write-Output "Run Script Against Machines"
$ScriptToRun = "C:\testps.ps1"
Out-File -InputObject $ScriptToRun -FilePath ScriptToRun.ps1
Invoke-AzVMRunCommand -ResourceGroupName "RG" `
-VMName "VM" `
-CommandId 'RunPowerShellScript' `
-ScriptPath ScriptToRun.ps1 `
-Parameter @{"name" = $name}
Remove-Item -Path ScriptToRun.ps1
Script on the server I want to execute with the parameters:
param(
[string]$name
)
MKdir -Path "C:\Users" -Name $name
The script does not produce the expected output: Creating a folder with name from the parametr under C:\Users. No folder is being created.
If I execute the Invoke-AzVMRunCommand on the same script with hardcoded arguments like so:
MKdir -Path "C:\Users" -Name "TEST" it does work and the folder gets created.