我无法通过以下脚本连接到域控制器。此脚本适用于另一个域控制器,因此这意味着脚本是正确的。连接时出现错误:
enter-pssession : Connecting to remote server "COMPUTER Name" failed with the following error message : The user name or
password is incorrect. For more information, see the about_Remote_Troubleshooting Help topic.
我 100%确定我的凭据是正确的,如果我手动输入,我可以连接(PSsession)到域。
脚本 -
$password = ConvertTo-SecureString "<MyPassword>" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential ("<Domain\UserName>", $password)
Enter-PSSession -ComputerName <IPAddress> -Port <PortNumber> -Credential $cred
WinRM 服务正在运行。Set-Item WSMan:\localhost\Client\TrustedHosts *
也已应用。
答案1
我遇到了同样的情况并帮助了我[https://migration-tool.sharegate.com/hc/en-us/articles/115000640868-Hide-credentials-in-scripts][1]
我使用从中获取的长字符串创建了密码
$credentials = Get-Credential
ConvertFrom-SecureString $credentials.Password
然后我使用以下方法将其转换为凭证
$secureString = "01000000d08c9ddf0115d1118c7a00c04fc297eb01000000c57ba489a6781343983ef0deba79d0250000000002000000000003660000c0000000100000007ba4ea450748178832c0187a37c513270000000004800000a000000010000000a3fe44ced5ac805a2fcf7a6c3652994510000000ccc7701593bd519546d09f216962fac4140000003904619dc8c320b7fb0cafa6efdd78dd2c69c8e5"
$password = ConvertTo-SecureString $secureString
$UserName = [email protected]
我像这样将其传递给 Invoke-Command
$Credentials = New-Object System.Management.Automation.PSCredential `
-ArgumentList $UserName, $password
Invoke-Command -Session $session -ScriptBlock {whoami} -ErrorAction Stop