Powershell|Powershell 远程连接 windows server

Windows PowerShell

  • Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用
    NET Framework的强大功能。它引入了许多非常有用的新概念,从而进一步扩展了您在 Windows 命令提示符
    和 Windows Script Host 环境中获得的知识和创建的脚本。
一、需求背景
  • 系统为windos server 因为服务器在国外,日志本身比较大(最大有3-5G多),不管是拉回本地还是在远程打开对网络和实例的负载都会比较大
  • 在本地局域网实现类似在Linux上 grep、tail、head等常用文本查询命令,可以节省大量网络带宽和负载以提高查询效率
  • 其中用到FRP内网穿透的工具来实现端口的转发和加速
二、拓扑图
Powershell|Powershell 远程连接 windows server
文章图片

三、服务部署
E节点 AWS LAN Server端设置
  1. 管理员身份打开 Windows PowerShell
Enable-PSRemoting -Force #启用 powershell 远程会话
  1. 配置 TrustedHosts 格式:`winrm set winrm/config/client '@{TrustedHosts=""}'
PS C:\Users\Administrator> winrm set winrm/config/client '@{TrustedHosts="192.168.1.20"}' Client NetworkDelayms = 5000 URLPrefix = wsman AllowUnencrypted = false Auth Basic = true Digest = true Kerberos = true Negotiate = true Certificate = true CredSSP = false DefaultPorts HTTP = 5985#5985为http默认连接端口 HTTPS = 5986#5986为https默认连接端口 TrustedHosts = 192.168.1.20#192.168.1.20为D节点实例IP 信任列表 多个地址用英文的逗号分隔 通配符 * 信任所有

  1. 配置好之后需要重启一下服务
    >Restart-Service WinRM

  2. 查看端口和信任列表
    >winrm get winrm/config/client

  3. shell系统的登录已完成,下面配置windows下使用shell命令
    网上搜索下载unix工具集UnxUtils C盘新建exe文件夹,根据自己的需求这里有很多命令可用
    因为考虑到安全只放入查询类的命令,并把exe目录加入到系统%PATH%环境变量中
Powershell|Powershell 远程连接 windows server
文章图片

  1. 新增相关系统帐号powersehll 并添加Remote management User Server2008添加Remote Desktop Users 开启Remote的权限
Powershell|Powershell 远程连接 windows server
文章图片

  1. 通过安全策略 拒绝用户写入权限
Powershell|Powershell 远程连接 windows server
文章图片

D节点 frpc内网端口转发 根据需求继续在下面添加#frpc工具使用这里不叙述,可参考我之前的帖子 vim ./frpcremote.ini [common] server_addr = 13.13.13.13#远程frps IP地址 server_port = 2222#远程frps 连接端口 token = test##123456#秘钥 [test1] type = tcp local_ip = 192.168.1.30#转发到 AWS LAN IP local_port = 5985#转发到 AWS LAN 端口 remote_port = 30030#远程转发过来的端口 use_encryption = true use_compression = true [test2] type = tcp local_ip = 192.168.1.40 local_port = 5985 remote_port = 30040 use_encryption = true use_compression = truenohup ./frpc -c frpcremote.ini >/dev/null 2>&1 &#开启frpc服务

C节点 frps公网暴露内网假设C节点公网IP为13.13.13.13 [common] bind_port = 2222#连接端口 vhost_http_port = 55005#web管理端口 dashboard_port = 55006 token = test##123456#秘钥 dashboard_user = frptest#登陆的帐号 dashboard_pwd =test##123456#登陆的密码 log_level = info log_file=/usr/logs/frp.lognohup ./frps -c frps.ini >/dev/null 2>&1 &#开启frps服务

B节点 本地局域网的端口转发到香港 13.13.13.13 实例 windows 端口转发常用命令 1、添加端口转发 netsh interface portproxy add v4tov4 listenport=30030 listenaddress=192.168.1.100 connectport=30030 connectaddress=13.13.13.13 netsh interface portproxy add v4tov4 listenport=30040 listenaddress=192.168.1.100 connectport=30040 connectaddress=13.13.13.13 2、删除端口转发 netsh interface portproxy del v4tov4 listenport=30040 listenaddress=192.168.1.100 3、查看已存在的端口映射 netsh interface portproxy show v4tov4 可以通过命令 netstat -ano|find 30040 查看端口是否已在监听 telnet 127.0.0.1 30040 测试端口是否连通

Powershell|Powershell 远程连接 windows server
文章图片

  • A节点 本地LAN Client使用
  • 首先在桌面 开始菜单找到powershell程序--右键以管理员身份运行
  • 输入 Set-ExecutionPolicy unrestricted 并跳出确定 输入 "Y"
    >Set-ExecutionPolicy unrestricted

Powershell|Powershell 远程连接 windows server
文章图片

  1. 启用 powershell 远程会话 "Enable-PSRemoting -Force"
4.配置信任主机:"winrm set winrm/config/client '@{TrustedHosts="92.168.1.100"}'" PS C:\Users\Administrator> winrm set winrm/config/client '@{TrustedHosts="92.168.1.100}' Client NetworkDelayms = 5000 URLPrefix = wsman AllowUnencrypted = false Auth Basic = true Digest = true Kerberos = true Negotiate = true Certificate = true CredSSP = false DefaultPorts HTTP = 5985 HTTPS = 5986 TrustedHosts =192.168.44.100登陆远程计算机 Enter-PSSession -ComputerName 192.168.1.100 -port 30030 -Credentialpowershell#跳出对话框输入帐号密码 (图片为测试截图)

Powershell|Powershell 远程连接 windows server
文章图片

登陆之后就可以和Linux一样 使用grep、tail、head等命令,powershell和Linux和shell一样
可以管理windos文件和服务,也带一些很强的命令,执行效率一般都会比 UnxUtils 命令集要高
但命令powershell一般都太长不容易记忆,如果写脚本建议使用自带的命令
四、自动化登陆脚本 tes1.sp1
$uname="test1" $pwd=ConvertTo-SecureString "test#123456" -AsPlainText -Force; $cred=New-Object System.Management.Automation.PSCredential($uname,$pwd); $pcname="192.168.1.20" $pcport=30030 Enter-PSSession -ComputerName $pcname -port $pcport -Credential $cred "====Remote login successful===="

五、疑难解答
1.除管理员administrator以外其它帐号无法登陆 可能是需要对powershell登陆进行授权 如何为非管理用户启用远程 Set-PSSessionConfiguration Microsoft.PowerShell -ShowSecurityDescriptorUI Performing operation "Set-PSSessionConfiguration" on Target "Name: Microsoft.PowerShell". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): A#同意思所有 安全考虑只给读和执行权限 之后会跳出对话框选择用户和设置用户权限2.报错提示会话没有创建 创建新的会话 系统默认用的标准(默认)会话配置是Microsoft.powershell 登录E节点实例 添加用户到配置 Set-PSSessionConfiguration Microsoft.PowerShell -ShowSecurityDescriptorUI查看配置项 Get-PSSessionConfiguration Get-PSSessionConfiguration -Name microsoft.powershell | Select *#查看指定microsoft.powershell会话如果Microsoft.powershell 不存在了那就创建新的会话 Register-PSSessionConfiguration -Name Microsoft.PowerShell也可创建其它会话配置 Register-PSSessionConfiguration -Name NewConfig

相关参考链接 【Powershell|Powershell 远程连接 windows server】https://docs.microsoft.com/en...
https://docs.microsoft.com/en...

    推荐阅读