Powershell = Task Sheduler ...无法在变量中传递值

时间:2016-03-12 17:37:11

标签: powershell scheduled-tasks

我搜索了很长时间,但我找不到解决方案......

这是我的powershell代码:

$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

你有没有想法,因为我真的没有想法!

2 个答案:

答案 0 :(得分:3)

您不能将参数开关包装在字符串中,并将其作为参数提供:

$TriggerFrequency = "-Daily"
$Trigger = New-ScheduledTaskTrigger -At $TriggerTime $TriggerFrequency

由于Once是下一个可用的位置参数,解析器将字面上解释为最后一个命令:

$Trigger = New-ScheduledTaskTrigger -At $TriggerTime -Once:"-Daily"

然而,您可以将Daily参数包装在哈希表中,并使用splatting将其作为参数提供:

$TriggerFrequency = @{ Daily = $true }
$Trigger = New-ScheduledTaskTrigger -At $TriggerTime @TriggerFrequency

(注意变量名称前面的@

答案 1 :(得分:0)

我使用PowerShell使用以下代码创建计划任务。 [ScriptBlock]::Create()方法的原因是允许变量扩展,例如此代码

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

使用如下:

 ScheduledTask.ps1 -server MyServer -date "03-12-16 11:15"

这将创建一个计划的PowerShell作业,以便在2016年3月12日上午11:15运行"D:\Scripts\Scheduled-Reboot.ps1 -server $server"