azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Set-AzVMRunCommand with -ScriptLocalPath parameter not working as expected against Windows Azure VM

Open hoshinokanade opened this issue 2 years ago • 6 comments

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 https://github.com/Azure/azure-powershell/issues/19468
  • -ScriptLocalPath parameter gets a bug report concerning Linux VM running .sh script https://github.com/Azure/azure-powershell/issues/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 [email protected].

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

hoshinokanade avatar Mar 07 '23 01:03 hoshinokanade

Thank you for your feedback. This has been routed to the support team for assistance.

ghost avatar Mar 07 '23 05:03 ghost

@hoshinokanade Thanks for your comment. We will review the issue and get back to you shortly.

SwathiDhanwada-MSFT avatar Mar 07 '23 07:03 SwathiDhanwada-MSFT

This is really annoying. Another workaround is https://github.com/Skatterbrainz/aztools/blob/2008ed923ad7aa93f2dfa3e435829edc3129a71d/public/Invoke-AzToolsVmRunCommand.ps1#L64-L93 but that brings its own problems. You can not pass Parameters / ProtectedParameters anymore.

splitt3r avatar Mar 05 '24 15:03 splitt3r

Currently facing the same issue. Have not tried it yet with BlobContent.

Gijsreyn avatar Mar 06 '24 13:03 Gijsreyn

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Drewm3, @TravisCragg-MSFT, @nikhilpatel909, @sandeepraichura, @hilaryw29, @GabstaMSFT.