Issue
I have a bash script check_disk_space.sh
to check and error exit if disk space for a given mount on a Linux Centos 7 host is less than the threshold(30GB for example).
check_disk_space.sh has 3 input parameters
${{ inputs.mount_points }}
which could be a comma-separated mount point like/tmp,/etc
${{ inputs.min_free_space }}
which is the threshold below which free disk space should error and exit."$eachhost"
which is each host obtained from the comma-separated listhosts
where the script(check_disk_space.sh
) runs and checks for free space.
This is running fine using Github Linux runners as below.
steps:
- name: Check Disk Space on Remote Hosts
shell: bash
run: |
cd "${{ github.workspace }}/DevOps/scripts"
IFS=',' read -ra remhosts <<< "${{ inputs.remote_hosts }}"
for eachhost in "${remhosts[@]}"; do
ssh -q -t user@"$eachhost" "bash -s" -- < ./check_disk_space.sh "${{ inputs.mount_points }}" "${{ inputs.min_free_space }}" "$eachhost"
done
This works fine however, I now wish to use a Windows runner due to policy constraints.
I tried the below by hard coding the values, but I get an error in doing so.
name: Attempt2 Check Disk Space on Remote Hosts
on:
push:
branches:
- main
jobs:
check_disk_space:
runs-on: windows-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Check Disk Space on Remote Hosts
shell: powershell
run: |
$remoteHosts = "80.240.24.193,80.240.24.194".Split(",")
$mountPoints = "/tmp"
$minFreeSpace = "5"
$user = "root"
cd $env:GITHUB_WORKSPACE
$scriptcode = Get-Content -Raw -Path ./check_disk_space.sh
foreach ($eachhost in $remoteHosts) {
$sshCommand = "ssh -q -t $user@$eachhost `"bash -s`" --"
$command = @"
$scriptcode
./check_disk_space.sh "$mountPoints" "$minFreeSpace" "$eachhost"
"@
$finalCommand = ($sshCommand + " `"$(echo $command)`"").Trim()
Write-Output $finalCommand
Invoke-Expression $finalCommand
}
Error:
Run $remoteHosts = "80.240.24.193,80.240.24.194".Split(",")
At D:\a\_temp\06b226b4-60d8-40ca-82d2-f8f76669f636.ps1:17 char:2
+ "@
+ ~~
White space is not allowed before the string terminator.
At D:\a\_temp\06b226b4-60d8-40ca-82d2-f8f76669f636.ps1:11 char:37
+ foreach ($eachhost in $remoteHosts) {
+ ~
Missing closing '}' in statement block or type definition.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : WhitespaceBeforeHereStringFooter
Thus, my attempt to run check_disk_space.sh
using PowerShell on a remote linux host fails for which I got no clue why.
Sample run with error here: https://github.com/knowyrtech/invokebashfrompowershell/actions/runs/5894294098/job/15987641430
I tried a different approach which was resolved by the accepted answer but unable to use it to execute check_disk_space.sh
script of remote Linux there too: Unable to use powershell's sshArgs to invoke a simple ssh command on remote linux host
Can you please suggest?
Solution
You are using the $host
variable in PowerShell and you are probably not aware of that it is an automatic variable in PowerShell. Replace it by whatever you really want.
Concerning your error message: You are passing two strings to Invoke-Expression
, but this cmdlet expects only one string. Either adjust your quoting or use another approach like direct invocation or Start-Process
.
Answered By - stackprotector Answer Checked By - Senaida (WPSolving Volunteer)