1

I have a Powershell script that runs in the background and part of the script will use IE that is hidden. Sometimes this script gets hung up and I'd like to view the IE and Powershell windows to see where its stuck. I can't find a way to do this.

Sometimes Windows will give me a message if a script is running that a process is running and asks if I would like to view it and then I am able to view the windows then but I can't figure out away to do that without getting that message first.

I need to figure out where this script is getting stuck at. If I run it manually it works fine and works fine most times but every so often it gets stuck in the same place.

It's a Windows 10 machine that it's running on.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jason Murray
  • 147
  • 2
  • 4
  • 13
  • Are you running the script in local machine or Remote Server? See if [this](https://stackoverflow.com/questions/49029864/start-application-in-foreground) is relevant. – Vivek Kumar Singh Feb 28 '18 at 15:37
  • The script is running in the local machine – Jason Murray Feb 28 '18 at 15:45
  • Looks like that is something used when you start the process. Problem is this process has already started without that parameter – Jason Murray Feb 28 '18 at 15:49
  • Are you running the script as a Scheduled Task i.e. from the Task Scheduler? And does your script have `$IE.Visible = $true` property set? This will make the IE window visible, if run from `PowerShell` console. – Vivek Kumar Singh Feb 28 '18 at 15:52
  • The script gets initiated from an outside controller using a username/password that I am logged on with. If i view taskmanager I see the processes running. In the script the $Ie.visible is set to false. I want to take that hidden window thats running and make it visible and the powershell window that hidden as well. – Jason Murray Feb 28 '18 at 15:59
  • Have you tried setting your IE object's Visible property to true? `$IE.Visible = $true` That should make it visible – HeedfulCrayon Feb 28 '18 at 16:06
  • As I mentioned the script has already run with that set to False, now I am trying to make that window visible. I found this page but cant seem to change the state http://community.idera.com/powershell/powertips/b/tips/posts/show-or-hide-windows – Jason Murray Feb 28 '18 at 16:21
  • @JasonMurray I see. Check out my answer, it should connect to the running instance for you – HeedfulCrayon Feb 28 '18 at 16:28
  • @heedfulCrayon to get that to work i need this command correct "new-object -com "InternetExplorer.Application"" If so that starts a new instance of IE and makes it visible, if there is a way to reference the running process can you let me know? – Jason Murray Feb 28 '18 at 16:31
  • @JasonMurray What all is in your `$windows` object when you try running the code below? Post it in a comment on my answer below. I was able to connect to a currently running instance of IE on my machine with those commands, but if it is running as a different user or with elevated privileges you need to run powershell as administrator – HeedfulCrayon Feb 28 '18 at 16:34

1 Answers1

2

Open a powershell window and type in these commands to connect to your running internet explorer:

$windows = (New-Object -Com "Shell.Application").Windows()
$ie = $windows | Where-Object {$_.Name -eq "Internet Explorer"}
$ie.Visible = $true

That should connect you to the running IE instance and make the window visible again. If that doesn't work, I would check out what objects are in the $windows variable to see if it is even detecting your IE. If it doesn't detect it, you might need to make sure it is running with Administrator rights.

After finding that the powershell script is starting the IE object in the remote session, I would instead implement a scheduled task and trigger that task remotely so that it will run in session 1 instead of session 0. More info on this issue with various other applications: http://psappdeploytoolkit.com/forums/topic/session-0-ui/

https://github.com/PowerShell/Win32-OpenSSH/issues/998

https://serverfault.com/questions/690852/use-powershell-to-start-a-gui-program-on-a-remote-machine

HeedfulCrayon
  • 837
  • 6
  • 20
  • This is the only thing that shows when I do $windows – Jason Murray Feb 28 '18 at 16:38
  • Sorry couldnt paste it in https://cisco.box.com/s/zlwuzj2290f7yyk0ti5sh8zpurdqficz – Jason Murray Feb 28 '18 at 16:39
  • @JasonMurray Have you tried running this both with admin rights and without? – HeedfulCrayon Feb 28 '18 at 16:42
  • Yeah i ran powershell as admin and tried to run and it only pulls back that one item – Jason Murray Feb 28 '18 at 16:43
  • @JasonMurray if you do `Get-Process iexplore` does it return two items or one? – HeedfulCrayon Feb 28 '18 at 16:48
  • Yes here are the process IDs https://cisco.box.com/s/cjxg5050ozfe87xccy7l6wwdg71jzloa – Jason Murray Feb 28 '18 at 16:52
  • I guess to answer your question it shows two processes. As I understand it the SI collumn with 0 means hidden. Id like to change it to 1 to show it for IE and powershell that is 0. – Jason Murray Feb 28 '18 at 17:00
  • @JasonMurray SI just stands for session ID, it doesn't have anything to do with whether it is visible or not. Still working on trying to figure out why your `Shell.Application` object can't see internet explorer – HeedfulCrayon Feb 28 '18 at 17:06
  • @JasonMurray What is running this script? Is it being run by a different machine? – HeedfulCrayon Feb 28 '18 at 17:06
  • Another "machine" kicks off the script but using the user creds on the workstation itself. So essentially its this user cholland that initiated the script. Im logged in with that user now so I should be able to change the visible status if I can get that process to show up with Shell.Application. It seems like that would work – Jason Murray Feb 28 '18 at 17:10
  • @JasonMurray I see what is going on SessionID 0 is the terminal service session or Remote shell session, and applications run in that session cannot be visible to the logged on user. Ways around this would be to set up a scheduled task on the local machine, and then use powershell to remotely call that scheduled task. – HeedfulCrayon Feb 28 '18 at 17:19
  • Ah ok so there is no hope to view those windows since they are running in the remote shell session then? Darn – Jason Murray Feb 28 '18 at 17:34
  • @JasonMurray I don't believe there is a simple way of doing it, but I did update my answer with a possible solution to your issue, you just have to change your approach a little bit. – HeedfulCrayon Feb 28 '18 at 17:35
  • Ok, well thanks for sticking in there with me. Appreciate the assistance. – Jason Murray Feb 28 '18 at 17:44
  • 1
    FYI I did a search on session 0 and found this program and it worked great https://www.coretechnologies.com/products/SwitchToSession0/ – Jason Murray Feb 28 '18 at 18:11