Trying to catch the exitcode from PowerShell Invoke-command with a BAT file

I'm trying to catch the exitcode from a PowerShell script that uses a Invoke-Command to run a scriptblock on a remote machine.

First the BAT file: The BAT file is run with a variable. The script looks like this:

powershell.exe -noninteractive -noprofile -command "& {E:\Scripts\Check-Services_XXX.ps1 %1 }"
EXIT /B %errorlevel%

The PowerShell script looks like this:

param(
[string] $ip #IP address van server
)


$username = "DOMAIN\DOMAIN_USER"
$secpasswdfile = "E:\Location\DOMAINUSER_encrypted_password.txt"
$secpasswd = Get-Content $secpasswdfile | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
$soptions = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck

Invoke-Command -ComputerName $ip -UseSSL -SessionOption $soptions -Credential $credentials -ScriptBlock `
{
# Start services
Start-Service -InputObject (Get-Service -Name IAS)

# Check services status

$checkservice = (get-service -Name IAS -ErrorAction SilentlyContinue)
if($checkservice.status -ne "Running"){$host.SetShouldExit(1)}

exit
}

The problem is that the ExitCode is not captured back, so when the BAT file ends, it ends with 0. That would be the case if everything is running. But i deliberately changed the service name in the check service section to something that does not exist for sure, but still it the BAT file ends with Exitcode 0

Done so far: Tried this solution: catching return code of a command with "invoke-command" - Powershell 2 But didn't work: got the following error "is not equal to Open, you cannot run a command in the session. The session state is Closing" Apparently, when it exited with a error, the session was closed, thus couldn't get the exitcode

Also tried this one: Capture Write-Host output and exit code from Invoke-Command on a Remote System But also the same result; no correct exitcode (expected 1 instead of 0 in the BAT file)

SOLUTION!

Thanks to @js2010 and @mklement0 ; it works now like a charm!

This is the BAT file:

powershell.exe -noprofile -File "E:\Scripts\Check-Services_XXX.ps1" "%1" "%2"
EXIT /B %errorlevel%

And here is the PowerShell code that eventually worked out for me:

param(
[string] $ip,       #IP address of checked server
[string] $service ) #Service name

$username = "DOMAIN\USER"
$secpasswdfile = "E:\Scripts\Credentials\DOMAIN-USER_encrypted_password.txt"
$secpasswd = Get-Content $secpasswdfile | ConvertTo-SecureString
$credentials = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)
$soptions = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck
$session = New-PSSession -ComputerName $ip -UseSSL -SessionOption $soptions -Credential $credentials

# Start services
Invoke-Command -Session $session -ScriptBlock { Start-Service -Name $using:service }

# Check services status
$checkservice = Invoke-Command -Session $session { Get-Service -name $using:service | where status -eq running }
if (! $checkservice) { 
  write-output ("Error 1, Service '" + $service + "' not running or not found.")
  exit 1
}

I had some issues with passing variables to remote commands, this link helped me out (https://powershellexplained.com/2016-08-28-PowerShell-variables-to-remote-commands/)


You would have to run the exit command outside of invoke-command.

# check-service.ps1
$result = invoke-command localhost { get-service appxsvc | 
  where status -eq running }
if (! $result) { 
  exit 1
}