0

I have been attempting to set up a PowerShell script to run as part of a scheduled task on a Windows Server 2012 R2 server. The script will run if I execute it manually from the GUI but all but one of my attempts to run it from a task have failed.

After some troubleshooting I found that if I set the task to "Run only when the user is logged on" and I execute the task, it runs just fine. It pops a powershell window, runs the script, and closes itself. It seems like I am on the right track and I may have narrowed it down as to what is the problem but I am at a loss as to where to go from here. Obviously, I would like the script to execute regardless of whether or not I am logged in.

Just in case, here is the script I am running:

    Get-ChildItem C:\somefolder -Recurse| 
    Where-Object{$_.PsIsContainer}| Sort-Object CreationTime -desc| 
    Select-Object -Skip 3| Remove-Item -Force

Where can I look from here? I have been able to run powershell scripts in this manner from server 2008 without problems but I just cannot seem to crack this one.

EDIT: For the action of the task set to start a program and under Program/script I have "powershell" then ".\myscript.ps1" for an arguement then "c:\" for start in.

  • Is it really ".\myscript"? Have you tried providing the fully qualified path the the script? You might also want to pass the ExecutionPolicy policy option. – Zoredache Apr 23 '15 at 19:23
  • whoops nope, I mistyped that its ".\myscript.ps1" my bad. Will edit the original question to correct – Tyson Navarre Apr 23 '15 at 19:29
  • 1
    ExecutionPolicy is definitely your culprit – Hyppy Apr 23 '15 at 19:30
  • Even if I have already set the executionpolicy to Unrestriced? – Tyson Navarre Apr 23 '15 at 19:30
  • 1
    also, Zoredache, I have tried a ton of different ways to express the path including fully qualified but nothing seems to work. Googled the living heck out of this and that seems to fix it for a lot of people – Tyson Navarre Apr 23 '15 at 19:32
1

Can you try running with following:

powershell -noprofile -noexit -executionpolicy bypass -file C:\path\script.ps1

Also check that: get-executionpolicy is set to RemoteSigned

Also check "Run whether user is logged on or not.

  • Thanks! if I have set the executionpolicy to Unrestricted, will it still work? – Tyson Navarre Apr 23 '15 at 19:34
  • Try RemoteSigned first – Ankh2054 Apr 23 '15 at 19:37
  • Update, tried running the script in the way you suggested and the task just runs forever, but nothing happens still. Before I would run the task, nothing would happen but the task would show as completed even though it did not actually appear to run. Weird stuff. – Tyson Navarre Apr 23 '15 at 19:38
  • 1
    Can you try running powershell as admin and then setting the execution policy again Or run get-execution policy just to check it is set to RemoteSigned – Ankh2054 Apr 23 '15 at 19:45
  • 1
    Try and make a very simple powershell script that actually outputs to file and see if that works. – Ankh2054 Apr 24 '15 at 16:22
1

Finally figured this one out:

The problem turned out to be the fact that I was attempting to manipulate something on a network drive. What I learned is that, even if you already have the drive mapped in Windows, you MUST un-map and re-map the drive as part of your script in order to manipulate files non-interactively through a PowerShell Script.

Here is what I added to my PowerShell script to get it to work:

$secpasswd = ConvertTo-SecureString "YourPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)

Remove-PSDrive –Name “Z”

New-PSDrive –Name “Z” –PSProvider FileSystem –Root “\\192.168.X.X\backup” –Persist -Credential $mycreds

There is something with credentials being cached that happens when you are logged on but is not used when you are running the same script via task manager.

Thanks a ton for all the answers I got to this question, it really helped me to finally figure this one out hopefully this will help someone else out there too.

Final note, this same method worked for batch files as well on the same server.

0

Have you tried changing UAC-settings for Admins on DCs? (best to do this through GPO) There are 3 different options that have to be changed to get Powershel script working within Sheduled Task. Those 3 UAC policy-settings can be found under Computer Configuration >> Policies >> Windows Settings >> Security Settings >> Local Policies >> Security Options: - User Account Control: Admin Approval Mode for the Built-i Administrator account (Not Defined) - User Account Control: Run all administrators in Admin Approval Mode (Disabled) - User Account Control: Behavior of the elevation prompt for administrators in Admin Approval Mode (Elevate without prompting) Configure them so that UAC doesn't kick in when you scheduled task is executed with Administrator-privileged account

Of couse you hade to choose the option "Run whenever user is loged on or not" on the General options in your Scheduled Task config.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.