2017-02-24 50 views
1

我想調用遠程服務器上的命令,我不想輸入密碼來運行腳本。我試着加密密碼並將其存儲在txt文件中。帶-credentials的調用命令

$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運行此命令更簡單的方法,而不必每次都放在密碼。

+0

刪除之間的空間 - 和參數列表中的第3行 –

+0

當我這樣做,它不再運行腳本的用戶多數民衆贊成在vim的信任狀讀取。 – user770022

回答

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 
+0

我試過了 以下是我添加-Credential時得到的錯誤 新對象:無法找到「PSCredential」的超載和參數計數:「2」。 At \\ MyDomain \ citrix \ Installation Media \ Citrix \ Ticketing_script \ Ticketing_Script.ps1:110 char:9 + $ cred = New-Object System.Management.Automation.PSCredential($ userna ... + ~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~ + CategoryInfo:InvalidOperation:(:) [New-Object],MethodException + FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand – user770022