Editing existing Group Policy using PowerShell - 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

Related

Deploy gMSA account as task scheduler user account

I am trying to create a task on windows 2016 server, and need to deploy gMSA account as the log on account and below is the script i am using, i need to ensure that the option- "Run whether user is logged or not" gets selected,what change should be made to below code?
$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "NoProfile -ExecutionPolicy Unrestricted C:\Admin\Scripts\test.ps1 "
$trigger = New-ScheduledTaskTrigger -daily -At 5:05am
$Pri = New-ScheduledTaskPrincipal -UserId "Domain\gMSA" -LogonType ServiceAccount -RunLevel Highest
$task = New-ScheduledTask -Action $action -Trigger $trigger
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Taskname" -Principal $Pric
This is a similar request as the SO topic and answers / accepted answer.
Set a Scheduled Task to run when user isn't logged in
But since you are using a gMSA, you'd never know what that password is.
So, you can create the task normally and then do say this...
schtasks /change /TN \YourTaskName /RU DOMAIN\gMSA_Name$ /RP
Or in pure PowerShell, you again set the Scheduled Task and then do this...
New-ScheduledTaskPrincipal -UserID Domain\GMServiceAccount$ -LogonType Password
See the details of the above here:
Active Directory - Scheduled Tasks Using a gMSA

Run ScheduledTask on first day of month?

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.

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

Update a Scheduled Task Action Argument using Powershell 4.0

Does anyone know how to update the Arguments of a Scheduled Task Action using PowerShell?
Here is how I have been told to update the Action, thanks to #Richard 's answer in another question.
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe"
Set-ScheduledTask -TaskName "YourTaskName" -Action $Action
What do I need to add to this so I can also change the Argument and I suppose whilst we are here, the Start In option as well?
Use the -Argument parameters to add an argument string to an action. And use the -WorkingDirectory parameter to add a Start In option.
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument 'Arg1 Arg2' -WorkingDirectory "C:\StartInThisFolder\"