Update a Scheduled Task Action Argument using Powershell 4.0 - powershell

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\"

Related

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

Register scheduled task with New-ScheduledTaskTrigger to trigger on event ID

Register-ScheduledTask with New-ScheduledTaskTrigger on a Windows event ID
Hello Stack-overflow users. Neither MSDN nor Google yields results...
I configured a couple of scheduled tasks via a Powershell script. The scheduled tasks are set to run at certain times.
This all works fine. But I need to configure another scheduled task which run when a certain event ID is logged in the Windows event logger.
I can set this up manually of course but I want it as part of my automated script.
this is the code I have so far for the scheduled tasks, I need to replace the $Trigger= New-ScheduledTaskTrigger -At 4:00am -Daily section:
Copy-Item "\\networkDrive\Backups\scripts\Reset-Sessions.ps1" "c:\scripts\Reset-Sessions.ps1"
$Trigger= New-ScheduledTaskTrigger -At 4:00am -Daily
$User= 'Nt Authority\System'
$Action= New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-executionpolicy bypass -File c:\scripts\Reset-Sessions.ps1"
Register-ScheduledTask -TaskName "Reset-Sessions" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest -Force
I have changed some of the directory and file names for online purposes.
I would appreciate it if somebody could steer me into the right direction or assist with an example.
I would prefer to only change the $Trigger portion and not re-write the whole script but I would understand if it is not possible.
I use Powershell version 5.1.
With this answer as a base and some additional help I was able to construct this script
$taskname="Reset-Sessions"
# delete existing task if it exists
Get-ScheduledTask -TaskName $taskname -ErrorAction SilentlyContinue | Unregister-ScheduledTask -Confirm:$false
# get target script based on current script root
$scriptPath=[System.IO.Path]::Combine($PSScriptRoot, "Reset-Sessions.ps1")
# create list of triggers, and add logon trigger
$triggers = #()
$triggers += New-ScheduledTaskTrigger -AtLogOn
# create TaskEventTrigger, use your own value in Subscription
$CIMTriggerClass = Get-CimClass -ClassName MSFT_TaskEventTrigger -Namespace Root/Microsoft/Windows/TaskScheduler:MSFT_TaskEventTrigger
$trigger = New-CimInstance -CimClass $CIMTriggerClass -ClientOnly
$trigger.Subscription =
#"
<QueryList><Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"><Select Path="Microsoft-Windows-NetworkProfile/Operational">*[System[(EventID=4004)]]</Select></Query></QueryList>
"#
$trigger.Enabled = $True
$triggers += $trigger
# create task
$User='Nt Authority\System'
$Action=New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "-ExecutionPolicy bypass -File $scriptPath"
Register-ScheduledTask -TaskName $taskname -Trigger $triggers -User $User -Action $Action -RunLevel Highest -Force
The main magic is to use Get-CimClass to get the correct instance, and then populate Subscription from Get-ScheduledTask "Tmp" | Select -ExpandProperty Triggers
Step1:
Go to eventvwr, then create a new scheduled task based on an eventid.
Step2:
Open powershell, then show the scheduled task and see the the way how to was written.
Step3:
Attached and test it in your scrip.
I created a temporary Get-ScheduledTask and run the below line : all what you have to do is to replace the Subscription to meet your requirement.
$A = (Get-ScheduledTask "Tmp" | select -ExpandProperty Triggers)
Here is another method I used in the end to solve the problem:
If you are trying to do this on a Windows Server prior to Server 2012 then you probably don't have the Get-ScheduledTask and New-ScheduledTaskTrigger and the Register-ScheduledTask modules on your Windows system. That means the script in my original question and the accepted answer won't work.
A workaround is to create the desired scheduled task manually and then export it to a xml file. You can edit this file if you like, it is normal plain text xml.
Then have your script and the xml file in the same directory.
Change your script to be compatible with the older version of Powershell to import the xml file.
schtasks /create /tn "Reset-Sessions" /xml "c:\scripts\Reset-Sessions.xml"
Your new scheduled task should now be registered and active.

Unable to pass the parameter while creating the scheduled task using powershell. I should get -version as "sql server 2016" instead of "sql"

I am trying to create a scheduled task using powershell.
able to create and execute the task properly
```
$servername ="myservername"
# Issue with the version, unable to pass. Splitting in the scheduler argument.
$version="sql server 2016"
$edition="enterprise"
$action = New-ScheduledTaskAction -Execute Powershell.exe -Argument "-File G:\ForAPI\MainFunctions_All.ps1 -Servername $servername -Version $version -Edition $edition"
$trigger = New-ScheduledTaskTrigger -Once -At 12:00PM
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "MSSQL" -Description "SQL Deployment"
```
I should get -version as "sql server 2016" instead of "sql"
You need to change the parameters as positional and make it in order like 1st servername ,then version followed by edition in the function where you are using it.
You should use single quotes for the parameters inside while the value is being held by them else you will observe partial record(exactly what you are getting now); Change the quotation like this below:
$action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-ExecutionPolicy ByPass -File G:\ForAPI\MainFunctions_All.ps1 '$servername' '$version' '$edition'"
If the parameters are all positional then you can completely avoid the -Servername , -Version and -Edition unnecessarily.
This will do the trick for you. Hope it helps.

Powershell command to create a schedule task to execute a batch file during bootup

How to use Powershell command to create a Windows schedule task to execute a batch file during startup?
PLease provide an example commands, thanks
$action = New-ScheduledTaskAction -Execute "Yourbatchhere.bat"
$trigger = New-ScheduledTaskTrigger -AtStartup
$principal = "Computername\youruseraccount"
$setting = New-ScheduledTaskSettingSet
$inputTask = -action $action -principal $principal -trigger $trigger -settings
Register-ScheduledTask BatchRunTask -InputObject $inputTask
I suggest that you try this, and fill in the fields with the appropriate information, as I pulled this almost directly off Technet. It should create a task running your batch file under your account at logon. Make sure to run it as admin to ensure it works, though.
The reference is here if you need it again later.

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