Executing PowerShell from Windows Task Scheduler - powershell

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

Related

Powershell: Invoke a session as Administrator

I am trying to run my script as a ADMIN user and install the certificates, but it only works if I run it from the command line as a Administrator. If I trigger my bat file from the other script it is installing the certificates as a local user.
SET PowerShellScriptPath='<FilePath>\File.ps1'
SET CertPath=%~dp0NewCSC.cer
start PowerShell.exe -ExecutionPolicy Bypass -Command "& {Set-ExecutionPolicy Bypass -Force -Scope LocalMachine}" 
timeout 05
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& %PowerShellScriptPath% %CertPath%"
Is it coz of I am using Powershell.exe to invoke for 2 times for every line its taking it as a local user? please help me out here.

Task Scheduler won't execute a powershell script

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?

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.

Run GPupdate in a hidden style

I created a .bat file calls a PowerShell script but a command prompt window continues to appear(it flashes). How can I do to make the command prompt hidden.
the powershell script update.ps1 contains GPupdate /force
Below is my code:
#echo off
PowerShell.exe -WindowStyle Hidden -File "update.ps1" -ExecutionPolicy Bypass -noProfile -NonInteractive
Create a scheduled tasks with another user (system for example)
the task will run in background

powershell script works in run.exe but doesn't work as scheduled task

I scheduled this task below and when I click run in Task Scheduler it doesn't throw any errors but the script doesn't execute:
Program:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& S:\Scripts\Download-USPTO-Forms.ps1 -destinationFolder 'S:\Scripts\USPTO Forms'"
When I open Run.exe and run this cmd then the Powershell window opens and the script executes:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& S:\Scripts\Download-USPTO-Forms.ps1 -destinationFolder 'S:\Scripts\USPTO Forms'"
Why is that? Any ideas? The task seems to be scheduled correctly.
Why are you using the call operator?
powershell -File "S:\Scripts\Download-USPTO-Forms.ps1" -destinationFolder "S:\Scripts\USPTO Forms"
Furthermore, ensure the task has properly mapped drives and permissions.