我在 Windows Server 2012 下安装了 Power Shell 4.0 版本。现在我运行以下 powershell 命令:-
$admin = "domain\user"
$pwd = "*****" | ConvertTo-SecureString -asPlainText -Force
$LiveCred = New-Object System.Management-Automation.PSCredential($admin, $pwd)
Invoke-Command -ComputerName **** -Credential $LiveCred {Get-VM}
但我得到了这个错误:-
New-Object : Cannot find type [System.Management-Automation.PSCredential]: verify that the assembly containing this
type is loaded.
因此我更新了我的 powershell 脚本如下:-
add-pssnapin System.Management-Automation;
$admin = "domain\user"
$pwd = "*****" | ConvertTo-SecureString -asPlainText -Force
$LiveCred = New-Object System.Management-Automation.PSCredential($admin, $pwd)
Invoke-Command -ComputerName **** -Credential $LiveCred {Get-VM}
但我却得到了这个错误:-
add-pssnapin : The Windows PowerShell snap-in 'System.Management-Automation' is not installed on this computer.
At line:1 char:1
+ add-pssnapin System.Management-Automation;
那么有人能建议是什么导致了上述两个错误吗?
第二个问题有没有更直接的方法来传递密码Invoke-Command
?现在我可以按如下方式传递用户名:-
Invoke-Command -ComputerName **** -Credential domain\username {Get-VM}
但不确定我是否可以用同样的方式传递密码?
谢谢
答案1
您在应该用句号的地方用了连字符。没有类System.Management-Automation
,而是System.Management.Automation
:
$LiveCred = New-Object System.Management.Automation.PSCredential($admin, $pwd)