billrr02

How to gather PID for single user

8 posts in this topic

#1 ·  Posted

Let me just start off by saying what I'm trying to accomplish:

I am attempting to write a script that launches iexplore.exe, points to a UNC path, which in turn brings up a windows explorer window. The script then waits for that launched/spawned explorer.exe process to close, and then closes the launched/spawned iexplore.exe process.

The kicker here is that this script will run on a provisioned xenapp server (Windows Server 2008 R2) with multiple users on it simultaneously. I can't have the script closing other users spawned iexplore.exe or explorer.exe processes - only those that belong to the individual user.

I know that there is much more to this, but I started with the following as a base:

 

    Run("C:\Program Files (x86)\Internet Explorer\iexplore.exe \\UNC\PATH")
    ProcessWaitClose("explorer.exe")
    ProcessClose("iexplore.exe")

 

This works in the sense that the windows explorer window appears, but when I close the explorer.exe process, iexplore.exe does not close, and the script continues to run.

There must be a way to use the PID of the running processes to do this, correct? If this isn't at all possible, someone just let me know so I stop obsessing over it.

Forgive my naivety - I'm a sysadmin with very little AutoIT scripting experience.

Share this post


Link to post
Share on other sites



#2 ·  Posted

Normally you would use something like:

$iPID = Run("Notepad.exe")
Sleep(3000)
ProcessClose($iPID)

However Internet Explorer tends to spawn different PIDs so for iExplore I would use something like:

$hWnd = _RuniExplore("C:\Program Files (x86)\Internet Explorer\iexplore.exe")
WinClose($hWnd)
Func _RuniExplore($sRunCommand)
    Local $aWinList_Before = WinList("[CLASS:IEFrame]")
    Run($sRunCommand)
    Do
        $aWinList_After = WinList("[CLASS:IEFrame]")
    Until $aWinList_After[0][0] > $aWinList_Before[0][0]
    Return $aWinList_After[1][1]
EndFunc

This will only close the processes spawned by the script.

Share this post


Link to post
Share on other sites

#3 ·  Posted

35 minutes ago, Subz said:

$hWnd = _RuniExplore("C:\Program Files (x86)\Internet Explorer\iexplore.exe") WinClose($hWnd) Func _RuniExplore($sRunCommand)     Local $aWinList_Before = WinList("[CLASS:IEFrame]")     Run($sRunCommand)     Do         $aWinList_After = WinList("[CLASS:IEFrame]")     Until $aWinList_After[0][0] > $aWinList_Before[0][0]     Return $aWinList_After[1][1] EndFunc

That looks promising. It did properly spawn IE, then Windows Explorer, but when I closed explorer, the iexplore.exe process was still running.

Should the CLASS definition refer to Explorer, not Internet Explorer?

Share this post


Link to post
Share on other sites

#4 ·  Posted

Can I ask why you need to open the UNC Path with iExplore?  Why not use Explorer.exe?

$hWnd = _RunExplorer(@WindowsDir & '\Explorer.exe "\\UNC PATH\Share$"')
WinWaitClose($hWnd)
MsgBox(0,'', "Process Closed")
Func _RunExplorer($sRunCommand)
    Local $aWinList_Before = WinList("[CLASS:CabinetWClass]")
    Run($sRunCommand)
    Do
        $aWinList_After = WinList("[CLASS:CabinetWClass]")
    Until $aWinList_After[0][0] > $aWinList_Before[0][0]
    Return $aWinList_After[1][1]
EndFunc

 

Share this post


Link to post
Share on other sites

#5 ·  Posted

Because Citrix / Microsoft:

https://support.citrix.com/article/CTX139195
 

I've tried a lot of different solutions to fix this "Application not running" message in AppCenter, including the ones they list in multiple KBs. I found that using iexplore to point to a UNC path is the only way to keep the application running properly in Citrix AppCenter. Using a third-party explorer app or using a batch file to launch explorer and then pause (in which case the command prompt window remains open) are not acceptable solutions.

Near the bottom of the article, it states:

"The existing workaround does not work with explorer.exe sometimes. To solve this, use iexplore.exe instead explorer.exe to open UNC Paths or local paths, it has exactly the same behaviour."

I guess I'm in the "sometimes" group.

I could try taking your above code and compiling it to see if that would work too - I've just gotten so used to going down the iexplore.exe \\UNC\PATH path.

Thank you for your responses so far.

Share this post


Link to post
Share on other sites

#6 ·  Posted

Hmmm  Have tested this on Win 7, 10, Win 2008 R2, Win 2012 R2 and Win 2016 if I click Start > Run: C:\Program Files (x86)\Internet Explorer\iexplore.exe \\UNC PATH\Share$, Internet Explorer opens for a brief second then opens Windows Explorer and then closes Internet Explorer immediately, the Explorer Window stays open as expected.

Reading the article above it appears that it's suggesting putting a wrapper around iexplore and explorer.exe and use this to determine if the application is still running or not.  So in my case I would use the following to leave the UNC Path open in Windows Explorer:  If you want to get the Internet Explorer window handle you would need to change CLASS:CabinetWClass to CLASS: IEFrame.

$hWnd = _RunCommand(@ProgramFilesDir & '\Internet Explorer\iexplore.exe "\\Unc Path\Share$"')
WinWaitClose($hWnd)
Func _RunCommand($sRunCommand)
    Local $aWinList_Before = WinList("[CLASS:CabinetWClass]")
    Run($sRunCommand)
    Do
        $aWinList_After = WinList("[CLASS:CabinetWClass]")
    Until $aWinList_After[0][0] > $aWinList_Before[0][0]
    Return $aWinList_After[1][1]
EndFunc

 

Share this post


Link to post
Share on other sites

#7 ·  Posted

2 minutes ago, Subz said:

 

Hmmm  Have tested this on Win 7, 10, Win 2008 R2, Win 2012 R2 and Win 2016 if I click Start > Run: C:\Program Files (x86)\Internet Explorer\iexplore.exe \\UNC PATH\Share$, Internet Explorer opens for a brief second then opens Windows Explorer and then closes Internet Explorer immediately, the Explorer Window stays open as expected.

 

Yes, that's true. The problem is that when IE closes, the application is listed as "Application not running" in Citrix App Center - even though the Windows Explorer window is still open. This is more of a Citrix issue than anything.

I also want to point out that this is not a user-facing issue. The user is none-the-wiser. Explorer opens and they can access their files just fine. It's an issue on the back-end because I want to get accurate reporting of which applications are running, and when I see 20+ applications listed with "Application not running", it's enough to drive me crazy! :P

I actually used your above code (I removed the UNC path so that the "My Computer" window opens and commented out the MsgBox) and it looks very promising:

$hWnd = _RunExplorer(@WindowsDir & '\Explorer.exe')
WinWaitClose($hWnd)
;MsgBox(0,'', "Process Closed")
Func _RunExplorer($sRunCommand)
    Local $aWinList_Before = WinList("[CLASS:CabinetWClass]")
    Run($sRunCommand)
    Do
        $aWinList_After = WinList("[CLASS:CabinetWClass]")
    Until $aWinList_After[0][0] > $aWinList_Before[0][0]
    Return $aWinList_After[1][1]
EndFunc

I'm working on it now and I will return with a response.

Share this post


Link to post
Share on other sites

#8 ·  Posted

Subz, thank you very much for helping out on this. With a few modifications, I've got what I need:
 

#NoTrayIcon
$hWnd = _RunExplorer(@WindowsDir & '\Explorer.exe')
WinWaitClose($hWnd)
Func _RunExplorer($sRunCommand)
    Local $aWinList_Before = WinList("[CLASS:CabinetWClass|.*Adobe Reader.*|.*Excel.*|.*Notepad.*|.*PowerPoint.*|.*Windows Media Player.*|.*Windows Photo Viewer.*|.*Word.*]")
    Run($sRunCommand)
    Do
        $aWinList_After = WinList("[CLASS:CabinetWClass|.*Adobe Reader.*|.*Excel.*|.*Notepad.*|.*PowerPoint.*|.*Windows Media Player.*|.*Windows Photo Viewer.*|.*Word.*]")
    Until $aWinList_After[0][0] > $aWinList_Before[0][0]
    Return $aWinList_After[1][1]
 EndFunc

Adding those additional applications allowed the application to stay "running" in Citrix AppCenter even if they closed the Windows Explorer window but still had an open file opened. That plus adding the name of the exe to the HKLM\System\CurrentControlSet\Control\Citrix\wfshell\TWI\LogoffCheckSysModules registry string on the provisioned server, and we're golden!

Thank you again!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now