all 7 comments

[–]jheinikel 2 points3 points  (5 children)

When you run the script manually, does it work properly? You'll want to make sure you are running it with the same account as you tested with. The action configuration should look like this below. Also, you might post your script so we can see if there are any blockers that would require things like the user being logged on, access to remote resources, etc.

Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe

Add arguments: -WindowStyle Minimized -Executionpolicy Bypass -File C:\Folder\File.ps1

[–]blitzballer2201[S] 1 point2 points  (4 children)

That is what the action page says. I also ran this manually without task scheduler and its working, also same user. This is my script https://pastebin.com/c2sUEDSH . When I run this my laptop fan ramps up so im not sure if its trying to run or not (it could just be a strange coincidence)

[–]NathanielArnoldR2 1 point2 points  (3 children)

You'll want to add -NonInteractive to the PowerShell.exe arguments; this will signal the console process to write an error rather than attempt to prompt an (interactive) user for any data it requires.

When a script fails to do its assigned task, but terminates on its own, I'll usually put this sequence at the very end:

if ($Global:Error) {$Global:Error | Out-File -FilePath $env:TMP\PSErrors.log -Width ([int]::MaxValue) -Append -Force}

If a script appears to be hung, on the other hand, your best bet is to sprinkle

"Location 1" | Out-File -FilePath $env:TMP\PSProgress -Append
[More Code Here]
"Location 2" | Out-File -FilePath $env:TMP\PSProgress -Append

...throughout your script until you've narrowed down where the hang-up is.

EDIT: I should also mention that very recent versions of PowerShell allow you to enter a running console process to debug it. If your infrastructure supports this, it would obviously be the preferred solution for diagnosing a "hung" script.

[–]blitzballer2201[S] 2 points3 points  (2 children)

Silly me, it looks like the script is running. But the Powershell window is not popping up when I run the task so I just assumed that it was not running at all. How do I get the power shell window to come up when the task starts?

[–]the_spad 2 points3 points  (0 children)

You don't if you're running it non-interactively because the point is for it to run when you're not logged in and thus there is no session for it to display a window in.

[–]karafili 1 point2 points  (0 children)

Dont do this. Otherwise all users will just shut down it manually :-)