2014-12-08 35 views
0

我想部署一個Windows服務使用MSBuild腳本運行Powershell命令。MSBuild與憑據調用Powershell

的MSBuild的腳本部署文件,我需要和PowerShell腳本將卸載,並使用此命令重新安裝Windows服務:

調用命令-ComputerName IPAddressHere -FilePath「C:\ theScriptFileName.ps1」 -credential「 TheUserName「

使用IP地址(我需要因爲不同的域)我需要使用憑據。問題是它會提示輸入密碼,這對TeamCity的自動化不起作用。

我知道我可以保存憑據到一個變量,以便及時將無法顯示,但我需要得到它成一條線像下面那的MSBuild可以執行:

powershell.exe -nonInteractive -executionpolicy Unrestricted -command「& Invoke-Command -ComputerName IPAddressHere -FilePath'C:\ theScriptFileName.ps1'」

有沒有適當的方法來做到這一點?

回答

0

也許這樣?第一

function Export-Credential($cred, $path) { 
    $cred.Password = $cred.Password | ConvertFrom-SecureString 
    $cred | Export-Clixml $path 
} 
function Import-Credential($path) { 
    $cred = Import-Clixml $path 
    $cred.password = $cred.Password | ConvertTo-SecureString 
    New-Object System.Management.Automation.PSCredential($cred.username, $cred.password) 
} 

保存憑據常會與將要運行建立在同一臺機器上相同的用戶:

$Creds = $host.ui.PromptForCredential("Need credentials", "Please enter username/password with proper rights on objects to manage.`r`n`r`nExample: AD-Domain\username", $env:userdomain + "\" + $env:username, "") 
$IPAddressHere = "192.168.0.1" 
powershell.exe -NonInteractive -executionpolicy Unrestricted -command "& {Invoke-Command -ComputerName $IPAddressHere -FilePath 'C:\theScriptFileName.ps1' -credentials $creds}" 
2

Lee Holmes' article on exporting credentials使用的代碼。 (那麼,在每個這樣的機器和用戶配置文件上。)然後,在構建腳本中,從相同路徑導入Credential並將新的$ cred傳遞給Invoke-Command。