[英]Use Powershell Command In Batch File
我想使用这个 powershell 命令在 my.bat 文件的任务调度程序中注册一个任务。 我还有很多其他命令。 我想将所有内容保存到一个批处理文件中,而不是专门为此 powershell 命令创建 a.ps1 脚本。 但是,这个 powershell 在批处理文件中不起作用。 它有什么问题。
这就是错误所说的: Register-ScheduledTask: The parameter is incorrect. At line:1 char:4
Register-ScheduledTask: The parameter is incorrect. At line:1 char:4
这是批处理文件中的命令:
powershell -command " &{Register-ScheduledTask -Xml (get-content "C:\Users\Disables_Updates.xml" | out-string) -TaskName "\Disables_Updates" -User $env:USERNAME –Force}"
双引号内的双引号通常不起作用。 取出内在的,无论如何你都不需要。 您可能希望将任务设置为“用户”组运行,然后导出 xml,如果您希望所有用户都运行它。 -force 仅在您想要覆盖任务时才需要。 xml 必须使用 -raw 选项作为单个字符串输入。
powershell "get-content -raw c:\users\disables_updates.xml | Register-ScheduledTask \Disables_Updates –Force"
根据我的评论,这些只是文本文件,从源导出/导入到目标应该是最直接的。 在大多数情况下,不需要弄乱文件的原始 XML。
只需创建 a.ps1 并从一个简单的批处理文件调用 the.ps1 ,而不是尝试在必须正确引用的字符串中传递一堆命令行级别的东西,等等。
Get-ChildItem -Path 'C:\Windows\System32\Tasks'
Export-ScheduledTask 'TestTask' |
out-file '\\TargetServer\c$\public\TestTask.xml'
Invoke-Command -ComputerName 'TargetServer' -ScriptBlock {
Register-ScheduledTask -Xml (Get-Content 'C:\Users\public\TestTask.xml' | out-string) -TaskName 'TestTask'
}
# Messing with the XML
# Create your task
$A = New-ScheduledTaskAction –Execute 'powershell' -Argument 'Hello, from task scheduler'
$T = New-ScheduledTaskTrigger -Weekly -WeeksInterval 1 -DaysOfWeek Monday,Tuesday,Wednesday,Thursday,Friday -At 8am
$S = New-ScheduledTaskSettingsSet
$D = New-ScheduledTask -Action $A -Trigger $T -Settings $S
$Task = Register-ScheduledTask 'TestTask' -InputObject $D
# View the created task XML
Get-Content -Path 'C:\Windows\System32\Tasks\TestTask'
# capture the task to work with
$Task = Get-ScheduledTask -TaskName 'TestTask'
# Step through the task information.
$Task | Select *
State : Ready
Actions : {MSFT_TaskExecAction}
Author :
Date :
Description :
Documentation :
Principal : MSFT_TaskPrincipal2
SecurityDescriptor :
Settings : MSFT_TaskSettings3
Source :
TaskName : TestTask
TaskPath : \
Triggers : {MSFT_TaskWeeklyTrigger}
URI :
Version :
PSComputerName :
CimClass : Root/Microsoft/Windows/TaskScheduler:MSFT_ScheduledTask
CimInstanceProperties : {Actions, Author, Date, Description...}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties
$ScheduleTaskKeys =
'State',
'Actions',
'Author',
'Date',
'Description',
'Documentation',
'Principal',
'SecurityDescriptor',
'Settings',
'Source',
'TaskName',
'TaskPath',
'Triggers',
'URI',
'Version',
'PSComputerName'
ForEach($TaskKey in $ScheduleTaskKeys)
{$Task.$TaskKey | Format-List -Force}
# View as JSON
$Task | ConvertTo-Json
# Example XML config
# Configuring triggers
$Task.Triggers | Format-List -Force
Enabled : True
EndBoundary :
ExecutionTimeLimit :
Id :
Repetition : MSFT_TaskRepetitionPattern
StartBoundary : 2018-11-10T08:00:00
DaysOfWeek : 62
RandomDelay : P0DT0H0M0S
WeeksInterval : 1
PSComputerName :
$Task.Triggers.Repetition | Format-List * -Force
Duration :
Interval :
StopAtDurationEnd : False
PSComputerName :
CimClass : Root/Microsoft/Windows/TaskScheduler:MSFT_TaskRepetitionPattern
CimInstanceProperties : {Duration, Interval, StopAtDurationEnd}
CimSystemProperties : Microsoft.Management.In
# Modify the trigger repetition settings, which cannot be done via the native cmdlet
$Task.Triggers.Repetition.Duration = 'P1D'
$Task.Triggers.Repetition.Interval = 'PT60M'
$Task | Set-ScheduledTask -User $env:USERNAME
TaskPath TaskName State
-------- -------- -----
\ TestTask Ready
# View the change
$Task.Triggers.Repetition | Format-List * -Force
Duration : P1D
Interval : PT60M
StopAtDurationEnd : False
PSComputerName :
CimClass : Root/Microsoft/Windows/TaskScheduler:MSFT_TaskRepetitionPattern
CimInstanceProperties : {Duration, Interval, StopAtDurationEnd}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties
# Modify the XML file directly – say the repetition times settings using a simple replace, to something else
(Get-Content -Path ‘C:\Windows\System32\Tasks\TestTask’).Replace(‘P1D’,’P12H’) |
Set-Content -Path ‘C:\Windows\System32\Tasks\TestTask’
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.