简体   繁体   English

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

[英]Use Powershell Command In Batch File

I want to use this powershell command to register a task in task scheduler in my.bat file.我想使用这个 powershell 命令在 my.bat 文件的任务调度程序中注册一个任务。 I have lots of other commands as well.我还有很多其他命令。 I want to keep everything to one batch file and not create a.ps1 script specifically for this powershell command.我想将所有内容保存到一个批处理文件中,而不是专门为此 powershell 命令创建 a.ps1 脚本。 HOWEVER, this powershell does NOT work in batch file.但是,这个 powershell 在批处理文件中不起作用。 What is wrong with it.它有什么问题。

This is what the error says: Register-ScheduledTask: The parameter is incorrect. At line:1 char:4这就是错误所说的: Register-ScheduledTask: The parameter is incorrect. At line:1 char:4 Register-ScheduledTask: The parameter is incorrect. At line:1 char:4

This is the command IN the batch file:这是批处理文件中的命令:

powershell -command " &{Register-ScheduledTask -Xml (get-content "C:\Users\Disables_Updates.xml" | out-string) -TaskName "\Disables_Updates" -User $env:USERNAME –Force}"

Doublequotes inside doublequotes don't normally work.双引号内的双引号通常不起作用。 Take out the inner ones, that you don't need anyway.取出内在的,无论如何你都不需要。 You probably want to set the task to run as the group "Users", and then export the xml, if you want all users to run it.您可能希望将任务设置为“用户”组运行,然后导出 xml,如果您希望所有用户都运行它。 -force is only needed if you want to overwrite the task. -force 仅在您想要覆盖任务时才需要。 The xml has to be piped in as a single string with the -raw option. xml 必须使用 -raw 选项作为单个字符串输入。

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

As per my comment, these are just text files, and export/import from source to a destination should be the most direct for you.根据我的评论,这些只是文本文件,从源导出/导入到目标应该是最直接的。 No real need to mess with the raw XML of the file in most cases.在大多数情况下,不需要弄乱文件的原始 XML。

Just create a.ps1 and call the.ps1 from a simple batch file vs trying to pass a bunch of command-line level stuff in a string that has to be properly quoted, etc.只需创建 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM