I search for a long time and I never find a solution....
Here is my powershell code :
$TaskName = "Backup"
$TaskDescription = "Scheduled Task for Backup."
$GeneralServiceAccount = "SRVBK$" ## It's a gMSA https://technet.microsoft.com/en-us/library/hh831782.aspx
$GeneralServiceAccountLogonType = "Password"
$GeneralRunLevel = "Highest"
$ActionScriptPath = "C:\Server\Backup\$TaskName.ps1"
$ActionTaskProgramStart = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$ActionTaskProgramStartArgument = "-NoProfile -ExecutionPolicy Unrestricted -File $ActionScriptPath"
$TriggerTime = "20:00"
$TriggerFrequency = "-Daily"
## Tab General
$General = New-ScheduledTaskPrincipal -UserID $GeneralServiceAccount -LogonType $GeneralServiceAccountLogonType -RunLevel $GeneralRunLevel
## Tab Trigger
$Trigger = New-ScheduledTaskTrigger -At $TriggerTime $TriggerFrequency ## This one doesn't work
$Trigger = New-ScheduledTaskTrigger -At 20:00 -Daily ## This one works like a charm
## Tab Action
$Action = New-ScheduledTaskAction -Execute $ActionTaskProgramStart -Argument $ActionTaskProgramStartArgument
## Create the Task
Register-ScheduledTask $TaskName –Principal $General –Trigger $Trigger –Action $Action -Description $TaskDescription
New-ScheduledTaskTrigger : Cannot process argument transformation on parameter 'Once'. Cannot convert value "System.String" to type "System.Management.Automation.SwitchParameter". Boolean parameters accept only Boolean values and numbers,
such as $True, $False, 1 or 0.
At line:20 char:54
+ $Trigger = New-ScheduledTaskTrigger -At $TriggerTime $TriggerFrequency ## This o ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-ScheduledTaskTrigger], ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,New-ScheduledTaskTrigger
Register-ScheduledTask : No mapping between account names and security IDs was done.
(21,8):UserId:
At line:26 char:1
+ Register-ScheduledTask $TaskName –Principal $General –Trigger $Trigger –Action $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (PS_ScheduledTask:Root/Microsoft/...S_ScheduledTask) [Register-ScheduledTask], CimException
+ FullyQualifiedErrorId : HRESULT 0x80070534,Register-ScheduledTask
Did you have a idea because I really don't have one !
You can't just wrap a parameter switch in a string and supply it like an argument:
$TriggerFrequency = "-Daily"
$Trigger = New-ScheduledTaskTrigger -At $TriggerTime $TriggerFrequency
Since Once
is the next available positional parameter, the parser will interpret the last command literally as:
$Trigger = New-ScheduledTaskTrigger -At $TriggerTime -Once:"-Daily"
You can however wrap the Daily
parameter in a hashtable and use splatting to supply it as a parameter:
$TriggerFrequency = @{ Daily = $true }
$Trigger = New-ScheduledTaskTrigger -At $TriggerTime @TriggerFrequency
(notice the @
in front of the variable name)
I create scheduled tasks using PowerShell with the following code. The reason for the [ScriptBlock]::Create()
method was to allow variable expansion, for example this code
Param([Parameter(Mandatory=$true,
HelpMessage="ServerName goes here")]
[string]$server,
[Parameter(Mandatory=$true,
HelpMessage="Enter a Date/Time 07-28-15 16:00 For July 28th, 2015 at 4:00 PM")]
[ValidatePattern('(?#Enter MM-dd-YY HH:MM 24hr clock)^(0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[-](\d{2})\s([012]?)([0-9])[:]([012345])([0-9])\Z')]
$date
)
#Get username and ask for password, password is encrypted.
$user = Get-Credential -UserName $env:USERNAME -Message "UserName/password for scheduled Reboot"
#Create job trigger at date given, in format of "03-12-16 11:11"
$trigger = New-JobTrigger -once -at $date
#Create script block to run PowerShell script along with a variable
$script = [ScriptBlock]::Create("D:\Scripts\Scheduled-Reboot.ps1 -server $server")
#Finally register the scheduled job
Register-ScheduledJob -Name $server -Credential $user -Trigger $trigger -ScriptBlock $script
Use as follows:
ScheduledTask.ps1 -server MyServer -date "03-12-16 11:15"
This will create a scheduled PowerShell job to run "D:\Scripts\Scheduled-Reboot.ps1 -server $server"
on March 12th, 2016 at 11:15 AM.
User contributions licensed under CC BY-SA 3.0