Is there an amendment to the following script which would allow me to rename a Scheduled Task?
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe"
Set-ScheduledTask -TaskName "YourTaskName" -Action $Action
Thanks
Related
I have a PS script that runs just fine when run manually, but I am trying to set it to run daily and can't seem to figure out why it won't run. The scheduled task itself will start, but the script doesn't actually seem to run. Any help would be appreciated.
This is what I used to create the scheduled task:
$Trigger= New-ScheduledTaskTrigger -At 11:00am –Daily
$User= "NT AUTHORITY\SYSTEM"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File G:\Path\ScriptName.ps1"
Register-ScheduledTask -TaskName "TaskyTask" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
I created a script on my machine # C:\test\ScriptName.ps1
The script is:
Write-Output "HelloWorld" | Out-File C:\test\HelloWorld.txt
So I can verfiy it's success by seeing if HelloWorld.txt has been created in C:\Test
With a tweak to your code to account for the differnce in my script location:
$Trigger= New-ScheduledTaskTrigger -At 11:00am –Daily
$User= "NT AUTHORITY\SYSTEM"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\Test\ScriptName.ps1"
Register-ScheduledTask -TaskName "TaskyTask" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force
The task appears in task scheduler, I run it and HelloWorld.txt is created, confirming it works. This to me suggests your script is executing, but maybe not doing what you think.
If you change your script to the same simple script above, does it run?
I have been created on my DC Group Policy which creates Schedule Task.
My question - How can I edit the command action inside the Schedule Task using PowerShell?
Do this via New-ScheduledTaskAction and Set-ScheduledTask
$action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument '-Command Example-Command'
Set-ScheduledTask -TaskName 'ExistingTaskName' -Action $action
When using following powershell script which creates a scheduled task to delete another scheduled task, I am confronted with an error (access denied: 0x80070005). When I manually define the task and execute it, it works fine.
# Prep task 2 for taskscheduler > delete task 1 after reboot if not deleted before
$Action2 = New-ScheduledTaskAction -Execute "C:\Windows\system32\cmd.exe" -Argument "/c schtasks /delete /tn 'Computer Forced Reboot'/f"
$Trigger2 = New-ScheduledTaskTrigger -AtLogon
$Settings2 = New-ScheduledTaskSettingsSet -DontStopIfGoingOnBatteries -Compatibility Win8 -RestartCount:10 -RestartInterval (New-TimeSpan -Minutes 1)
$Principal2 = New-ScheduledTaskPrincipal -UserID "domain\$env:UserName" -LogonType Interactive
# Register task 2 to Taskscheduler
Register-ScheduledTask -TaskName "test test" -Action $Action2 -Trigger $Trigger2 -Settings $Settings2 -Principal $Principal2here
What am I doing wrong? Is this not possible (specific rights) trough powershell? Is there a workaround?
$Trigger = New-ScheduledTaskTrigger -AtLogOn
$User = "Administrator"
$Action = New-ScheduledTask -Execute "PowerShell_ISE.exe" -Argument "C:\Payload\XML_Read.ps1"
Register-ScheduledTask -TaskName "StartupScript_PS" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force
This is my code which creates a scheduled task and runs fine upon logon. the problem is when it logs on it opens PowerShell and the XML_Read file but I have to manually click run for the XML file to be read etc. Is there a way I can modify my code to do this for me? thanks in anticipation.
You can't execute scripts automatically with the ISE. Instead of PowerShell_ISE.exe, use PowerShell.exe.
I am trying to run a batch file from powershell, the script should run every 1 of the month.
But what this does is, it runs the batch file regardless of the time given.
I'm totally new to powershell so can't figure it out.
$trigger= New-ScheduledTaskTrigger -At 04:26pm –Daily # Specify the trigger settings
$action = Start-Process -FilePath C:\Users\xxx\abc.bat -Wait -passthru;$a.ExitCode
Register-ScheduledTask -Action $action -Trigger $trigger
this example is from https://www.verboon.info/2013/12/powershell-creating-scheduled-tasks-with-powershell-version-3/
the action is not Start-Process but New-ScheduledTaskAction
$TaskAction = New-ScheduledTaskAction -Execute "$TaskCommand" -Argument "$TaskArg"
$TaskTrigger = New-ScheduledTaskTrigger -At $TaskStartTime -Once
Register-ScheduledTask -Action $TaskAction -Trigger $Tasktrigger -TaskName "$TaskName" -User "System" -RunLevel Highest
There is a great answer here it shows you how to create a job to run daily, weekly or monthly in Powershell. Running monthly is a little more difficult as Powershell doesn't have a parameter option for monthly.