使用-credentials调用Command

时间:2017-02-24 05:12:15

标签: powershell powershell-v2.0 powershell-v3.0

我想在远程服务器上调用命令,我不想输入密码来运行脚本。我已经尝试加密密码并将其存储在txt文件中。

 $username = "Mydomain\service.account"
 $password = cat C:\username-password-encrypted.txt | convertto-securestring
 $cred = new-object -typename System.Management.Automation.PSCredential - argumentlist $username, $password
 Invoke-command $cred -Computer myserver -scriptblock {param([string]$LocalUser); Add-PSSnapin Citrix* ; Get-BrokerSession -max 10000 | Where-Object brokeringusername -eq  "mydomain\$($LocalUser)" | Stop-BrokerSession} -ArgumentList $user

这是我得到的错误

 Invoke-Command : A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.
 At \\uncpath\citrix\Installation Media\Citrix\Ticketing_script\Ticketing_Script - Copy (3).ps1:70 char:1
+ Invoke-command $cred -Computer MyServer -scriptblock {param([s ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Invoke-Command],   ParameterBindingException
+ FullyQualifiedErrorId :   PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

必须有一种更简单的方法在myserver上运行此命令,而不必每次都输入密码。

1 个答案:

答案 0 :(得分:0)

您只需指定-Credential参数:

 Invoke-command -Credential $cred -Computer myserver -scriptblock {param([string]$LocalUser); Add-PSSnapin Citrix* ; Get-BrokerSession -max 10000 | Where-Object brokeringusername -eq  "mydomain\$($LocalUser)" | Stop-BrokerSession} -ArgumentList $user
相关问题