2017-09-24 61 views
0

我爲自定義TFS任務(構建和發佈任務)編寫了一個powershell腳本。現在我需要執行一些具有特定憑證的命令。爲此,我創建以下語句:自定義TFS任務 - 執行具有特定憑據的Powershell腳本

Start-Process powershell -Credential $mycred -Wait -ArgumentList "-file $taskDir\task.ps1" -RedirectStandardOutput C:\Temp\taskOutput.log 

如果我在powershell中執行該命令,那麼一切正常。但是一旦命令將從TFS服務中執行,它就不起作用。如果我刪除-Credential $mycred參數,該命令也可以在TFS執行的上下文中運行。

我想問題是,與-Credential $mycred打開一個新窗口。所以它在TFS執行中不起作用。

任何人都知道更好的解決方案來執行具有特定憑證的PowerShell腳本?

謝謝!

UPDDATE 1:

爲了更好地理解我上傳全定製任務here

+0

你使用的是什麼TFS版本? – tukan

+0

一般來說,我會建議使用已經創建的解決方案 - https://github.com/huserben/TfsExtensions/tree/master/BuildTasks。如果你想有自定義的BuildTasks,你可以擴展一個github。 – tukan

+0

構建任務執行時,它使用TFS構建服務帳戶?你的意思是你想用構建管道中的另一個憑證運行任務嗎? –

回答

0

當你排隊的構建,所有建設任務,您應該生成服務帳戶下運行,如網絡服務。如果您運行該腳本,則會彈出一個PS窗口並再次立即關閉。它不能直接以不同的用戶身份運行腳本。

TFS構建允許您通過構建 定義中的設置來訪問PAT令牌。這些都是即時生成的PAT令牌,因此您不需要在任何地方存儲任何密碼。

要在開發人員的機器上運行腳本,您可以要求 開發人員輸入PAT或使用if if邏輯,您可以在其中詢問用戶密碼 。

更多詳細信息請參考以下鏈接: https://docs.microsoft.com/en-us/vsts/build-release/actions/scripts/powershell#use-the-oauth-token-to-access-the-rest-api

你也可以看看這個類似的問題:Powershell/VSTS Build - Store Credentials Independent/ Agnostic of User Running Script

+0

謝謝,但這並不能解決我的問題。我的問題是,如果我想執行一個具有特定憑證的腳本,那麼powershell會爲'Start-Process'命令打開一個新窗口。但是通過從tfs構建服務中執行此操作,將不會執行'Start-Process'參數中指定的進程。 –

0

使用調用命令來代替。相關主題:Start-Job with credential in custom task problems

$mypwd = ConvertTo-SecureString -String "[password, could use variable]" -Force -AsPlainText 
$Cred = New-Object System.Management.Automation.PSCredential('[user name]',$mypwd) 
$scriptToExecute = 
{ 
$VerbosePreference='Continue' 
Write-Output "$env:UserName" 
# Write-Verbose "Verbose" 4>&1 
} 
$b = Invoke-Command -ComputerName localhost -ScriptBlock $scriptToExecute -Credential $Cre 
相關問題