powershell脚本添加远程防火墙规则

发布时间:2019-11-10编辑:脚本学堂
本文介绍了powershell脚本添加防火墙规则的方法,启用了远程桌面,然后使用powershell添加powershell远程防火墙规则,需要的朋友参考下。

将windows server 2012 core的默认控制台设置成了powershell,还启用了远程桌面,但是对于core版本的服务器来讲,远程桌面形同鸡肋,所以启用powershell远程访问,在服务器上以管理员权限运行:
Enable-PSRemoting -Force

在尝试建立远程连接时,提示访问被拒绝,此时可能是防火墙问题:
使用powershell添加powershell远程防火墙规则:
 

复制代码 代码示例:
New-NetFirewallRule -Name powershell-remote-tcp -Direction Inbound -DisplayName 'PowerShell远程连接 TCP' -LocalPort 5985-5996 -Protocol 'TCP'
New-NetFirewallRule -Name powershell-remote-udp -Direction Inbound -DisplayName 'PowerShell远程连接 UDP' -LocalPort 5985-5996 -Protocol 'UDP'

再次尝试建立连接:
 

复制代码 代码示例:
PS> Enter-PSSession -ComputerName 192.168.1.15 -Credential administrator
[192.168.1.15]: PS C:UsersAdministratorDocuments> hostname
AD-2012