Remote Sessions and Invoke-Command

    PowerShell Remote Sessions:

    PowerShell remoting allows you to execute PowerShell commands or scripts on remote computers. It is built on top of the Windows Remote Management (WinRM) service, which is Microsoft's implementation of the WS-Management protocol. To use PowerShell remoting, you need to have administrator privileges on the local and remote computers.

    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.

    On that PC, you’ll need to open PowerShell with administrative privileges.

    Enable-PSRemoting -Force
     
    This command enables the WinRM service, sets up a listener for incoming connections, and configures the necessary firewall rules.
     
     
    winrm quickconfig

     

     
    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.

     
    get-Item wsman:\localhost\client\trustedhosts # Tto list trusted hosts
    Set-Item wsman:\localhost\client\trustedhosts *  # To set trusted hosts
    Restart-Service WinRM
     
    Now you should be able to connect to the remote server. Test the connection:

    From the local computer, open PowerShell and use the Test-WSMan command to check if the remote computer is configured for PowerShell remoting:

    Test-WSMan -ComputerName "RemoteComputerName"
    

    Establish a remote connection:

    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
     
    If the server is a non-domain server or other domain server, you should specify the Credentials.
    PS C:\> $cred = Get-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

    Exit the remote session:

    To exit the remote session and return to your local PowerShell session, use the Exit-PSSession command:

    Exit-PSSession