I’m trying to get some information on this command I am trying to run.
Here is my script I’m attempting to run from an AzurePowerShell pipeline task…
param (
[Parameter(Mandatory = $true)]
[string[]] $vm_names = @("test-na01-vm004", "test-na01-vm005"),
[Parameter(Mandatory = $true)]
[string] $rg_name,
[Parameter(Mandatory = $true)]
[string] $env,
[Parameter(Mandatory = $false)]
[string] $client_secret,
[Parameter(Mandatory = $false)]
[string] $secret_name = 'my_secret'
)
try {
# UTILITY FUNCTION IGNORE, USED FOR LOCAL TESTING ONLY.
if (Test-Path "$($PSScriptRoot).env " -PathType Leaf) {
Get-Content "$($PSScriptRoot).env " | foreach {
$name, $value = $_.split('=')
if ([string]::IsNullOrWhiteSpace($name) -or $name.Contains('#')) {
continue
}
Set-Content env:$name $value
}
}
if ($env:client_secret) {
$client_secret = $env:client_secret
}
else {
# The secret name needs to be made dynamic based on what environment this is being run against (This is where the $env variable will come into play)
$client_secret = Get-AzKeyVaultSecret -VaultName "$($env)testna01kv001" -Name $secret_name -AsPlainText
}
foreach ($vm in $vm_names) {
Write-Host "Preparing remote execution on $($env)-$($vm), from resource group: $rg_name. Environment: $env"
$command = @{
ResourceGroupName = $rg_name
VMName = $($env)-$($vm)
CommandId = 'RunPowerShellScript'
ScriptPath = '.psregistration.ps1'
Parameter = @{
client_secret = $client_secret
}
}
Invoke-AzVMRunCommand @command
}
What I am trying to figure out is my "$vm_names" parameter at the top of the script.
Everything seems to be working fine other than the foreach
loop which is giving me the following error: "| Cannot process command because of one or more missing mandatory | parameters: vm_names."
In the foreach loop I have the VMName
variable set to $($env)-$(vm)
. The $($env)
is being passed in from the pipeline task via a runtime parameter i.e.
parameters:
- name: environment
displayName: Environment
default: dev
values:
- dr
- qa
- uat
- pd
variables:
- name: resource_group
${{ if eq(parameters.environment, 'dev') }}:
value: dv
- task: AzurePowerShell@5
displayName: "Execute Remote Registration"
inputs:
azureSubscription: "EnvConfig"
ScriptType: "FilePath"
ScriptPath: "$(System.DefaultWorkingDirectory)/ps/vm_run_command.ps1"
ScriptArguments: '-env "${{ parameters.environment }}" -rg_name "${{ variables.resource_group }}-test-na01-rg001"'
azurePowerShellVersion: LatestVersion
But the foreach loop is not picking up either of the vms from $vm_names
I want to be able to run this command on 2 specific VMs in any env (vm004 and vm005) but am having an issue with it not passing the parameter correctly.
I am still getting used to Stackoverflow so forgive me for any formating errors or missed information!
I’ve tried pretty much what is shown in my script other than formatting the command differently i.e.
Invoke-AzVMRunCommand -ResourceGroupName $rg_name -VMName $($env)-$($vm) -CommandId 'RunPowerShellScript' -ScriptPath '.psregistration.ps1' -Parameter @{client_secret = $client_secret
I can also run a specific VM in the command like -VMName dv-test-na01-vm004
and it will run correctly on that one machine.
2
Answers
Ended up getting the solution.
So from above, I used this PS parameter
But I removed the array from the parameter in the PS script and instead passed the variables through the pipeline like so
Now when I call the variables in my PS script they are being passed correctly and is running my Invoke command on each of the VMs
Firstly it appears you will be having collisions because of the use of your variable
$env
whilst also using$env:
providerI would change your environment parameter to not be abbreviated:
But it might work if you quote your VMName within your foreach loop. You also don’t need the
$()
subexpressionI would still recommend not using
$env
however to prevent issues with the provider already using$env