Description
Description
Set-AzVMRunCommand with -ScriptLocalPath parameter not working as expected against Windows Azure VM. As a follow-up to
Traces:
- -ScriptLocalPath parameter was introduced in Set-AzVMRunCommand, Set-AzVmssVMRunCommand need to support ScriptPath parameter like Invoke-AzVMRunCommand, Invoke-AzVmssVMRunCommand #19468
- -ScriptLocalPath parameter gets a bug report concerning Linux VM running .sh script Set-AzVMRunCommand, Set-AzVmssVMRunCommand with -ScriptLocalPath parameter not working as expected #19985
While the issue itself get solved, Set-AzVMRunCommand (possibly Set-AzVmssVMRunCommand as well) is now broken for multi-line command file because Set-AzVMRunCommand_ScriptLocalPath.ps1 now append ";" to every line of powershell. These semi-colon go to weird places and eventually the Powershell at remote Windows VM fail to parse it.
This issue occur when attempting to use Set-AzVMRunCommand with -ScriptLocalPath parameter, use a mult-line .ps1 file against Windows Azure VM. The Windows VM is running Microsoft.CPlat.Core.RunCommandHandlerWindows@2.0.5.
Workaround
Instead of using ScriptLocalPath parameter, use SourceScript parameter, with Get-Content "your path to script.ps1" | Out-String and everything will be fine.
Discussions
From https://learn.microsoft.com/en-us/powershell/module/az.compute/set-azvmruncommand?view=azps-9.4.0,
it says
Create or update Run Command on a VM passing the script content directly to -SourceScript parameter. Use ';' to delimit multiple commands.
There is no docs surrounding ScriptLocalPath yet. I would question the behavior of each line in the file found in ScriptLocalPath to be delimited by ';' everywhere even within the same line.
Issue script & Debug output
C:\Scripts\test.ps1:
param (
[string]
$MyVarible
)
Write-Output "Hello..."
Local Powershell:
Set-AzVMRunCommand -ResourceGroupName MyRg -VMName MyVm -RunCommandName MyCommand -Location southeastasia -ScriptLocalPath "C:\Scripts\test.ps1" -NoWait
// Success
$x = Get-AzVMRunCommand -ResourceGroupName MyRg -VMName MyVm -Expand InstanceView -RunCommandName MyCommand
// Success
$x.InstanceView
ExecutionState : Failed
ExecutionMessage :
ExitCode : 1
Output :
Error : ParserError: C:\Packages\Plugins\Microsoft.CPlat.Core.RunCommandHandlerWindows\2.0.5\Downloads\S
cript_mycommand_0.ps1:1
Line |
1 | ;param (;[string];$MyVarible;);Write-Output "Hello...";
| ~
| An expression was expected after '('.
StartTime : 07-Mar-23 12:51:09 AM
EndTime : 07-Mar-23 12:51:12 AM
Statuses :
// Pay attention to the extra semi-colons, they are added by Set-AzVMRunCommand_ScriptLocalPath.ps1 within this repository
Environment data
*Local machine*
Name Value
---- -----
PSVersion 7.2.9
PSEdition Core
GitCommitId 7.2.9
OS Microsoft Windows 10.0.22621
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Module versions
Az: 9.4.0
Get-Module Az*
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 2.11.2 Az.Accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault…}
Script 5.4.0 Az.Compute {Add-AzImageDataDisk, Add-AzVhd, Add-AzVMAdditionalUnattendContent, Add-AzVMDataDisk…}
Error output
No response