使用此工作在遠端電腦上使用 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
-
Username
string
。
指定目標主機上網域或本機系統管理帳戶的用戶名稱。
- 支援
username
、domain\username
、machine-name\username
和.\username
等格式。 - 不支援
username@domain.com
和內建系統帳戶等 UPN 格式,例如NT Authority\System
。
UserPassword
-
密碼
string
。
指定目標計算機的密碼。 接受建置/發行定義中定義為 $(passwordVariable)
的變數。 您可以將變數類型標示為 secret
來保護它。
ScriptType
-
文稿類型
string
。 允許的值:FilePath
(檔案路徑),Inline
。 預設值:Inline
。
指定要執行的文稿類型:內嵌或檔案路徑。
ScriptPath
-
腳本檔案路徑
string
。
ScriptType = FilePath
時為必要項。
指定目標計算機上或 UNC 路徑上 PowerShell 腳本的位置,例如 C:\BudgetIT\Web\Deploy\Website.ps1
,這應該可從目標計算機存取。
InlineScript
-
文稿
string
。
ScriptType = 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 路徑上 DSC 的資料文稿位置,例如 C:\BudgetIT\Web\Deploy\WebsiteConfiguration.ps1
。 建議使用自變數,而不是初始化腳本。
SessionVariables
-
會話變數
string
。 選擇性。
ScriptType = FilePath
時使用 。
用來設定PowerShell腳本的會話變數。
指定逗號分隔清單,例如 $varx=valuex, $vary=valuey
。 最常用於與舊版發行服務的回溯相容性。 建議使用自變數,而不是會話變數。
CommunicationProtocol
-
通訊協定
string
。 允許的值:Http
、Https
。 預設值:Https
。
指定要用於與機器之 WinRM 服務連線的通訊協定。 預設值是 HTTPS
。
AuthenticationMechanism
-
驗證
string
。 允許的值:Default
、Credssp
。 預設值:Default
。
指定用來建立 PSSession 的驗證機制。 針對 CredSSP
驗證,用戶名稱和密碼欄位是必要的。
NewPsSessionOptionArguments
-
工作階段選項參數
string
。 預設值:-SkipCACheck -IdleTimeout 7200000 -OperationTimeout 0 -OutputBufferingMode Block
。
遠程會話的進階選項 (New-PSSessionOption)。 例如,-SkipCACheck
、-SkipCNCheck
、-SkipRevocationCheck
等。若要深入瞭解,請參閱 所有會話選項的完整清單。
ErrorActionPreference
-
ErrorActionPreference
string
。 允許的值:stop
、continue
、silentlyContinue
。 預設值: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 驗證。
- 已新增錯誤處理的選項:
ErrorActionPreference
、ignoreLASTEXITCODE
和 標準錯誤失敗。
需求
要求 | 說明 |
---|---|
管線類型 | YAML、傳統組建、傳統版本 |
執行於 | Agent、DeploymentGroup |
需求 | 沒有 |
功能 | 此工作不符合作業中後續工作的任何需求。 |
命令限制 | 任意 |
Settable 變數 | 任意 |
代理程式版本 | 2.134.0 或更新 |
工作類別 | 部署 |