我需要将参数传递给使用Windows Scheduled Task运行的PowerShell脚本。计划任务使用cmdStart-ScheduledTask -TaskName "ExampleTask*"
命令从Azure管道运行
计划任务具有如下PS脚本:
param(
[Parameter(Mandatory = $true)]
$var
)
echo $var
我需要$var
从Azure DevOps管道中动态更改。有什么方法可以做到这一点?
您可以使用Set-ScheduledTask
Azure Pipeline任务中的动态变量来更新现有的ScheduledTask。请参阅以下步骤。
1,在azure管道中创建变量,如果它是凭据,则将变量类型更改为secret。如下所示:我在管道中创建了User,Password,DynamicVariable
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] 删除。
我来说两句