Skip to content

Commit e4558aa

Browse files
grizzlytheodoreTheodore Chang
andauthored
[Compute] fix linux issue of RunCommand cmdlets (#20613)
* join differently for Linux shell script header * fix linux issue by detecting comments and escaping them and then new line Co-authored-by: Theodore Chang <[email protected]>
1 parent 5f17f7f commit e4558aa

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/Compute/Compute.Autorest/custom/Set-AzVMRunCommand_ScriptLocalPath.ps1

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,50 @@ function Set-AzVMRunCommand_ScriptLocalPath {
180180
if ($PSBoundParameters.ContainsKey("ScriptLocalPath"))
181181
{
182182
# Read Local File and add
183-
$script = (Get-Content -Path $ScriptLocalPath) -join ";"
183+
$script = ""
184+
if ((Get-ChildItem $scriptLocalPath | Select-Object Extension).Extension -eq ".sh"){
185+
foreach ($line in Get-Content -Path $scriptLocalPath){
186+
$words = $line.trim().split()
187+
$commentFound = $false
188+
foreach ($word in $words){
189+
if ($word[0] -eq "#" -and $commentFound -eq $false){
190+
$commentFound = $true
191+
$script += "``" + $word + " "
192+
}
193+
else{
194+
$script += $word + " "
195+
}
196+
}
197+
$script = $script.trim()
198+
#close
199+
if ($commentFound){
200+
$script += "``"
201+
}
202+
$script += ";"
203+
}
204+
}
205+
else{
206+
foreach ($line in Get-Content -Path $scriptLocalPath){
207+
$words = $line.trim().split()
208+
$commentFound = $false
209+
foreach ($word in $words){
210+
if ($word[0] -eq "#" -and $commentFound -eq $false){
211+
$commentFound = $true
212+
$script += "<" + $word + " "
213+
}
214+
else{
215+
$script += $word + " "
216+
}
217+
}
218+
$script = $script.trim()
219+
#close
220+
if ($commentFound){
221+
$script += "#>"
222+
}
223+
$script += ";"
224+
}
225+
}
226+
184227
$PSBoundParameters.Add("SourceScript", $script)
185228
# If necessary, remove the -ParameterA parameter from the dictionary of bound parameters
186229
$null = $PSBoundParameters.Remove("ScriptLocalPath")

src/Compute/Compute.Autorest/custom/Set-AzVmssVMRunCommand_ScriptLocalPath.ps1

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,50 @@ function Set-AzVmssVMRunCommand_ScriptLocalPath {
185185
process {
186186
if ($PSBoundParameters.ContainsKey("ScriptLocalPath"))
187187
{
188-
# Read Local File and add
189-
$script = (Get-Content -Path $ScriptLocalPath) -join ";"
188+
# Read Local File and add
189+
$script = ""
190+
if ((Get-ChildItem $scriptLocalPath | Select-Object Extension).Extension -eq ".sh"){
191+
foreach ($line in Get-Content -Path $scriptLocalPath){
192+
$words = $line.trim().split()
193+
$commentFound = $false
194+
foreach ($word in $words){
195+
if ($word[0] -eq "#" -and $commentFound -eq $false){
196+
$commentFound = $true
197+
$script += "``" + $word + " "
198+
}
199+
else{
200+
$script += $word + " "
201+
}
202+
}
203+
$script = $script.trim()
204+
#close
205+
if ($commentFound){
206+
$script += "``"
207+
}
208+
$script += ";"
209+
}
210+
}
211+
else{
212+
foreach ($line in Get-Content -Path $scriptLocalPath){
213+
$words = $line.trim().split()
214+
$commentFound = $false
215+
foreach ($word in $words){
216+
if ($word[0] -eq "#" -and $commentFound -eq $false){
217+
$commentFound = $true
218+
$script += "<" + $word + " "
219+
}
220+
else{
221+
$script += $word + " "
222+
}
223+
}
224+
$script = $script.trim()
225+
#close
226+
if ($commentFound){
227+
$script += "#>"
228+
}
229+
$script += ";"
230+
}
231+
}
190232
$PSBoundParameters.Add("SourceScript", $script)
191233
# If necessary, remove the -ParameterA parameter from the dictionary of bound parameters
192234
$null = $PSBoundParameters.Remove("ScriptLocalPath")

0 commit comments

Comments
 (0)