如何给Windows定时任务传参数?
How to pass parameters to Windows Scheduled Task?
我需要将参数传递给使用 Windows 计划任务运行的 PowerShell 脚本。
使用 cmd Start-ScheduledTask -TaskName "ExampleTask*"
command
从 Azure 管道运行计划任务
Scheduled Task image
计划任务有 PS 如下脚本:
param(
[Parameter(Mandatory = $true)]
$var
)
echo $var
我需要从 Azure DevOps 管道动态更改 $var
。
有什么方法可以做到这一点?
这样配置;
Program/Script : %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
参数: -c "C:\script.ps1 -var thevalue"
脚本.ps1 :
param(
[Parameter(Mandatory = $true)]
$var
)
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Messagebox]::Show("This is the Message text : " + $var)
当路径或参数中有空格时,可以使用以下任何引用变体;
-c "& 'c:\script.ps1'" -var 'the param'
-c "& ""c:\script.ps1""" -var 'the param'
参数中的引号;
-c "& ""c:\script.ps1""" -var 'the x\"x''param'
-c "& ""c:\script.ps1""" -var 'the x"""x''param'
您可以使用 Set-ScheduledTask
使用 Azure Pipeline 任务中的动态变量更新现有的 ScheduledTask。请参阅以下步骤。
1、在你的azure pipeline中创建变量,如果是credential,则将变量类型改为secret。见下文:我在管道
中创建了User、Password、DynamicVariable
2、在您的管道中添加一个 powershell 任务以更新您现有的 ScheduledTask。
我在计划任务中将 Arguments 设置为:-NoProfile -ExecutionPolicy Bypass -File "c:\test\scheduled.ps1" -var "$(DynamicVariable)"'
请参阅 Powershell 任务中的以下脚本。
#update the Argument with variable defined in the pipeline $(DynamicVariable)
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File "c:\test\scheduled.ps1" -var "$(DynamicVariable)"'
#update the scheduled task
Set-ScheduledTask -Password "$(Password)" -User "$(User)" -TaskName "PipelineTask" -Action $Action
Start-ScheduledTask -TaskName "MyTask"
如果要在管道中动态设置变量DynamicVariable
。您可以使用 logging commands "##vso[task.setvariable variable..]..
.
在上面的 powershell 任务之前添加另一个 powershell 任务到 运行 下面的命令:
echo "##vso[task.setvariable variable=DynamicVariable]newValue"
我需要将参数传递给使用 Windows 计划任务运行的 PowerShell 脚本。
使用 cmd Start-ScheduledTask -TaskName "ExampleTask*"
command
Scheduled Task image
计划任务有 PS 如下脚本:
param(
[Parameter(Mandatory = $true)]
$var
)
echo $var
我需要从 Azure DevOps 管道动态更改 $var
。
有什么方法可以做到这一点?
这样配置;
Program/Script : %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
参数: -c "C:\script.ps1 -var thevalue"
脚本.ps1 :
param(
[Parameter(Mandatory = $true)]
$var
)
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Messagebox]::Show("This is the Message text : " + $var)
当路径或参数中有空格时,可以使用以下任何引用变体;
-c "& 'c:\script.ps1'" -var 'the param'
-c "& ""c:\script.ps1""" -var 'the param'
参数中的引号;
-c "& ""c:\script.ps1""" -var 'the x\"x''param'
-c "& ""c:\script.ps1""" -var 'the x"""x''param'
您可以使用 Set-ScheduledTask
使用 Azure Pipeline 任务中的动态变量更新现有的 ScheduledTask。请参阅以下步骤。
1、在你的azure pipeline中创建变量,如果是credential,则将变量类型改为secret。见下文:我在管道
中创建了User、Password、DynamicVariable2、在您的管道中添加一个 powershell 任务以更新您现有的 ScheduledTask。
我在计划任务中将 Arguments 设置为:-NoProfile -ExecutionPolicy Bypass -File "c:\test\scheduled.ps1" -var "$(DynamicVariable)"'
请参阅 Powershell 任务中的以下脚本。
#update the Argument with variable defined in the pipeline $(DynamicVariable)
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File "c:\test\scheduled.ps1" -var "$(DynamicVariable)"'
#update the scheduled task
Set-ScheduledTask -Password "$(Password)" -User "$(User)" -TaskName "PipelineTask" -Action $Action
Start-ScheduledTask -TaskName "MyTask"
如果要在管道中动态设置变量DynamicVariable
。您可以使用 logging commands "##vso[task.setvariable variable..]..
.
在上面的 powershell 任务之前添加另一个 powershell 任务到 运行 下面的命令:
echo "##vso[task.setvariable variable=DynamicVariable]newValue"