PowerShell Remote Sessions and Invoke-Command

    PowerShell Remote Sessions:

    PowerShell Remote Sessions and Invoke-Command: PowerShell Remoting lets you run PowerShell commands or access full PowerShell sessions on remote Windows systems. It’s similar to SSH for accessing remote terminals on other operating systems.

    Enable PowerShell Remoting

    Enable PowerShell Remoting on the PC You Want to Access Remotely
    Your first step is to enable PowerShell Remoting on the PC to which you want to make remote connections. There are several methods to do this.

    1. On that PC, you’ll need to open PowerShell with administrative privileges.
      Enable-PSRemoting -Force

      PS C:\> Enable-PSRemoting -Force
      WinRM has been updated to receive requests.
      WinRM service started.
       
      WinRM has been updated for remote management.
      Enabled remote access.​
    2. winrm quickconfig

      PS C:\> winrm quickconfig
      WinRM is not set up to receive requests on this machine.
      The following changes must be made:
       
      Start the WinRM service.
       
      Make these changes [y/n]? y
       
      WinRM has been updated to receive requests.
       
      WinRM service started.
      WinRM is already set up for remote management on this computer.
      PS C:\>​
    3. Using server manager – GUI

    Configure TrustedHosts:

    Next, you need to configure the TrustedHosts setting on both the PC to which you want to connect and the PC (or PCs) you want to connect from, so the computers will trust each other. You can do this in one of two ways.

    First check if there are any existing trustedhosts

    PS C:\> get-Item wsman:\localhost\client\trustedhosts
     
     
       WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client
     
    Type            Name                           SourceOfValue   Value
    ----            ----                           -------------   -----
    System.String   TrustedHosts

    There are no Trustedhosts defined here. If the target server is in the same domain, there is no need to cinfigure trustedhosts. It will be trusted automatically.

    To add trusted hosts, use the command Set-Item wsman:\localhost\client\trustedhosts *

    PS C:\> Set-Item wsman:\localhost\client\trustedhosts *
     
    WinRM Security Configuration.
    This command modifies the TrustedHosts list for the WinRM client. The computers in the TrustedHosts list might not
    authenticated. The client might send credential information to these computers. Are you sure that you want to modif
    this list?
    [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
    PS C:\> get-Item wsman:\localhost\client\trustedhosts
     
     
       WSManConfig: Microsoft.WSMan.Management\WSMan::localhost\Client
     
    Type            Name                           SourceOfValue   Value
    ----            ----                           -------------   -----
    System.String   TrustedHosts                                   *
     
     
    PS C:\>

    Now, once the configuration is completed successfully, restart WinRM service.

    Restart-Service WinRM

    Now you should be able to connect to the remote server.

    PS C:\> Enter-PSSession -ComputerName vc01
    [vc01]: PS C:\Users\wintel\Documents> hostname
    vc01
    [vc01]: PS C:\Users\wintel\Documents>

    If you anticipate making multiple connections to a remote system, use the New-PSSession cmdlet to create a remote Windows PowerShell session. New-PSSession permits you to store the remote session in a variable, and it provides you with the ability to enter and leave the remote session as often as required, without the additional overhead of creating and destroying remote sessions.

    PS C:\> $s = New-PSSession -ComputerName vc01
    PS C:\> Invoke-Command -Session $s -ScriptBlock {hostname}
    vc01
    PS C:\> Invoke-Command -Session $s -ScriptBlock {hostname; Get-WmiObject win32_computersystem;}
    vc01
     
     
    Domain              : winadmin.org
    Manufacturer        : VMware, Inc.
    Model               : VMware Virtual Platform
    Name                : VC01
    PrimaryOwnerName    : Windows User
    TotalPhysicalMemory : 8589328384
    PSComputerName      : vc01
     
     
     
    PS C:\>

    If the server is a non-domain server or other domain server, you should specify the Credentials.

    PS C:\> $cred = Get-Credential
     
    cmdlet Get-Credential at command pipeline position 1
    Supply values for the following parameters:
    Credential
    PS C:\> $s = New-PSSession -ComputerName vc01 -Credential $cred
    PS C:\> Invoke-Command -Session $s -ScriptBlock {hostname; Get-WmiObject win32_computersystem;}
    vc01
     
     
    Domain              : winadmin.org
    Manufacturer        : VMware, Inc.
    Model               : VMware Virtual Platform
    Name                : VC01
    PrimaryOwnerName    : Windows User
    TotalPhysicalMemory : 8589328384
    PSComputerName      : vc01
     
     
     
    PS C:\>

     


    © 2019 WinAdmin.org . All Rights Reserved.
    Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
    Ok Decline