Azure / Azure/azure-powershell

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

Open
#21,131 7 comments 2 reactions 2 assignees Claimed by @viveklingaiah View on GitHub
act-observability-squad Azure PS Team bug Compute - VM customer-reported Service Attention
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
2d 17h
Merged PRs (30d)
51

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 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 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:
```PowerShell
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

```PowerShell
*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

```PowerShell
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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.