【问题标题】:How to pass parameters to Windows Scheduled Task?如何将参数传递给 Windows 计划任务?
【发布时间】:2020-10-30 06:38:51
【问题描述】:

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

Scheduled Task image

Scheduled Task 有这样的 PS 脚本:

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

echo $var

我需要从 Azure DevOps 管道动态更改 $var。 有没有办法做到这一点?

【问题讨论】:

    标签: powershell scheduled-tasks


    【解决方案1】:

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

    1,在你的天蓝色管道中创建变量,如果它是凭据,则将变量类型更改为秘密。见下文:我在管道中创建了 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。你可以使用logging commands"##vso[task.setvariable variable..]..

    在上面的 powershell 任务之前添加另一个 powershell 任务以运行以下命令:

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

    【讨论】:

    • 您好,谢谢您的回复,我喜欢这种方法,但是有一些方法可以在与 SchedulerTask 一起运行的 PS 脚本中使用 Azure Pipeline 中的秘密变量吗?例如,如果我在 PS 脚本中使用密码
    • 您只需将 azure 管道中定义的秘密变量(例如密码)传递给$ActionNew-ScheduledTaskAction -Execute "PowerShell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File "c:\test\scheduled.ps1" -var "$(Password)"'
    • 是的,但是当我像这样传递秘密变量时,它会在 TaskScheduler Actions 部分中可见
    • 您可以尝试在powershell任务中映射秘密变量。例如将其映射到powershell任务MyPassword = $(Password)中的环境变量MyPassword,然后将环境变量$env:MyPassword传递给$ActionNew-ScheduledTaskAction -Execute "PowerShell.exe" -Argument '-NoProfile -ExecutionPolicy Bypass -File "c:\test\scheduled.ps1" -var "$env:MyPassword"'。请参阅here 了解更多信息
    【解决方案2】:

    这样配置;

    程序/脚本: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe

    参数: -c "C:\script.ps1 -var thevalue"

    script.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'
    

    【讨论】:

    • 嗨!感谢您的回答,我想直接从 Azure Pipeline 任务更改变量,该任务使用 Start-ScheduledTask 命令触发已经存在的 ScheduledTask。但是正如我在您的方法中所理解的那样,您直接在 Arguments 字段中设置变量值,但这有点不安全,因为如果我想在这里传递一些凭据,它们将是可见的。
    猜你喜欢
    • 2010-11-08
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2014-08-23
    相关资源
    最近更新 更多