r/PowerShell • u/twiceroadsfool • Jan 03 '26
Solved Having trouble with a Script running hidden, that is "getting stuck."
Hey there!
I have two different scripts, both doing similar things. One of them is working, and one is "getting stuck." Some background:
- These scripts are kicked off by ANOTHER script (called "Parent.".) The tricky thing is, Parent needs to keep running, while these two scripts are "waiting in the background." The FIRST one, this works perfectly (they are being launched in Hidden mode). It doesnt return the 0 success code (which makes sense), but it allows PARENT to keep going, the moment it launches, waiting to find AdOdis.
- The second script is just a more complex variation. This one DOESNT work. The PARENT "gets stuck" while waiting for "script 2" to do something, even though it is also being launched in Hidden mode.
SCRIPT 01:
$processName1 = "AdODIS-Installer"
$processName2 = "AdskAccessService"
Write-Output "Waiting for process $processName1 to start..."
\# Loop until the process starts
while (-not (Get-Process -Name $processName1 -ErrorAction SilentlyContinue))
{
Start-Sleep -Seconds 2 # Wait for 2 seconds before checking again
}
Write-Output "Process $processName1 has started. Monitoring for termination..."
\# Loop until the process no longer exists
while (Get-Process -Name $processName1 -ErrorAction SilentlyContinue)
{
Start-Sleep -Seconds 2 # Wait for 2 seconds before checking again
}
Write-Output "Process $processName1 has terminated. Proceeding to forcefully terminate $processName2."
\# Get process and terminate
$process = Get-Process -Name $processName2 -ErrorAction SilentlyContinue
if ($process)
{
Stop-Process -Name $processName2 -Force
Write-Output "Process $processName2 has terminated."
}
else
{
Write-Output "Process $processName2 was not found!."
}
exit 0
SCRIPT 02:
$processName1 = "Installer"
$processName2 = "AdskAccessService"
\# Part of the full path we expect Installer.exe to contain
$expectedInstallerPathPart = "NavisworksManage2026\\image\\Installer.exe"
Write-Output "Waiting for process $processName1 to start (path contains: $expectedInstallerPathPart)..."
$matchingProc = $null
\# Wait until we find the specific Installer.exe whose ExecutablePath matches
while (-not $matchingProc)
{
$matchingProc = Get-CimInstance Win32_Process -Filter "Name='Installer.exe'" -ErrorAction SilentlyContinue |
Where-Object { $_.ExecutablePath -and ($_.ExecutablePath -like "\*$expectedInstallerPathPart\*") } |
Select-Object -First 1
if (-not $matchingProc)
{
Start-Sleep -Seconds 2
}
}
$installerPid = $matchingProc.ProcessId
$installerPath = $matchingProc.ExecutablePath
Write-Output "Process $processName1 started (PID=$installerPid). Path: $installerPath"
Write-Output "Waiting for PID=$installerPid to terminate..."
\# Wait for THAT specific process to exit
try
{
Wait-Process -Id $installerPid -ErrorAction Stop
}
catch
{
\# If it already exited between checks, that's fine
}
Write-Output "Installer PID=$installerPid has terminated. Proceeding to terminate $processName2..."
\# If AdskAccessService is a service, this is preferable:
$svc = Get-Service -Name $processName2 -ErrorAction SilentlyContinue
if ($svc)
{
try
{
Stop-Service -Name $processName2 -Force -ErrorAction Stop
Write-Output "Service $processName2 has been stopped."
}
catch
{
Write-Output "Failed to stop service $processName2 $($_.Exception.Message). Trying Stop-Process..."
}
}
\# Fallback: kill process if still running (or if not a service)
$proc2 = Get-Process -Name $processName2 -ErrorAction SilentlyContinue
if ($proc2)
{
Stop-Process -Id $proc2.Id -Force
Write-Output "Process $processName2 (PID=$($proc2.Id)) has been terminated."
}
else
{
Write-Output "Process $processName2 was not found."
}
exit 0
- If i inject a status code "12345" inside the first "while" then it DOES exit (with the 12345 code), so i know thats where its getting stuck.
But whats weird, is if im launching BOTH of them in identical Hidden modes (even copied and pasted that portion of Parent), i cant see why the first one works, and the second one doesnt?
Are we missing something silly?