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()

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