Azure / Azure/azure-powershell

`Set-AzVMRunCommand` / `Set-AzVmssVMRunCommand` with `-ScriptLocalPath` fails when shell script contains blank lines

Open
#29,432 1 comment 0 reactions 0 assignees View on GitHub
bug Compute Possible-Solution Service Attention Similar-Issue
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
2d 17h
Merged PRs (30d)
51

Description

## Description

When using `Set-AzVMRunCommand` or `Set-AzVmssVMRunCommand` with the `-ScriptLocalPath` parameter pointing to a local `.sh` file that contains blank lines, the command fails with:

```
ExecutionState : Failed
ExecutionMessage : Execution failed: failed to execute command: command terminated with exit status=2
Error : /var/lib/waagent/run-command-handler/download/RunShellScript/1/script.sh: line 1: syntax error
near unexpected token `;;'
```

## Steps to Reproduce

1. Create a shell script with blank lines between commands:

```bash
#!/bin/bash
echo "hello"

echo "world"
```

2. Execute via managed run command:

```powershell
Set-AzVMRunCommand -ResourceGroupName "myRG" -VMName "myVM" -RunCommandName "myRC" -Location "eastus" -ScriptLocalPath "./script.sh"
```

3. The run command fails with a `;;` syntax error on the VM.

## Root Cause

In [`Set-AzVMRunCommand_ScriptLocalPath.ps1`](https://github.com/Azure/azure-powershell/blob/main/src/Compute/Compute.Autorest/custom/Set-AzVMRunCommand_ScriptLocalPath.ps1) and [`Set-AzVmssVMRunCommand_ScriptLocalPath.ps1`](https://github.com/Azure/azure-powershell/blob/main/src/Compute/Compute.Autorest/custom/Set-AzVmssVMRunCommand_ScriptLocalPath.ps1), the script processing loop reads the local file line-by-line and joins them with `;` as a command separator. However, it appends `;` **unconditionally** after every line — including blank lines.

For blank lines:
- `$line.trim().split()` returns an empty array
- The inner `foreach ($word in $words)` loop never executes (no content added)
- But `$script += ";"` still runs at the end of the loop body

This causes consecutive blank lines to produce `;;` or `;;;` in the output, which is invalid shell syntax (`;;` is only valid as a `case` statement terminator).

**Example transformation:**
| Input | Buggy Output | Expected Output |
|-------|-------------|-----------------|
| `echo "hello"`

`echo "world"` | `echo hello ;; echo world ;` | `echo hello ; echo world ;` |

## Affected Cmdlets

- `Set-AzVMRunCommand` (VM version)
- `Set-AzVmssVMRunCommand` (VMSS VM version)

Both `.sh` and non-`.sh` code paths are affected.

## Environment

- Az.Compute module (all versions with managed run command support)
- Reproduced on RHEL 8.10 target VM
- Issue only occurs when using `-ScriptLocalPath` with a local file (not when using `-SourceScript` directly)

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.