0

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.

Fynenix
  • 11
  • 2
  • You cannot create a folder c:\Users unless you are an admin. Try a different folder location – jdweng Feb 17 '23 at 13:32
  • Thanks for the Answer. When 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. So I dont think its a permission issue. – Fynenix Feb 17 '23 at 14:56
  • Did you check the permissions on the folder? Can you manually add a file to the folder? – jdweng Feb 17 '23 at 15:11
  • Yes I can manually create a file in the folder. – Fynenix Feb 17 '23 at 15:27
  • Change `-Parameter @{"name" = $name}` to `-Parameter @{name = $name}`; do you get the same issue? – Anthony Norwood Feb 17 '23 at 21:36
  • @AnthonyNorwood Yes unfortanetly I have the same issue – Fynenix Feb 20 '23 at 07:41
  • Your script to run is just a ps1 file containing a file path; have you confirmed if you do `.\ScriptToRun.ps1` on your local system it works? It doesn't look to me like the script you're actually passing to Invoke-AzVMRunCommand has any parameters itself. Why can't you pass the actual script to Invoke-AzVMRunCommand, why are you wrapping it in another ps1 file? – Anthony Norwood Feb 20 '23 at 07:59
  • @AnthonyNorwood Yes when I run the Script on the VM without the parameters it creates the folder. I have to wrap it in another file because else it wont find the file: https://stackoverflow.com/questions/63796082/could-not-find-file-invoke-azurermvmruncommand – Fynenix Feb 20 '23 at 10:10
  • $ScriptToRun doesn't have a param block; it should be `$ScriptToRun = "C:\testps.ps1 -name $name"` and remove parameter from `Invoke-AzVmRunCommand` – Anthony Norwood Feb 20 '23 at 13:57

1 Answers1

0

Firstly, I have script on VM and i have followed Microsoft-Document:

param(
    [string]$name
)
MKdir -Path "C:\Users" -Name $name

Then i have used az invoke command in your script instead of Invoke-AzVMRunCommand and used below command:

az vm run-command invoke  --command-id RunPowerShellScript --name "name of the VM" -g "name of the resourcegrp" --scripts "C:\test.ps1  $name"     

Here $name is the param that you have send to runbook.

By this way you can create the folder with the name you want.

References taken from:

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • Thanks for you answer. The "az vm run-command" unfortanetly doesnt work in the Runbook. It is not recognized. As this is Powershell I think I need to use Invoke-AzVMRunCommand https://learn.microsoft.com/en-us/azure/virtual-machines/windows/run-command#powershell – Fynenix Feb 20 '23 at 10:05
  • Have you got any informtion from this https://stackoverflow.com/questions/74703049/how-to-pass-json-file-as-parameter-to-azure-powershell-runbook/74712739#74712739 reference – RithwikBojja Feb 20 '23 at 10:16
  • No I dont think this helps. I can get the parameter from Powerapps to the Runbook. But not from the Runbook to the PS Script on the VM. – Fynenix Feb 20 '23 at 10:30
  • Without them I doesnt work. And also the Command works when I try to excecute the Script on the VM without parameters. – Fynenix Feb 20 '23 at 10:45
  • Using Powershell I have tried too, I am getting Blank Parameters as you have got. – RithwikBojja Feb 27 '23 at 05:18