如何将参数传递给Windows计划任务?

叶夫根尼·阿基莫夫(Evgeny Akimov)

我需要将参数传递给使用Windows Scheduled Task运行的PowerShell脚本。计划任务使用cmdStart-ScheduledTask -TaskName "ExampleTask*"命令从Azure管道运行

预定任务图像

计划任务具有如下PS脚本:

param(
    [Parameter(Mandatory = $true)]
    $var
)

echo $var

我需要$var从Azure DevOps管道中动态更改有什么方法可以做到这一点?

李维·鲁

您可以使用Set-ScheduledTaskAzure Pipeline任务中的动态变量来更新现有的ScheduledTask。请参阅以下步骤。

1,在azure管道中创建变量,如果它是凭据,则将变量类型更改为secret。如下所示:我在管道中创建了UserPasswordDynamicVariable

在此处输入图片说明

2,在管道中添加一个powershell任务,以更新现有的ScheduledTask。

在预定任务中将参数设置为:-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在管道中动态设置变量您可以使用日志记录命令 "##vso[task.setvariable variable..]..

在上述powershell任务之前添加另一个powershell任务,以在以下命令中运行:

echo "##vso[task.setvariable variable=DynamicVariable]newValue"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章