Skip to content

Commit d1ac9af

Browse files
Update Invoke-AzVMRunCommand.md with more Windows Examples (#23693)
* Update Invoke-AzVMRunCommand.md with more Windows Examples Added more examples to educate Microsoft Learn readers on how to run cmdlets and script blocks using Invoke-AzVMRunCommand. This will help readers understand that the ScriptBlock parameter in PowerShell Invoke-Command cmdlet can also be worked around in Az PowerShell. * Update Invoke-AzVMRunCommand.md --------- Co-authored-by: Yunchi Wang <[email protected]>
1 parent 5536fd0 commit d1ac9af

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/Compute/Compute/help/Invoke-AzVMRunCommand.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,39 @@ Invoke a run command on the VM.
3939

4040
## EXAMPLES
4141

42-
### Example 1: Invoke a command on Windows
42+
### Example 1: Invoke a command on Windows - Using ScriptPath parameter when the script resides on the remote Windows VM
4343
```powershell
4444
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter @{param1 = "var1"; param2 = "var2"}
4545
```
4646

4747
Invoke a run command 'RunPowerShellScript' with overriding the script 'sample.ps1' on a Windows VM named 'vmname' in resource group 'rgname'. Var1 and var2 are defined as parameters in the sample.ps1. Parameter value can be string type only and script is responsible for converting them to other types if needed.
4848

49-
### Example 2: Invoke a command on Linux
49+
### Example 2: Invoke a command on Windows - Using ScriptString parameter to execute cmdlet on the Windows VM
50+
```powershell
51+
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptString "Set-TimeZone -Name 'Coordinated Universal Time' -PassThru"
52+
```
53+
54+
This command invokes a run command 'RunShellScript' that will execute the cmdlet Set-TimeZone with it's associated parameters. This example is useful when you want to execute short commands on Windows VM.
55+
56+
### Example 3: Invoke a command on Windows - Using ScriptString parameter to run script blocks on the Windows VM
57+
```powershell
58+
$ScriptBlock = {
59+
param(
60+
[string] $NewTimeZone,
61+
[string] $NewDate
62+
)
63+
Set-TimeZone -Id $NewTimeZone
64+
Set-Date -Date [DateTime]$NewDate
65+
}
66+
67+
$Script = [scriptblock]::create($ScriptBlock)
68+
69+
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptString $Script -Parameter @{'NewTimeZone' = "UTC"; 'NewDate' = "Dec-8"}
70+
```
71+
72+
This command invokes a run command 'RunShellScript' that executes a script block on a remote Windows VM named 'vmname'. The script block way allows you to execute multiple cmdlets with parameters in a single invoke and it also saves time on invoking multiple run commands for different cmdlets. Parameter value(s) can be of string type only.
73+
74+
### Example 4: Invoke a command on Linux
5075
<!-- Skip: Output cannot be splitted from code -->
5176

5277

0 commit comments

Comments
 (0)