Microsoft.Compute/virtualMachines/runCommands parameters & protected parameters #10553
-
Hello, I'm trying to figure out how to use parameters & protected paramaters in the runCommands resource. The resource allows the use of the parameters & protectedParameters properties but how do I actually reference to them in the script?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Can I assume you are using Windows based on the PowerShell command + the directory path?
param vmName string = 'AEU1-PE-CTL-D1-vmJMP01'
param Location string = resourceGroup().location
resource virtualMachine 'Microsoft.Compute/virtualMachines@2022-11-01' existing = {
name: vmName
}
resource runCommands 'Microsoft.Compute/virtualMachines/runCommands@2022-11-01' = {
name: 'configure'
location: Location
parent: virtualMachine
properties: {
timeoutInSeconds: (60 * 90)
parameters: [
{
name: 'vmName'
value: vmName
}
]
source: {
script: loadTextContent('configure.ps1')
}
}
} contents of configure.ps1 (local script file in the same directory as my bicep file, although doesn't have to be.)param (
[string]$vmName
)
$PSBoundParameters
Write-Output $vmName The following also works. resource runCommands 'Microsoft.Compute/virtualMachines/runCommands@2022-11-01' = {
name: 'configure'
location: Location
parent: virtualMachine
properties: {
timeoutInSeconds: (60 * 90)
parameters: [
{
name: 'vmName'
value: vmName
}
]
source: {
script: '''
param (
[string]$vmName
)
$PSBoundParameters
Write-Output $vmName
'''
}
}
} Example of outputGet-AzVMRunCommand -ResourceGroupName AEU1-PE-CTL-RG-D1 -VMName AEU1-PE-CTL-D1-vmJMP01 -RunCommandName configure -Expand InstanceView | foreach InstanceView
ExecutionState : Succeeded
ExecutionMessage :
ExitCode : 0
Output : Key Value
--- -----
vmName AEU1-PE-CTL-D1-vmJMP01
AEU1-PE-CTL-D1-vmJMP01
Error :
StartTime : 4/28/2023 1:23:28 AM
EndTime : 4/28/2023 1:23:29 AM You could also use source: {
script: '''
$args
echo "vmName is [$($args[1])]"
'''
} Get-AzVMRunCommand -ResourceGroupName AEU1-PE-CTL-RG-D1 -VMName AEU1-PE-CTL-D1-vmJMP01 -RunCommandName configure -Expand InstanceView | foreach InstanceView
ExecutionState : Succeeded
ExecutionMessage :
ExitCode : 0
Output : -vmName
AEU1-PE-CTL-D1-vmJMP01
vmName is [AEU1-PE-CTL-D1-vmJMP01]
Error :
StartTime : 4/28/2023 1:40:08 AM
EndTime : 4/28/2023 1:40:08 AM This is documented over here: So just build your param block to accept those arguments for PowerShell. Linux uses ENV vars. I found it works without having a resource runCommands 'Microsoft.Compute/virtualMachines/runCommands@2022-11-01' = {
name: 'configure'
location: Location
parent: virtualMachine
properties: {
timeoutInSeconds: (60 * 90)
parameters: [
{
name: 'vmName'
value: vmName
}
]
source: {
script: '''
printenv vmName
'''
}
}
} Get-AzVMRunCommand -ResourceGroupName AEU1-PE-CTL-RG-D1 -VMName AEU1-PE-CTL-D1-vmUBU01 -RunCommandName configure -Expand InstanceView | foreach InstanceView
ExecutionState : Succeeded
ExecutionMessage : Execution completed
ExitCode : 0
Output : AEU1-PE-CTL-D1-vmUBU01 |
Beta Was this translation helpful? Give feedback.
Can I assume you are using Windows based on the PowerShell command + the directory path?