【问题标题】:Use Powershell Command In Batch File在批处理文件中使用 Powershell 命令
【发布时间】:2020-05-03 16:38:54
【问题描述】:

我想使用这个 powershell 命令在我的 .bat 文件中的任务调度程序中注册一个任务。我还有很多其他命令。我想将所有内容保存到一个批处理文件中,而不是专门为此 powershell 命令创建 .ps1 脚本。但是,此 powershell 不适用于批处理文件。它有什么问题。

这就是错误所说的: 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}"

【问题讨论】:

  • powershell -command "& {Register-ScheduledTask -Xml $(get-content 'C:\Users\Disables_Updates.xml' | out-string) -TaskName '\Disables_Updates' -User $env:USERNAME –Force}"this answer
  • 计划任务只是在一台主机上创建的xml文件,可以从主机导出,然后复制粘贴到任意目标,直接导入。

标签: powershell


【解决方案1】:

双引号内的双引号通常不起作用。取出内在的,无论如何你都不需要。如果您希望所有用户都运行它,您可能希望将任务设置为“用户”组运行,然后导出 xml。 -force 仅在您想要覆盖任务时才需要。 xml 必须使用 -raw 选项作为单个字符串输入。

powershell "get-content -raw c:\users\disables_updates.xml | Register-ScheduledTask \Disables_Updates –Force"

【讨论】:

  • 它对我有用。 xml 是什么样的?您可以将其添加到问题中吗?
  • 好的,它在系统启动时以系统用户身份运行,如果您使用 $env:username 则以当前用户身份运行。这个对我有用。我不知道还能告诉你什么。
【解决方案2】:

根据我的评论,这些只是文本文件,从源到目标的导出/导入对您来说应该是最直接的。在大多数情况下,不需要弄乱文件的原始 XML。

只需创建一个 .ps1 并从一个简单的批处理文件调用 .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’

【讨论】:

  • 如何为“工作站解锁”添加触发器?它无限期地每隔几分钟运行一次
  • 您无法将新触发器添加到正在执行的 ST。 ST 可以有多个触发器,尽管这种努力是基于时间的:Task TriggersAdding a second trigger in task schedule。但是,Windows 正确的启动/登录/解锁都是不同的事件操作,所以......就是这样。
  • 我看到您将此作为单独的问题发布 --- stackoverflow.com/questions/61644860/… --- 您已经从 Mike Shepard 那里得到了答案。
猜你喜欢
  • 2019-11-15
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多