2

When a scheduled task runs I would like to open a Powershell window on my desktop to tail the log file and watch the progress. Specifically I would like the task to open the window on my desktop and run Get-Content C:\LogFile.txt -Wait. When the task has finished I would like it to close the powershell window.

Is there a way to start a graphical window under another user? From what I have found, most commands will run under the privilege of the specified user but not create a window on their desktop.

It would be even nicer if it would run under whichever user is logged on when the task starts and not a hard coded user.

EDIT: So far I have the code below. I works to an extent. It creates the tailing window but when I call $p.kill() it kills the psexec process but not the powershell process. Can I send a Ctrl-C to the powershell window?

# start tailing the log
$psexec = 'c:\psexec.exe'
$arguments = '-i powershell.exe -windowstyle maximized -command "& {get-content c:\logs\task_log.log -wait}"'
$p = start-process $psexec -argumentlist $arguments -passthru
# start the task
Some-task.exe
# kill the tail
$p.kill()
TwistedTech
  • 197
  • 2
  • 4
  • 15
  • 2
    You keep saying 'I WOULD like...', How far along in this script are you? Is launching a window under another user the only problem/question here? – Wutnaut May 01 '14 at 18:42
  • @Wutnaut Well I have the task that runs a powershell script and creates the log file. Currently I just open the log file manually. – TwistedTech May 01 '14 at 18:52
  • @Colyn1337 I haven't tried to many things since everything I can think of just runs under the privilege of the account. I don't know how to start a graphical window under another account which is why I asked. – TwistedTech May 01 '14 at 18:53

1 Answers1

0

First, you'll need PSExec.exe, it's free and provided by the sys internals group. You want a command similar to this:

psexec \\RemoteComputer "%systemroot%\system32\windowspowershell\v1.0\powershell.exe" -u username -p password -i -h

Read the PSExec help file and play around with the settings. You'll be most interested in the '-i' switch as it's what indicates the session to interact with. Just using the '-i' without a session id (like in the example), psexec just chooses one. If there isn't one available, it'll use session 0.

You may even find that the New-PSSession and Enter-PSSession Powershell cmdlets are better suited to what you need. On your computer, run the following in powershell:

$Session = New-PSSession -ComputerName computer.domain.com
Enter-PSSession -Session $Session

When you run those commands, your terminal on your machine becomes the terminal on the remote machine. Commands you enter in the remote session are executed on the remote machine and the output is fed back to yours. When your done enter the Exit-PSSession command to return to your own session.

Colyn1337
  • 1,228
  • 9
  • 24
  • So to get the window to show up on that user's desktop I need to specify the session id? Is there a way to list the existing sessions? – TwistedTech May 01 '14 at 19:06
  • 1
    @TwistedTech performing operations in someone else's session is a huge responsibility. There's a certain level of knowledge required to own that responsibility effectively. Take some time and research some more before you attempt something like this. – Colyn1337 May 01 '14 at 19:13
  • I know. The accounts are utility accounts for kiosks. I am not taking over another person's session. (Someone else added the `session-hijacking` tag. I don't really agree that is what I am trying to do but I left it anyway.) – TwistedTech May 01 '14 at 19:18
  • While Powershell remoting is awesome, I actually do want the window to appear. My goal is to be able to look at the screen of a kiosk and check where the task is at. The task can take up to an hour to run and I want to know what it is doing without having to open the log on each machine. – TwistedTech May 01 '14 at 21:18
  • 2
    @TwistedTech, why can't you just monitor the state of all the kiosks from one central computer? – dangph May 01 '14 at 22:23
  • @dangph They are, but in this setup the people in the field need to know what is going on. They don't have easy access to the central server. – TwistedTech May 02 '14 at 13:29