如何使远程计算机在远程计算机本身上运行PowerShell脚本?


4

我有两台远程计算机:1我们将其称为驱动程序,2我们将其称为客户端。它们在同一个域中,我们称它们为“ TECH.com”。

在驱动程序计算机上,我有一个控制客户端计算机的PowerShell脚本:1,还原客户端上的检查点。2,停止客户端。3,启动客户端。等。我正在尝试做的是使客户端计算机执行另一个PowerShell脚本(可以驻留在客户端或驱动程序和/或驱动程序上。可以在必要时将文件从驱动程序复制到客户端)。客户端计算机。

因此我做了一些研究,发现了两种方法:1,WS管理。2,WMI我在两台机器上都启用了psremoting。我已经在两台机器上测试了WsMan,并且它们都工作正常,但是我已经能够在这两台机器之间传输文件。但是,当我尝试运行Invoke-command时,它给了我错误:

Connecting to remote server XXXXXXtech.com 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.
    + CategoryInfo          : OpenError: (XXXXXX.XXXXtech.com:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : AuthenticationFailed,PSSessionStateBroken

Answers:


5

有两个选择:


在本地计算机上,允许未经身份验证连接到远程计算机:

Set-Item WSMan:\localhost\Client\TrustedHosts -Value $remoteMachine -Force

如果您在同一域中,并且在远程计算机上具有管理员权限,请继续尝试使用用户名输入远程会话。


否则,您可能需要/想要设置一个凭据对象,并使用它来启动的会话Invoke-Command

$script = { Write-Host "Hello, World!" }
$computerName = "Server Name Or Ip Address"
$username = "domain\user"
$pw = "worstpasswordever"

# Create Credentials
$securepw = ConvertTo-SecureString $pw -asplaintext -force
$cred = new-object -typename System.Management.Automation.PSCredential -argument $username, $securepw

# Create and use session
$session = New-PSSession -credential $cred -ComputerName $computerName
Invoke-Command -Session $session -ScriptBlock $script
Remove-PSSession $session

我已经使用过set-item命令。然后,我将尝试创建凭据对象。我使用$ clientMachineName \ Admin作为凭据
FrozenLand 2013年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.