0

My goal is to fill the variable $output with the, you guessed it, output of the PSService.exe. Unfortunately this does not work. At all. As you can see in the code, I'm looking for a specific line, which is needed to tell if the command was a success or not.

Get-Content .\Computer.txt | ForEach-Object {
            $var = $_.Split(“+”)
}

foreach ($pc in $var) {
            .\PSService.exe \\$pc start WinRM
            $output = <command> 
            If ($output –like “START_PENDING”){
                        Write-Host “Success $pc”
            } else {
                        Write-Host “Error $pc”
            }
}

How can I declare a variable ($output) with the output within an IF -Statement?

janne
  • 79
  • 6
  • 4
    If you want the output from `.\PSService.exe` assigned to `$output`: `$output = .\PSService.exe ...` – Mathias R. Jessen Feb 04 '22 at 17:42
  • 3
    The assignment should be `$output = .\PSService.e....`, in addition, you might wanna look into [`$LastExitCode`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2#lastexitcode) – Santiago Squarzon Feb 04 '22 at 17:42
  • 4
    `$output = .\PSService.exe \\$pc start WinRM` to capture stdout output from the external program. Then change `-like "START_PENDING"` to `-like "*START_PENDING*"` – mklement0 Feb 04 '22 at 17:43
  • 1
    Does this answer your question? [How do I capture the output into a variable from an external process in PowerShell?](https://stackoverflow.com/questions/8097354/how-do-i-capture-the-output-into-a-variable-from-an-external-process-in-powershe) – zett42 Feb 04 '22 at 18:40

1 Answers1

0
$output = .\PSService.exe \\$pc start WinRM

This one does the job.

janne
  • 79
  • 6