zoukankan      html  css  js  c++  java
  • PowerShell连接远程主机运行远程命令

    PowerShell主要采用Web Services for Mangement(WS-MAN)进行远程处理,WS-MAN完全基于Http(默认5985)或者Https(默认5986)进行工作,这样保证在需要的情况下,能够轻易透过防火墙进行作业(因为每种协议都使用唯一的端口进行通信)。微软对WS-MAN的实现是Windows Remote mangement(WinRM)。WinRM是一个基于SOAP的后台服务。

    PowerShell连接常规远程主机

    1. 在远程机器上以Administrator角色打开PowerShell执行以下命令,启动允许远程连接

    Enable-PsRemoting

    2. 在客户机器 (本地) 执行以下命令,将远程机器IP地址加入可信主机列表

    Set-Item wsman:localhostClientTrustedHosts -value <Remote Host IP Address>

    3. 在客户机器 (本地) 输入以下命令, 在弹出对话框输入密码,以交互的方式连接远程主机

    Enter-PSSession -ComputerName <Remote Host IP Address> -Credential <UserName>

    4. PowerShell远程命令的基本使用见官方文档: Running Remote Commands

    • Start an Interactive Session (Enter-PSSession and Exit-PSSession)
    • Run a Remote Command / Script (Invoke-Command)
    • Establish a Persistent Connection (New-PSSession)

    使用PowerShellHttp方式连接Azure虚拟机

    1. 同以上常规连接远程主机设置

    1. 若虚拟机安全受网络完全组(Network Security Group)管控,设置允许5985入栈

    2. 若虚拟机绑定了负载均衡器(load balancer),为5985端口设置入站NAT规则(Inbound NAT Rules), 或者为了隐藏实际端口,为5985端口运行状况探测(probes)和负载均衡规则(load Balancing Rules)

     使用PowerShell以Https方式连接Azure虚拟机

    1. 同以上常规连接远程主机设置

    2. 打开Azure远程虚拟机(Windows)防火墙设置,设置允许5986入栈

    3. 若虚拟机安全受网络完全组(Network Security Group)管控,设置允许5986入栈

    4. 若虚拟机绑定了负载均衡器(load balancer),为5986端口设置入站NAT规则(Inbound NAT Rules),或者为了隐藏实际端口,为5986端口运行状况探测(probes)和负载均衡规则(load Balancing Rules)

    5. 需使用以下命令连接远程主机, 详细参数设置见: Enter-PSSession

    # 设置会话,忽略远程机器的SSL证书验证
    $SessionOption
    = New-PsSessionOption SkipCACheck -SkipCNCheck Enter-PSSession -ConnectionUri https://<Remote Host IP Address>:5986 -Credential <User Name> -SessionOption $SessionOption # 另一种参数形式 Enter-PSSession -ComputerName <Remote Host IP Address> -Port 5986 -UseSSL -Credential <User Name> -SessionOption $SessionOption

    从PowerShell 6开始,除了WS-MAN之外,远程处理技术还可以基于SSH协议。在最新的Windows 10和Windows Server 2019中,可以使用OpenSSH连接远程机器,详细文档参见:PowerShell remoting over SSH and OpenSSH in Windows, 另外,OpenSSH客户端在最新的Windows 10和Windows Server 2019中已默认安装,在 设置应用可选功能 列表下可以找到(见下图), 可以通过上方的添加功能按钮安装OpenSSH服务器。

     

    更多参考:

    1. Enable PowerShell remoting on Azure RM virtual machines

    2. Copy files to Azure VM using PowerShell Remoting

    3. Secrets of PowerShell Remoting

    3. 远程管理WinRM,Enter-PSSession (Enable-PSRemoting = Set-WSManQuickConfig = winrm quickconfig)

    4. PowerShell 远程执行任务

    5. Windows开启WinRM服务 

  • 相关阅读:
    配置apache的文件访问路径
    php 常量const
    php接口interface的使用
    php 抽象类abstract
    php 面向对象三大特点:封装、继承、多态
    程序员的情怀《从前慢》木心
    php static静态属性和静态方法
    php面向对象的构造方法与析构方法
    关于php变量的赋值和引用的区别
    angular4.0微信oAuth第三方认证的正确方式
  • 原文地址:https://www.cnblogs.com/makesense/p/12820404.html
Copyright © 2011-2022 走看看