执行powershell远程问题

时间:2016-11-02 09:00:00

标签: powershell tfs build passwords

嗨,当我尝试做一些代码时:

$Username = 'us'
$Password = 'password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList   $Username,$pass



powershell.exe  -command "Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred"

这提示我输入密码但是当我尝试像这里运行此命令时(没有powershell.exe):

Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred

没有提示就行了。你知道如何解决这个问题吗?我需要使用选项1,因为此命令是从TFS构建定义文件运行的,如下所示:

  <Exec Command="powershell.exe -command &quot;Invoke-Command -ComputerName $(Server) -scriptblock {path} -Credential $Cred&quot;" Condition="'$(RunTests)' == 'True'"/>

3 个答案:

答案 0 :(得分:0)

您可以尝试将命令传递给powershell.exe,如下所示:

'$Username = "us"; $Password = "password"; $pass = ConvertTo-SecureString -AsPlainText $Password -Force; $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList   $Username,$pass; Invoke-Command -ComputerName server.com -scriptblock {pathCopyAndUnzip.ps1} -Credential $Cred' | powershell.exe  -command -

答案 1 :(得分:0)

您可以将脚本放入其自己的文件中,然后从TFS调用它而不是内联代码。

C:\文件夹\ script.ps1:

Param(
    [string]$Username,
    [string]$Password,
    [string]$OtherParam,
)

$Password = $Password | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Password

Invoke-Command -ComputerName server.com -FilePath "C:\folder\CopyAndUnzip.ps1 -Something $OtherParam" -Credential $Cred

然后这样称呼它:

<Exec Command="powershell.exe -command "C:\folder\script.ps1 -username user10 -password P@55w0rd -OtherParam Whatever" Condition="'$(RunTests)' == 'True'"/>

答案 2 :(得分:0)

<Exec Command="$(PsExecPath) -accepteula \\$(Server) cmd /C  powershell -File FILEPATH " Condition="'$(RunTests)' == 'True'"/>

我使用旧的好的psExec :)现在一切正常。