Task Scheduler won't execute a powershell script - powershell

I have a powershell script that runs on 1 machines but remotes out to other machines to collect system information. The script works fine when I run it manually, but it will not run as a scheduled task. I have it setup with the action "start a program" with the program/script set to run powershell.exe and the argument set to
-NoProfile -NoLogo -NoExit -NonInteractive -ExecutionPolicy Bypass -File filepath
I have tried the task with my admin credentials and by running as SYSTEM, same results with both sets of permissions. Any suggestions?

Related

Running a powershell script as administrator and minimized

So I have set up a task on task scheduler to run a .bat file that runs a powershell script as admin which sets the DNS settings. I figured out how to make the .bat file run minimised, but the powershell window still pops up. Here is the script for the .bat file called "SetDNS". The powershell script's name is "DNS.ps1".
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%DNS.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%C:\Users\Test\Downloads\DNS.ps1%""' -Verb RunAs}";
I want to change it so that the powershell script does not flash open while it runs. Is there something that I could add to the above code to make it run minimized? I tried to add "start /min" to the above code but it did not work. Help is appreciated.

batch-file pauses after execution of powershell command

What I am trying to do is to set -ExecutionPolicy (allow scripts) for the actual process (cmd.exe)
If a write this command in cmd.exe (as Administrator):
PowerShell.exe -ExecutionPolicy Unrestricted
Output is:
Windows PowerShell
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
Lernen Sie das neue plattformübergreifende PowerShell kennen – https://aka.ms/pscore6
Than I put on this command:
PowerShell.exe -Command "SomePowerShellScript.ps"
And all works.
If I try the same from a batch-file, it pauses after the first command.
The reason why I use the first command and not the Powershell specific with Set-ExecutionPolicy is described by Microsoft:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-5.1
PowerShell.exe -ExecutionPolicy Unrestricted
--> sets ExecutionPolicy to cmd and powershell (script can be started)
PowerShell.exe -Command Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
--> sets Policy only on actual powershell.exe process (script is not allowed to start)
And for clarifing I don't want to set execution policy to Unrestricted for LocalMachine/CurrentUser if possible.
For explaning this problem, the batch-file is only for "lazy" people to directly start the Powershell-Script without having to set ExecutionPolicy (So it works by double clicking).
Has anyone an idea why the batch-file pauses after the first command (I also tried it with call powershell.exe)?
Thanks for a response :).
The apparent pause is a result of PowerShell.exe -ExecutionPolicy Unrestricted actually entering an interactive PowerShell session, which you must manually exit (aside from that, the execution policy is set for that session (process) only).
If you place -ExecutionPolicy Unrestricted (or -ExecutionPolicy Bypass) before a -File argument, the execution policy is set - for the process only - before execution of the script file passed to -File is attempted, and execution should succeed (unless a group policy preempts overriding the persistently configured execution policy on the command line:
powershell.exe -ExecutionPolicy Unrestricted -File SomePowerShellScript.ps1
You can also use -Command, but note that this changes the interpretation of the arguments; notably, you must then use .\SomePowerShellScript.ps1 to refer to the script to execute.
See this answer for more information.

Executing PowerShell from Windows Task Scheduler

I am trying to execute a very simple PowerShell script from Windows Scheduled Tasks.
My PowerShell script is this:
$currentTime = (Get-Date).ToString('yyyy-MM-ddTHH.mm.ss')
$contentToDump = "JLS"
$path = "$home\Desktop\resutfile_" + $currentTime + ".txt"
Add-Content $path $contentToDump
So it simply writes the string "JLS" to a file on my desktop with the current time as part of the name.
If I execute it using Windows PowerShell ISE it works fine. It also works fine when executing using the command prompt.
But when I hook up at Scheduled task it doesn't work. The task returns with a valid ("0x0")-result but no file is generated.
My configuration of the task is this:
Action: Start a program
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command “&{c:\Users\jls\desktop\untitled2.ps1}”
Start in (optional): C:\users\jls\desktop
The task is set to run as "me" with highest privileges and it is running locally on my machine where I am admin.
What am I missing?
You need to replace the -Command with -File in your Add Arguments
Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File c:\Users\jls\desktop\untitled2.ps1

Running PowerShell script from batch file as SYSTEM

There are various posts I have seen showing how to run a PowerShell script from a batch file, however I am still not quite sure how to do this with admin rights or running under the system account. I have a batch file that needs to execute a PowerShell script and run the script as admin or the local system account WITHOUT prompting for credentials. This is running on end user machines that do not have the permissions. This is the best I have found for executing a powershell script from a batch file:
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
PAUSE
But UAC will prompt with the above. Any ideas?

Run Powershell script in windows Task Scheduler(Windows 7)

Hi I've got a Script and are trying to start it in Task Scheduler.
this poses Two problems for me.
is that the script will activate a Application that requires the GUI to be running and shown. so how can I make Task Scheduler to log in to windows automatically at a specific time. The computer will never/rarely be turned off or rebooted.
2.
I have yet to Successfully start a Powershell script from the Task Scheduler. I've tried Following:
powershell.exe -noexit -executionpolicy bypass -command &'Y:\Run Test\StartPowershellScript.ps1'
And
powershell.exe -noexit -ExecutionPolicy Bypass -File 'Y:\Run Test\StartPowershellScript.ps1'
And a variation of the above.
powershell.exe -noexit -ExecutionPolicy Bypass &'Y:\Run Test\StartPowershellScript.ps1'
and I've tried some variations with and without &"" &'' & and so on.
now I'm not really sure if it can be done. but I hope I can get some help here.
Edit 1.
I just tried the following.
powershell -noexit -ExecutionPolicy Bypass &'Y:\Run Test\StartPowershellScript.ps1'
after I did this in powershell.
set-executionpolicy -scope Process -executionPolicy Unrestricted -force
set-executionpolicy -scope CurrentUser -executionPolicy Unrestricted -force
set-executionpolicy -scope LocalMachine -executionPolicy Unrestricted -force
However How do I make sure that this will persist after the computer has been rebooted ?
Edit 2.
as it seems I can't make Scheduled task log in or unlock the computer, is there Anyway to set up a task to run at a specific time to log in to the computer so the scheduled task can run at this time ?
You can't log into the GUI with a scheduled task. Enable automatic logon if you need an interactive desktop.
Remove -NoExit from the argument list, otherwise PowerShell will keep running after the script completes. Also replace your single quotes with double quotes.
The execution policy is persistent unless it's superseded by a local or group policy (in which case Set-ExecutionPolicy should throw an error) or you're logged in with a temporary profile.
If that doesn't help you need to provide more details. Doesn't the task start at all? Do you get an error? What do the task history and eventlog say? What is the output of Get-ExecutionPolicy?