如何获取 运行 的远程 PowerShell 脚本?启用 PSRemoting 不工作
How do I get remote PowerShell scripts to run? Enable-PSRemoting isn't working
我们都在同一个域中,我在两台机器上都启用了 PSRemoting,但由于某些原因,我的远程脚本不会 运行。执行策略不受限制,但这是我收到的消息:
Get-Process:无法连接到远程机器。
在 line:1 char:1
+ 获取进程 -ComputerName 10.xxx.xx.xx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-Process], InvalidOperationException
+ FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand
Get-EventLog:未找到网络路径。
在 line:1 char:1
+ Get-EventLog -LogName Application -ComputerName 10.xxx.xx.xx
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-EventLog], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.GetEventLogCommand
我运行在两台机器上都安装了 PSVersion 5.1 和 PSRemotingProtocolVersion 2.3,有人有什么建议吗?
经过更多研究后,我启用了远程服务器管理 (WinRM) 并允许在两台机器上进行远程 PowerShell 访问,对于上面的那些基本 cmdlet,它仍然给我相同的错误消息,但它会让我进入一个会话在远程机器上使用 Enter-PSSession
。我在这里不知所措,因为解决方法不错,但理想情况下我想远程 运行 脚本。
如果您可以使用 Enter-PSSession
,那么 PSRemoting 就可以正常工作。但是,Get-Process
和 Get-EventLog
首先都没有使用 PSRemoting 进行远程连接。您可以使用 Wireshark or WinDump 等工具检查网络流量来检查这一点。您需要访问远程主机上的端口 139/tcp(NetBIOS 会话)和 445/tcp (DirectSMB),cmdlet 才能工作。这些端口可能在远程主机上的 Windows 防火墙中被阻止,因此您需要更改它以启用到该主机的远程连接。
如果您想使用 PSRemoting,您需要通过 Invoke-Command
:
在远程主机上本地调用 cmdlet
Invoke-Command -Computer 10.x.x.x -ScriptBlock {
Get-Process
Get-EventLog -LogName Application
}
我们都在同一个域中,我在两台机器上都启用了 PSRemoting,但由于某些原因,我的远程脚本不会 运行。执行策略不受限制,但这是我收到的消息:
Get-Process:无法连接到远程机器。 在 line:1 char:1 + 获取进程 -ComputerName 10.xxx.xx.xx + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-Process], InvalidOperationException + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.PowerShell.Commands.GetProcessCommand Get-EventLog:未找到网络路径。 在 line:1 char:1 + Get-EventLog -LogName Application -ComputerName 10.xxx.xx.xx + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-EventLog], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.GetEventLogCommand
我运行在两台机器上都安装了 PSVersion 5.1 和 PSRemotingProtocolVersion 2.3,有人有什么建议吗?
经过更多研究后,我启用了远程服务器管理 (WinRM) 并允许在两台机器上进行远程 PowerShell 访问,对于上面的那些基本 cmdlet,它仍然给我相同的错误消息,但它会让我进入一个会话在远程机器上使用 Enter-PSSession
。我在这里不知所措,因为解决方法不错,但理想情况下我想远程 运行 脚本。
如果您可以使用 Enter-PSSession
,那么 PSRemoting 就可以正常工作。但是,Get-Process
和 Get-EventLog
首先都没有使用 PSRemoting 进行远程连接。您可以使用 Wireshark or WinDump 等工具检查网络流量来检查这一点。您需要访问远程主机上的端口 139/tcp(NetBIOS 会话)和 445/tcp (DirectSMB),cmdlet 才能工作。这些端口可能在远程主机上的 Windows 防火墙中被阻止,因此您需要更改它以启用到该主机的远程连接。
如果您想使用 PSRemoting,您需要通过 Invoke-Command
:
Invoke-Command -Computer 10.x.x.x -ScriptBlock {
Get-Process
Get-EventLog -LogName Application
}