Hi All,
How can I schedule a task to run immediately?
I have a script that sets a scheduled task on a remote computer, but Im having trouble having it just run once and as soon as its scheduled. AS it is now I can set whatever time I like, this is Daily at 8PM
(I know wsus can do this, but I am trying with PS)
Here is my script:
#Windowsupdates.ps1
cls
$remotecomputer = Read-Host -Prompt 'Please enter computer name to Install Windows Updates on'
$Credential = Get-Credential
$SessionRemote = New-Pssession $remotecomputer
$Path = "\\$remotecomputer\c$\Temp\"
Copy-Item -path 'C:\Temp\ITScript\WindowsUpdates\InstallAllWindowsUpdatesAndLog.ps1' -Destination 'c:\Temp' -ToSession $SessionRemote
Invoke-Command -ComputerName $remotecomputer -Credential $Credential -ScriptBlock {
$Action = New-ScheduledTaskAction -Execute "C:\Program Files\PowerShell\7\pwsh.exe" `
-Argument "C:\Temp\InstallAllWindowsUpdatesAndLog.ps1" # Specify what program to run and with its parameters
$Trigger = New-ScheduledTaskTrigger -Daily -At '8PM' # Specify the trigger settings
$User = "NT AUTHORITY\SYSTEM" # Specify the account to run the script
Register-ScheduledTask -Action $Action -Trigger $Trigger -TaskName "WindowsUpdates8PM" -User $User -RunLevel Highest -Force # Specify the name of the task
}