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 0SCRIPT 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 03. 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?
8
Upvotes
5
u/BlackV Jan 03 '26
p.s. formatting, you've use a quote block
it'll format it properly OR
Inline code block using backticks
`Single code line`inside normal textSee here for more detail
Thanks
3
u/BlackV Jan 03 '26 edited Jan 03 '26
I dont see your code that's launching the processes
there seems like there is a lot of extra work going on here that is unneeded
but dont launch script 2 hidden, see what is happening
write detailed logging to a file see what is happening
looking at it script 2 is dependent on script 1, what is stopping the parent script from launching those 2 instantly ?
1
u/twiceroadsfool Jan 04 '26
I wanted to come back to say thanks to everyone, and provide an update.
TL:DR version: Im a DOOFUS.
Basically, here is the gist:
What i forgot?
Buried deep inside PARENT, where we told it to allow PS1's to run in Hidden mode, we told it SPECIFICALLY to look for the FILE NAME of Script 01 (the ODIS one). Because we hadnt yet progressed to the point of needing to do it for all of the INSTALLERS as well.
Whoops.
But, i took all of the suggestions (and some from GPT as well, begrudgingly), and we optimized the scripts. AND yes: Script 01 and Script 02 both work the same way now. They were written at different times, since Installer.exe wasnt an issue we knew about until this year. And Installer presented more challenges than ODIS, which is why the direction was different.
But hey... its working now!
1
u/purplemonkeymad Jan 03 '26
Not really sure what you are doing, but it sounds like the best thing is to find out where it gets stuck. So either don't run the scripts "hidden" so you can look at the output and see where it has got to, or write your output to a file so you can read the status from the file.
1
u/I_see_farts Jan 03 '26 edited Jan 04 '26
While looking this over and playing with what I could on my end. Why not use
Get-Processin the second loop? I think you're over filtering it.I threw some Verbose messages into the loop and set my
$verbosePreference = "Continue"and since-liketakesRegExWildcards just let the filtering rely on that.Thoughts?
Edit: If you attempt this, you'll have to change your
$installerPid = $matchingProc.ProcessIdto$installerPid = $matchingProc.IdEdit2:
-likeDOES NOT take RegEx. My mistake, you'll need-matchfor that.