Timing Issues with Invoke-Command in PowerShell?

0
3
Asked By CuriousCoder85 On

I'm dealing with a PowerShell script that checks for a third-party application and attempts to uninstall it using the `Invoke-Command` cmdlet. Here's a snippet of my code:

```powershell
if( $endpointInfo.Is3rdPartyAppPresent ) {
try {
$endpointInfo.Is3rdPartyAppPresent = Invoke-Command -Session $session -ScriptBlock {
Start-Process -FilePath "$env:SystemRootSystem32cmd.exe" -ArgumentList "/c "$using:tempDir$using:appUninstallExe"" -Verb "RunAs" -Wait -PassThru
$__is3rdPartyAppPresent = if( Get-CimInstance -ClassName "Win32_Product" -Property "Name" -ErrorAction "Stop" | Where-Object { $_.Name -like "*$using:appName*" } ) { $true } else { $false }
return $__is3rdPartyAppPresent
}

if( $endpointInfo.Is3rdPartyAppPresent ) { throw "Unable to remove 3rd-party vendor application. Reason unknown" }
Write-Log -Message "succeeded" -Screen -NewLine -Result "Success"
} catch {
Write-Log -Message "failed {$( $_.Exception.Message )}" -Screen -NewLine -Result "Error"
} finally {
if( $Verbose ) { Write-Log -Message "Is3rdPartyAppPresent is $( $endpointInfo.Is3rdPartyAppPresent )" -Screen -File -NewLine -Result "Highlight" }
}
} else {
Write-Log -Message "skipped {$appName was not found}" -Screen -File -NewLine -Result "Skipped"
}
```

My question is, should the lines wrapped in `===><===` execute before the previous `Invoke-Command` has finished? I'm confused about the timing here.

3 Answers

Answered By PowerShellProX On

Depending on your `$session`, if it's indeed a new session, you might need to ensure that the script block has completed before moving on. Consider adding the `-AsJob` flag to your `Invoke-Command`, and use `Wait-Job` to pause the script until the background job finishes. This should handle your issue effectively.

CuriousCoder85 -

Exactly, my `$session` is created using `New-PSSession`, so I should add that logic in!

Answered By ScriptNinja42 On

I wouldn't jump to conclusions about the execution order without checking a few things first. The `Invoke-Command` usually waits for the command to finish before proceeding, but it depends on how you've set up your session. You might want to confirm that your session is set up correctly and that nothing unusual is happening there.

CuriousCoder85 -

That makes sense, I'm just a bit worried about the output coming through before the command is fully done.

Answered By TechGuru91 On

I think it's worth noting that when running the `win32_product`, it can potentially cause issues since it scans everything. Have you tried running the uninstall locally? If it works fine on your machine, that might help narrow down if it's the script or the execution environment that's causing the issue.

CuriousCoder85 -

Yeah, I'm aware of the issues with `win32_product`. And yes, running locally doesn't return anything after the uninstall, so that seems fine.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.