我在Windows 7管理机上写了一些代码,在这里工作得很好。现在,我想从控制台-命令行运行它,并从Windows 10上的客户端计算机进行管理访问。
脚本尝试通过任务计划程序的COM对象注册任务时抛出错误:
值不在预期范围内
# Aufruf in der command line mit
# powershell.exe -file C:\Workspace\controlling-macros\TaskCreator.ps1
$path = "C:\Workspace\controlling-macros\FullWorkbookPathsMR.csv"
$csv = Import-CSV -Path $path -Delimiter ";" | % {
# The name of the scheduled task
[string]$TaskName = "$($_.reportsource) - $($_.reportname) - $($_.reportid)"
# The description of the task
[string]$TaskDescr = "Hello, it's you again! I am $($_.reportname) and I will get started, when Report ID $($_.reportid) is fired. Have a nice day!"
# The Task Action command
$TaskCommand0 = "cmd"
$TaskCommand1 = "`"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE`""
# The Task Action command argument
$TaskArg = "/c C:\Windows\System32\taskkill.exe /F /IM EXCEL.EXE"
$TaskArg1 = "$($_.fullpath)"
# attach the Task Scheduler com object
$service = new-object -ComObject("Schedule.Service")
# connect to the local machine.
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa381833(v=vs.85).aspx
$service.Connect()
$rootFolder = $service.GetFolder("\")
$TaskDefinition = $service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$triggers = $TaskDefinition.Triggers
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa383915(v=vs.85).aspx
$trigger = $triggers.Create(0) # Creates an "On an event" trigger
$trigger.Subscription = "<QueryList><Query Id='0'><Select Path='Application'>*[System[Provider[@Name='$($_.reportsource)'] and EventID='$($_.reportid)']]</Select></Query></QueryList>"
# "*[System[Provider[@Name='$reporttype'] and EventID=$reportid]]"
# http://msdn.microsoft.com/en-us/library/windows/desktop/aa381841(v=vs.85).aspx
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = "$TaskCommand0"
$action.Arguments = "$TaskArg"
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = "$TaskCommand1"
$action.Arguments = "$TaskArg1"
#http://msdn.microsoft.com/en-us/library/windows/desktop/aa381365(v=vs.85).aspx
$rootFolder.RegisterTaskDefinition("$TaskName", $TaskDefinition, 6, "System", $null, 5) # <<--- here the error occurs
Write-Host $($_.sequence) $($_.reportsource) $($_.reportname) $($_.reportid) $($_.fullpath) "task created successfully."
}
为什么没有数据通过管道传递给变量?
答案 0 :(得分:0)
我知道这似乎很基本,但是我首先要匹配Powershell版本,因为不同版本的Powershell确实编译了一些代码,并且某些cmdlet在旧版本中不起作用。
答案 1 :(得分:0)
在新计划的任务设置集中,有一个兼容性开关(v1,vista,win7,win8)。