在批处理文件中使用Powershell命令

时间:2020-05-03 16:38:54

标签: powershell

我想使用此powershell命令在.bat文件中的任务计划程序中注册任务。我还有很多其他命令。我想将所有内容保存在一个批处理文件中,而不是专门为此powershell命令创建.ps1脚本。但是,此Powershell在批处理文件中不起作用。怎么了?

这是错误的意思: class GitUsers { List<Item> items; } class ... extends State<SearchController> { @override void initState() { super.initState(); gitUsers = fetchGitUsers() as List<Item>; } }

这是批处理文件中的命令:

Register-ScheduledTask : The parameter is incorrect.
At line:1 char:4

2 个答案:

答案 0 :(得分:0)

双引号内的双引号通常不起作用。取出内在的东西,反正你也不需要。您可能希望将任务设置为以“用户”组的身份运行,然后导出xml(如果希望所有用户运行它)。仅在要覆盖任务时才需要-force。必须使用-raw选项将xml作为单个字符串传递。

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

答案 1 :(得分:0)

根据我的评论,这些只是文本文件,从源到目标的导出/导入对您来说应该是最直接的。在大多数情况下,没有真正的必要弄乱文件的原始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’