Run ScheduledTask on first day of month? - powershell

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.

Related

Scheduled task created in Powershell runs but does not run Powershell script?

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?

PowerShell script as a task scheduler doesn't work correctly

When I run the task scheduler it gives me a (0x1) error code. Basically this is to copy all files on the desktop and place them in Google Drive.
Backup1.ps1
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument 'C:\Users\User\Desktop\Backup2.ps1'
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "BackUP Google Drive" -Description "Google Drive Backup"
Backup2.ps1
Copy-Item "C:\Users\User\Desktop\*" -Destination "G:\My Drive\BackUp" -Recurse -Force
The Backup2.ps1 works 100% of the time when running it under PowerShell alone. The problem is that the task scheduler isn't completing the task due to the (0x1) error.

Editing existing Group Policy using PowerShell

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

Create scheduled task with power shell in a Remote Machine

How can one create a task in task scheduler on a remote machine using Power shell?
To create a task in the scheduler you can use the New-ScheduledTaskAction, New-ScheduledTaskTrigger and Register-ScheduledTask cmdlets - detailed info can be found here: https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/13/use-powershell-to-create-scheduled-tasks/
Here's an example:
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
-Argument '-NoProfile -WindowStyle Hidden -command "& {get-eventlog -logname Application -After ((get-date).AddDays(-1)) | Export-Csv -Path c:\fso\applog.csv -Force -NoTypeInformation}"'
$trigger = New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "AppLog" -Description "Daily dump of Applog"
Running Powershell remotely requires a few configuration changes, which you can find out about here: https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/

Rename a Scheduled Task Using PowerShell 4.0

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