In order to include a timeout, I am running the following job on Powershell:
> $job = Start-Job {$PSOut = net use * "<network path>"/persistent:no}
>> $job | Wait-Job -Timeout 30
>> if ($job.State -eq 'Running') {
>> # Job is still running, cancel it
>> $job.StopJob()
>> } else {
>> # Job completed normally, get the result
>> $myArray = $job | Receive-Job
I would like the variable $PSOut to carry the output in the console window: "Drive X: is now successfully mapped with "
Or: "System error 53 encountered..." in case of an error message.
However, $PSOut always returns an empty value.
What I've already tried: Including the following in the Else branch. That way, I obtain the output if the script executes successfully, but am yet to find a way to do this when the script fails.
$PSOut = (Get-job | Receive-job)