V2 Engine Waits, V3 Engine Doesn't

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
that1geek
Posts: 12
Last visit: Mon Feb 12, 2024 3:06 pm

V2 Engine Waits, V3 Engine Doesn't

Post by that1geek »

Hello,

I have a script that uses Start-Process with the -Wait switch within a foreach loop. When I run this script in PowerShell V2 mode with PowerShell Studio, it 'waits' properly. When I run it in PowerShell V3 mode within PowerShell Studio, it doesn't wait.

However, when I run the very same script from a standalone PowerShell v3 console (outside of PowerShell Studio), it waits as expected.

I have Windows 7 Enterprise SP1 32-bit and PowerShell Studio 2012 3.1.23.

The code is similar to as follows:
PowerShell Code
Double-click the code block to select all.
foreach ($IP in $UniqueData) {
	$p++
	Write-Progress -Activity "Checking IPs" -Status "Percent completed: " -PercentComplete (($p / $UniqueData.Count) * 100)
	$error.clear()
	$DataLine = "" | Select-Object IP, Hostname, OS, Site, AccessPermittedPrimary, AccessPermittedSecondary, InAD, DNSServer1, DNSServer2, DNSServer3, DNSServer4, DNSServer5, DNSServer6, NeedsChange, Changed
	if (Test-Connection -ComputerName $IP -Count 1 -Quiet){ # Machine pings
		$RandomNumber = Get-Random -Minimum 0 -Maximum 50000
		$NmapFile = "C:\Temp\Output" + $RandomNumber + ".xml"
		Start-Process -FilePath C:\Users\Me\Desktop\nmap-6.40\nmap.exe -ArgumentList "$IP -Pn -O -n -oX $NmapFile" -WindowStyle 'Hidden' -Wait
		$var = parse-nmap $NmapFile
		$OS = $var.OS
		Remove-Item $NmapFile
}
I know it's not waiting for the process to exit because it's trying to remove the file that hasn't been created or saved by the process yet (generates errors).
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: V2 Engine Waits, V3 Engine Doesn't

Post by davidc »

Please refer to the following post:

http://www.sapien.com/forums/viewtopic.php?f=12&t=6856

There is a PowerShell bug involved here that we have no control over. The post includes a simple work around for the issue.

David
David
SAPIEN Technologies, Inc.
User avatar
that1geek
Posts: 12
Last visit: Mon Feb 12, 2024 3:06 pm

Re: V2 Engine Waits, V3 Engine Doesn't

Post by that1geek »

Thank you for your reply, David.

I came across that thread and I used that in a newly-created V3 script with success.

However, what I can't figure out is: Why would it wait correctly when run outside of PowerShell Studio in a V3 console? (Powershell.exe; Get-Host reports V3). That makes the issue seem to be with PowerShell Studio and not PowerShell itself.

I'd rather not have to modify other scripts just to be able to run or debug them in PowerShell Studio in V3. Running everything in V2 seems preferable to that.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: V2 Engine Waits, V3 Engine Doesn't

Post by davidc »

The host running PowerShell has no control over the cmdlet functionality. All this is handled by the PowerShell engine internally.

Just out of curiosity, is this a GUI script or a normal script? And are you running STA?


David
David
SAPIEN Technologies, Inc.
User avatar
that1geek
Posts: 12
Last visit: Mon Feb 12, 2024 3:06 pm

Re: V2 Engine Waits, V3 Engine Doesn't

Post by that1geek »

It's a normal .ps1 but I do have a System.Windows.Forms.OpenFileDialog before the foreach loop. STA is enabled.

Let me rephrase the issue. I run the same script in the built-in PowerShell V3 ISE and it waits with just the -Wait switch in the Start-Process cmdlet. I run the same script in PowerShell Studio V3 STA and it does not wait.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: V2 Engine Waits, V3 Engine Doesn't

Post by davidc »

Can you run it without the STA option and see if it makes a difference?

David
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: V2 Engine Waits, V3 Engine Doesn't

Post by davidc »

Are you running 32 bit?

David
David
SAPIEN Technologies, Inc.
User avatar
that1geek
Posts: 12
Last visit: Mon Feb 12, 2024 3:06 pm

Re: V2 Engine Waits, V3 Engine Doesn't

Post by that1geek »

Yes, Windows 7 Enterprise SP1 32-bit.

When I turn off STA, the script just hangs. You see the quick progress bar appear with it loading the ActiveDirectory module, but then nothing else happens. The file open dialog doesn't appear... nothing... no error.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: V2 Engine Waits, V3 Engine Doesn't

Post by davidc »

The open dialog requires STA to run, but I was just curious if it affected the -Wait parameter.

David
David
SAPIEN Technologies, Inc.
User avatar
that1geek
Posts: 12
Last visit: Mon Feb 12, 2024 3:06 pm

Re: V2 Engine Waits, V3 Engine Doesn't

Post by that1geek »

I commented out the section for the open file dialog and specified the path to the file in the script instead.

STA is still turned off, V3 on, and it still doesn't "-Wait" with PowerShell Studio.
This topic is 10 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.