如何在 powershell 命令中传递凭据以及用户名和密码

如何在 powershell 命令中传递凭据以及用户名和密码

我正在尝试使用 powershell 进入远程会话。我创建了以下命令,但它要求输入密码才能手动输入,然后才能连接。我一直在寻找没有密码提示的方法

Enter-PSSession <IPaddress> -Credential Administrator

我已经运行了以下操作:

$username = "Administrator"
$password = ConvertTo-SecureString "******" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList($username, $password)
$serverNameOrIp = "*****"

Enter-PSSession -ComputerName $serverNameOrIp -Credential $cred

以下是错误:

Enter-PSSession : Connecting to remote server ****** failed with the following error message : WinRM cannot
process the request. The following error with errorcode 0x80090311 occurred while using Kerberos authentication: There
are currently no logon servers available to service the logon request.
 Possible causes are:
  -The user name or password specified are invalid.
  -Kerberos is used when no authentication method and no user name are specified.
  -Kerberos accepts domain user names, but not local user names.
  -The Service Principal Name (SPN) for the remote computer name and port does not exist.
  -The client and remote computers are in different domains and there is no trust between the two domains.
 After checking for the above issues, try the following:
  -Check the Event Viewer for events related to authentication.
  -Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or
use HTTPS transport.
 Note that computers in the TrustedHosts list might not be authenticated.
   -For more information about WinRM configuration, run the following command: winrm help config. For more
information, see the about_Remote_Troubleshooting Help topic.
At C:\Users\Administrator\Downloads\cred.ps1:6 char:1
+ Enter-PSSession -ComputerName $serverNameOrIp -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (******:String) [Enter-PSSession], PSRemotingTransportException
    + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

请注意远程服务器位于受信任的主机列表中

答案1

您可以通过以下方式创建凭证对象:

$passwd = convertto-securestring -AsPlainText -Force -String <your password>

$cred = new-object -typename System.Management.Automation.PSCredential 
-argumentlist "Domain\User",$passwd

然后您可以将$cred对象作为参数的值传递-Credential ,如下所示:-Credential $cred

您可能会在帖子中找到更多可根据需要调整的选项
Windows PowerShell 在命令 Enter-PSSession 中输入密码

相关内容