Schedule Task with schtasks - powershell

I created a script that creates a scheduled task GPUpdate /Force to be executed from a Windows server machine to a Windows 10 machine "Beta" but the script is not executed and an error shows up:
starttime is not valid
Below you will find my script:
schtasks /Create /S Client.Admin.6NLG-AD /U Admin.6NLG-AD\Beta /P ******** /SC MINUTE /MO 1 /TN Update /TR "GPUpdate /Force" /ST defaults

Related

How to Start a Script Using Start-Process Every 30 Minutes With Hidden Window [duplicate]

This question already has answers here:
How to run a PowerShell script without displaying a window?
(23 answers)
Closed 2 years ago.
Windows neophyte here.
I have a script to move video files from my Downloads folder to my Plex server. The script works fine.
I scheduled it to run every 30 minutes using Schtasks. This works fine but shows a window every time it starts.
schtasks.exe /CREATE /SC DAILY /MO 1 /TN 'MoveMKV' /TR 'powershell.exe C:\Users\rammjet\myApps\movemkv.ps1 -WindowStyle Hidden' /ST 07:00 /RI 30 /DU 24:00
I found an answer on Stack Overflow saying to use Start-Process to start the script with the hidden window flag. However, when I try to schedule that with Schtasks, the script does not trigger.
I tried the following two commands:
schtasks.exe /CREATE /SC DAILY /MO 1 /TN 'MoveMKV' /TR 'start-process powershell.exe -arg C:\Users\rammjet\myApps\movemkv.ps1 -WindowStyle Hidden' /ST 07:00 /RI 30 /DU 24:00
schtasks.exe /CREATE /SC DAILY /MO 1 /TN 'MoveMKV' /TR 'start-process -FilePath Powershell.exe -Args C:\Users\rammjet\myApps\movemkv.ps1 -WindowStyle Hidden' /ST 07:00 /RI 30 /DU 24:00
How can I periodically run the script without a window?
Thank you #Lee_Dailey for making me look again at a previous answer.
The 2nd answer in this post did the trick: How to run a PowerShell script without displaying a window?
I was having this same issue. I found out if you go to the Task in Task Scheduler that is running the powershell.exe script, you can click "Run whether user is logged on or not" and that will never show the powershell window when the task runs.

Schedule a Task to delete another task

I have a scheduled task in EC2 windows server 2019 that runs on start and I would like to delete after it finishes.
I tried to schedule a task to delete it but the task kept running without deleting the other one, but the scripts work from Powershell directly.
SCHTASKS /Delete /TN AfterRestartSetup /F
I tried to add /z to delete the task right after it is done but it did not work.
schtasks /create /tn "AfterRestartSetup" /sc onstart /z /rl highest /ru system /tr "powershell.exe -file C:\scripts\setup\AfterRestartSetup.ps1"
I also tried to unregister but it didn't work also:
Unregister-ScheduledTask -TaskName "AfterRestartSetup" -Confirm:$false
You can disable a task like this:
Disable-ScheduledTask -TaskPath "\your-user\" -TaskName "your-task-name"
Add the above line to a text file and save it with ps1 extension. Then create a task on Task Scheduler. In the General tab (of properties window), make sure you check Run with highest privileges. The Action should be Start a program where the Program/script is Powershell.exe and the argument has a complete path to the ps1 file you created earlier (e.g. C:\Scripts\myTask.ps1).

Running a local powershell script for all users using schtasks

Normally I use GPO to run scripts for all my users but I want to push out a powershell script and if certain conditions are not met, then it will create a schedule task for all users on that computer to run a local powershell script. (Needs to run with admin privs)
I am sure the solution is very simple but I just cannot get it to work.
I have tried all of the following including setting executionpolicy to unrestricted and moving where I am storing the script locally.
schtasks /create /f /ru "NT AUTHORITY\SYSTEM" /tn "MYTASK" /tr "powershell -file C:\ProgramData\script.ps1 -executionpolicy bypass" /sc onlogon
schtasks /create /f /tn "My Task Name" /ru Administrator /sc onlogon /tr "powershell.exe -noprofile -executionpolicy bypass -file C:\ProgramData\script.ps1"
I have also tried a .cmd file and .bat file with the following in them and calling them with the schtasks above:
powershell.exe -noprofile -executionpolicy bypass -file C:\ProgramData\script.ps1
schtasks /create /f /tn "My Task Name" /ru Administrator /sc onlogon /tr "C:\ProgramData\script.bat"
Any help is appreciated.

How can a PowerShell script be automatically run on startup?

I want to run my script at boot up time in windows 7
I tried
Setup a scheduled task to run at startup - it doesn't run until someone logs in.
Local GP to run script at startup - it doesn't run until someone logs in.
Adding the scheduled task from a command prompt with admin - some time work some time not
schtasks /create /tn "start" /sc onstart /delay 0000:30 /rl highest /ru system /tr "powershell.exe -file <>
any other method to this ?
you can make an bat file in your startup folder.
startup.bat whit this content
Powershell -command "& {c:\Temp\Test.ps1}"

Run PowerShell script in Vagrant

I use this command to run script in Vagrant via PowerShell
vagrant.exe powershell -c "SchTasks /Create /TN 'InstallTask' /SC ONCE /ST 23:59 /IT /RL HIGHEST /TR 'powershell -Command c:/vagrant/I_O.ps1' /F | Out-Host; SchTasks /Run /TN 'InstallTask' | Out-Host;"
But it works only if I start it manually from PowerShell ISE and as a separate command.
I want to use it inside this script block
# Run Vagrant
vagrant up
# Run PowerShell in Vagrant
vagrant.exe powershell -c "Set-Location C:\vagrant"
#Set policy to Unrestricted
vagrant.exe powershell -c "Set-ExecutionPolicy Unrestricted -Force"
# Install Chocolatey
vagrant.exe powershell -c "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))"
# Turn off confirmation in Chocolatey
vagrant.exe powershell -c "chocolatey feature enable -n=allowGlobalConfirmation"
# Install .net 3.5
vagrant.exe powershell -c "choco install dotnet3.5 -force"
# Run O installation script
vagrant.exe powershell -c "SchTasks /Create /TN 'InstallTask' /SC ONCE /ST 23:59 /IT /RL HIGHEST /TR 'powershell -Command c:/vagrant/I_O.ps1' /F | Out-Host; SchTasks /Run /TN 'InstallTask' | Out-Host;"