当我们将凭据作为安全字符串传递时,无法连接到远程服务器,但当我们手动输入密码而不是转换为字符串时,可以登录。
我使用的脚本如下
param(
[string]$ServiceAccount = 'xyz/v-abc',
[string]$ServiceAccountPassword = '$Kxyz'
)
$pwdSecureString = $password | ConvertTo-SecureString -AsPlainText -force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ServiceAccount, $pwdSecureString
$appServer = 'xyz'
Invoke-Command -ComputerName $appServer -ScriptBlock { Get-service -Name 'WinRM'} -Credential $cred
#我面临的错误如下
[xyz] 连接到远程服务器 xyz 失败,并显示以下错误消息:用户 名称或密码不正确。有关详细信息,请参阅 about_Remote_Troubleshooting 帮助主题。 + 类别信息:OpenError:(xyz)[],PSRemotingTransportException + FullQualifiedErrorId :登录失败,PSSessionStateBroke
#当我使用以下命令时,我能够连接到服务器,即在脚本执行时输入密码,而不是将密码作为 $cred 传递
Invoke-Command -ComputerName $appServer -ScriptBlock { Get-service -Name 'WinRM'} -Credential xyz
您能否通过将密码作为安全字符串传递来帮助我们使用调用命令连接远程服务器
您找到这个问题的答案了吗?