简体   繁体   English

执行powershell远程问题

[英]Executing powershell remotely issue

Hi when i try to do some code: 嗨,当我尝试做一些代码:

$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"

This prompt me for a password but when i try to run this command like here (without powershell.exe): 这提示我输入密码但是当我尝试像这里运行此命令时(没有powershell.exe):

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

it works without prompt. 它工作没有提示。 Do you know how to resolve that? 你知道如何解决这个问题吗? I need to use option 1 because this command is runned from TFS build definition file like here: 我需要使用选项1,因为此命令是从TFS构建定义文件运行的,如下所示:

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

您可以尝试将命令传递给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 -

You could put your script into it's own file and then call that from TFS rather inline code. 您可以将脚本放入其自己的文件中,然后从TFS调用它而不是内联代码。

C:\\folder\\script.ps1: 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

Then call it like so: 然后像这样调用它:

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

我用旧的好psExec :)现在一切都工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM