Skip to content

Commit

Permalink
Update Invoke-AzVMRunCommand.md with more Windows Examples (Azure#23693)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Toluwaloope and wyunchi-ms authored Apr 30, 2024
1 parent 5536fd0 commit d1ac9af
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/Compute/Compute/help/Invoke-AzVMRunCommand.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,39 @@ Invoke a run command on the VM.

## EXAMPLES

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

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.

### Example 2: Invoke a command on Linux
### Example 2: Invoke a command on Windows - Using ScriptString parameter to execute cmdlet on the Windows VM
```powershell
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptString "Set-TimeZone -Name 'Coordinated Universal Time' -PassThru"
```

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.

### Example 3: Invoke a command on Windows - Using ScriptString parameter to run script blocks on the Windows VM
```powershell
$ScriptBlock = {
param(
[string] $NewTimeZone,
[string] $NewDate
)
Set-TimeZone -Id $NewTimeZone
Set-Date -Date [DateTime]$NewDate
}
$Script = [scriptblock]::create($ScriptBlock)
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptString $Script -Parameter @{'NewTimeZone' = "UTC"; 'NewDate' = "Dec-8"}
```

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.

### Example 4: Invoke a command on Linux
<!-- Skip: Output cannot be splitted from code -->


Expand Down

0 comments on commit d1ac9af

Please sign in to comment.