【问题标题】:Invoke-Command with -credentials使用 -credentials 调用命令
【发布时间】:2017-07-14 20:21:57
【问题描述】:

我想在远程服务器上调用命令,我不想输入密码来运行脚本。我尝试加密密码并将其存储在 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 上运行此命令,而不必每次都输入密码。

【问题讨论】:

  • 删除第 3 行中 - 和 argumentlist 之间的空格
  • 当我这样做时,它不再读取运行脚本的用户的信用。

标签: powershell powershell-2.0 powershell-3.0


【解决方案1】:

您只需指定-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

【讨论】:

  • 我试过这是我添加 -Credential New-Object 时遇到的错误:找不到“PSCredential”的重载和参数计数:“2”。在 \\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
【解决方案2】:

4 年后,但这里是:

Frode F. 在 cmets 中的想法是正确的。你的第三行有- 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 这样的连字符来模拟这一点并亲自查看,所以您的语法是错误的somewhere

密码格式不正确

Cannot find an overload for "PSCredential" and the argument count: "2".

这意味着$Password 的格式不正确。您正在导入一个名为 password-encrypted.txt 的文件,但在此之后您将其传递给 ConvertTo-SecureString。您确定password-encrypted.txt 文件中的信息在您保存之前传递给ConvertTo-SecureStringConvertFrom-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 运行此处提供的代码,就像其他人建议的那样,它应该可以正常运行。您很可能没有运行您在此处提供的代码,因为通过这两项调整,代码运行了。

【讨论】:

    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-30
    • 2014-04-11
    • 2022-01-14
    相关资源
    最近更新 更多