PowerShellOnTargetMachines@3 - 目标计算机上的 PowerShell v3 任务

使用此任务在远程计算机上使用 PSSession 和 Invoke-Command 执行 PowerShell 脚本进行远程处理。

语法

# PowerShell on target machines v3
# Execute PowerShell scripts on remote machines using PSSession and Invoke-Command for remoting.
- task: PowerShellOnTargetMachines@3
  inputs:
    Machines: # string. Required. Machines. 
    #UserName: # string. Username. 
    #UserPassword: # string. Password. 
  # Script options
    #ScriptType: 'Inline' # 'FilePath' | 'Inline'. Script Type. Default: Inline.
    #ScriptPath: # string. Required when ScriptType = FilePath. Script File Path. 
    InlineScript: # string. Required when ScriptType = Inline. Script. 
    #ScriptArguments: # string. Optional. Use when ScriptType = FilePath. Script Arguments. 
    #InitializationScript: # string. Optional. Use when ScriptType = FilePath. Initialization script. 
    #SessionVariables: # string. Optional. Use when ScriptType = FilePath. Session Variables. 
  # PSSession options
    #CommunicationProtocol: 'Https' # 'Http' | 'Https'. Protocol. Default: Https.
    #AuthenticationMechanism: 'Default' # 'Default' | 'Credssp'. Authentication. Default: Default.
    #NewPsSessionOptionArguments: '-SkipCACheck -IdleTimeout 7200000 -OperationTimeout 0 -OutputBufferingMode Block' # string. Session Option parameters. Default: -SkipCACheck -IdleTimeout 7200000 -OperationTimeout 0 -OutputBufferingMode Block.
  # Error handling options
    #ErrorActionPreference: 'stop' # 'stop' | 'continue' | 'silentlyContinue'. ErrorActionPreference. Default: stop.
    #failOnStderr: false # boolean. Fail on Standard Error. Default: false.
    #ignoreLASTEXITCODE: false # boolean. Ignore $LASTEXITCODE. Default: false.
  # Advanced
    #WorkingDirectory: # string. Working Directory. 
    #RunPowershellInParallel: true # boolean. Run PowerShell in Parallel. Default: true.

输入

Machines - 计算机
string。 必填。

指定计算机 FQDN 或 IP 地址的逗号分隔列表,并选择性地包括端口号。 可以是:

  • Azure 资源组的名称
  • 以逗号分隔的计算机名称列表。 示例:dbserver.fabrikam.com,dbserver_int.fabrikam.com:5986,192.168.34:5986
  • 上一个任务的输出变量。

如果未指定端口,则使用默认 WinRM 端口。 这取决于你配置的协议。 对于 WinRM 2.0,默认 HTTP 端口 5985,默认 HTTPS 端口 5986


UserName - 用户名
string

指定目标主机上的域或本地管理帐户的用户名。

  • 支持 username___domain\usernamemachine-name\username.\username 等格式。
  • 不支持 UPN 格式,例如 username@___domain.com 和内置系统帐户(如 NT Authority\System)。

UserPassword - 密码
string

指定目标计算机的密码。 接受在生成/发布定义中定义的变量,$(passwordVariable)。 可以将变量类型标记为 secret 来保护它。


ScriptType - 脚本类型
string。 允许的值:FilePath(文件路径),Inline。 默认值:Inline

指定要执行的脚本的类型:内联或文件路径。


ScriptPath - 脚本文件路径
stringScriptType = FilePath时是必需的。

指定目标计算机上或 UNC 路径(如 C:\BudgetIT\Web\Deploy\Website.ps1)上 PowerShell 脚本的位置,该位置应可从目标计算机访问。


InlineScript - 脚本
stringScriptType = Inline时是必需的。 默认值:# Write your powershell commands here.\n\nWrite-Output "Hello World"


ScriptArguments - 脚本参数
string。 可选。 当 ScriptType = FilePath时使用。

指定 PowerShell 脚本的参数。 可以是序号参数或命名参数,例如 -testParam 测试。 例如:-applicationPath $(applicationPath)-username $(vmusername)-password $(vmpassword)


InitializationScript - 初始化脚本
string。 可选。 当 ScriptType = FilePath时使用。

指定目标计算机上的 DSC 或 UNC 路径(如 C:\BudgetIT\Web\Deploy\WebsiteConfiguration.ps1)的数据脚本的位置。 建议使用参数而不是初始化脚本。


SessionVariables - 会话变量
string。 可选。 当 ScriptType = FilePath时使用。

用于设置 PowerShell 脚本的会话变量。
指定逗号分隔的列表,例如 $varx=valuex, $vary=valuey。 最常用于与早期版本的发布服务向后兼容。 建议使用参数而不是会话变量。


CommunicationProtocol - 协议
string。 允许的值:HttpHttps。 默认值:Https

指定要用于与计算机建立的 WinRM 服务连接的协议。 默认值是 HTTPS


AuthenticationMechanism - 身份验证
string。 允许的值:DefaultCredssp。 默认值:Default

指定用于创建 PSSession 的身份验证机制。 对于 CredSSP 身份验证,用户名和密码字段是必需的。


NewPsSessionOptionArguments - 会话选项参数
string。 默认值:-SkipCACheck -IdleTimeout 7200000 -OperationTimeout 0 -OutputBufferingMode Block

远程会话的高级选项(New-PSSessionOption)。 例如,-SkipCACheck-SkipCNCheck-SkipRevocationCheck等。有关详细信息,请参阅 所有会话选项的完整列表


ErrorActionPreference - ErrorActionPreference
string。 允许的值:stopcontinuesilentlyContinue。 默认值:stop

将脚本顶部 $ErrorActionPreference = 'VALUE' 行前面。


标准错误failOnStderr - 失败
boolean。 默认值:false

如果设置为 true,则如果任何错误写入错误管道或写入标准错误流中的任何数据,则失败。 否则,任务依赖于退出代码来确定失败。


ignoreLASTEXITCODE - 忽略$LASTEXITCODE
boolean。 默认值:false

如果设置为 false,则行 if ((Test-Path -LiteralPath variable:\LASTEXITCODE)) { exit $LASTEXITCODE } 在脚本末尾执行。 这将导致外部命令中的最后一个退出代码作为 PowerShell 的退出代码进行传播。 否则,该行不会执行到脚本的末尾。


WorkingDirectory - 工作目录
string

指定运行脚本的工作目录。


RunPowershellInParallel - 并行运行 PowerShell
boolean。 默认值:true

如果设置为 true,则可在目标计算机上并行运行 PowerShell 脚本。


任务控制选项

除任务输入之外,所有任务都具有控制选项。 有关详细信息,请参阅 控件选项和常见任务属性

输出变量

没有。

注解

使用此任务在远程计算机上执行 PowerShell 脚本。

此任务可以同时运行 PowerShell 脚本和 PowerShell-DSC 脚本:

  • 对于 PowerShell 脚本,计算机必须安装 PowerShell 2.0 或更高版本。
  • 对于 PowerShell-DSC 脚本,计算机必须 已安装最新版本的 Windows Management Framework。 默认情况下,这安装在 Windows 8.1、Windows Server 2012 R2 和后续版本上。

先决条件

此任务使用 Windows 远程管理(WinRM)访问已加入域或工作组的本地物理计算机或虚拟计算机。

为本地物理计算机或虚拟机设置 WinRM

按照已加入域 中所述的步骤作

为 azure 虚拟机Microsoft设置 WinRM

Azure 虚拟机要求 WinRM 使用 HTTPS 协议。 可以使用自签名测试证书。 在这种情况下,自动化代理不会验证由受信任的证书颁发机构颁发的证书的真实性。

  • Azure 经典虚拟机。 从 Azure 门户创建 经典虚拟机 时,虚拟机已为 WinRM over HTTPS 设置,默认端口 5986 已在防火墙中打开,并在计算机上安装了自签名证书。 无需进一步配置即可访问这些虚拟机。 也可以使用 Azure 资源组部署 任务来选择现有的经典虚拟机。

  • Azure 资源组。 如果已在 Azure 门户中定义 Azure 资源组,则必须将其配置为使用 WinRM HTTPS 协议。 需要在防火墙中打开端口 5986,并安装自签名证书。

若要动态部署包含虚拟机的 Azure 资源组,请使用 Azure 资源组部署 任务。 此任务具有名为“启用部署先决条件”复选框。 选择此选项可自动在虚拟机上设置 WinRM HTTPS 协议,在防火墙中打开端口 5986,并安装测试证书。 然后,虚拟机即可在部署任务中使用。

此任务版本中的新增功能

  • 使用 PSSession 和 invoke-command 在目标计算机上执行远程处理。
  • 添加了对内联脚本执行的支持。
  • 支持默认身份验证和 CredSSP 身份验证。
  • 添加了错误处理选项:标准错误 ErrorActionPreferenceignoreLASTEXITCODE失败。

要求

要求 说明
管道类型 YAML,经典版本,经典版本
运行时间 代理,DeploymentGroup
需求 没有
功能 此任务不满足作业中后续任务的任何要求。
命令限制 任何
Settable 变量 任何
代理版本 2.134.0 或更高版本
任务类别 部署