From d1ac9af4ce936656214fd10431aa182026786d02 Mon Sep 17 00:00:00 2001 From: Toluwaloope <71833008+Toluwaloope@users.noreply.github.com> Date: Tue, 30 Apr 2024 04:14:53 +0100 Subject: [PATCH] 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 <54880216+wyunchi-ms@users.noreply.github.com> --- .../Compute/help/Invoke-AzVMRunCommand.md | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Compute/Compute/help/Invoke-AzVMRunCommand.md b/src/Compute/Compute/help/Invoke-AzVMRunCommand.md index 5011a2842113..2f766a8c05ef 100644 --- a/src/Compute/Compute/help/Invoke-AzVMRunCommand.md +++ b/src/Compute/Compute/help/Invoke-AzVMRunCommand.md @@ -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