我想在远程服务器上调用一个命令,我不想必须输入密码才能运行脚本。我已经尝试加密密码并将其存储在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上运行此命令,而不必每次都输入密码。
发布于 2017-02-24 13:44:17
您只需指定-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发布于 2021-10-29 00:38:13
4年后,但这是这样的:
Frode F.在评论中提出了正确的想法。第三行包含- argumentlist,但它必须为-ArgumentList,否则将抛出New-Object : A positional parameter cannot be found that accepts argument 'ArgumentList'.
此外,当我复制并运行您的代码时,它不会抛出相同的错误,这会使我相信您正在运行的代码与您在此处编写的代码不同。以下是您收到的不同错误及其原因:
语法不正确的
A positional parameter cannot be found that accepts argument 'System.Management.Automation.PSCredential'.这意味着您没有正确指定标志,Powershell会认为您使用的是一个名为System.Management.Automation.PSCredential的标志或参数。您可以通过添加像New-Object -TypeName - System.Management.Automation.PSCredential这样的连字符来模拟这一点,并亲自查看,因此您的语法在某些地方是错误的。
密码格式不正确
Cannot find an overload for "PSCredential" and the argument count: "2".这意味着$Password的格式不正确。您正在导入一个名为password-encrypted.txt的文件,然后将其传递给ConvertTo-SecureString。是否确定password-encrypted.txt文件中的信息在保存之前先传递给ConvertTo-SecureString,然后传递给ConvertFrom-SecureString,如下所示?
"MyPassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "C:\username-password-encrypted.txt"有关如何在Powershell中处理密码的更多信息,请参阅这篇文章,其中详细介绍了如何在Powershell中处理凭据:
https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/
结论中的
如果像其他人建议的那样,同时使用-ArgumentList (而不是- argumentlist)和-Credentials $creds运行这里提供的代码,那么它应该运行得很好。您很可能没有运行这里提供的代码,因为通过这两个调整,代码就会运行。
https://stackoverflow.com/questions/42431237
复制相似问题