Documents for Enabling the PS Remoting

These are the below command to enable the PS remoting and after you can cross check by executing the invoke-command utility to cross check the winrm connectivity from source to target.

  1. enable-psremoting -f
  2. winrm quickconfig
  3. set-executionpolicyremotesigned (must login as administrator)
  4. enable-wsmancredssp server ((must login as administrator))
  5. set winrm/config/service/Auth @{Basic="true"}
  6. set winrm/config/service @{AllowUnencrypted="true"}
  7. set winrm/config/winrs @{MaxMemoryPerShellMB="1024"}
  8. give exception to windows firewall to winrm or stop the windows firewall service
  1. Below is the command to cross check whether you are able to execute the command remotely

$username = "domain\username";

$password = ConvertTo-SecureString "password" -AsPlainText -Force;

$cred = new-object -typenameSystem.Management.Automation.PSCredential -argumentlist $username, $password;

Invoke-command -computer 172.20.17.26 -credential $cred –Authentication CredSSP -ScriptBlock{HOstname}

  1. You can pass multiple command in script block by separating the command by semi colon ";" for example

Invoke-command -computer 172.20.17.26 -credential $cred –Authentication CredSSP -ScriptBlock{Hostname;Get-PsDrive;}

  1. you can also save a command in a local variable, then use Invoke-Command to run the command against several remote computers:

PS C:\> $cmd = { get-eventlog -log "windows powershell" | where {$_.message -like "*certificate*"} }

PS C:\> invoke-command -computername DC1, DC2, DC3 -scriptblock $cmd

12. Remote server management through WinRM

First, open your Group Policy Editor (I’m using a Windows Server 2012 R2 domain controller) and navigate to the following path:

Computer Configuration\Policies\Administrative Templates\Windows Components\Windows Remote Management (WinRM)\WinRM Service

As you can see in the following screenshot, the policy that we enable is called Allow remote server management through WinRM, and we should both enable the policy and set the IPv4/IPv6 filters to all (*).

  1. Configure Windows Firewall

Third, and finally, we need to set Windows Firewall rules by navigating to:

Computer Configuration\Policies\Windows Settings\Security Settings\Windows Firewall with Advanced Security\Windows Firewall with Advanced Security

  1. Set WinRM service to automatic startup

Second, we need to ensure that the WinRM service is set for automatic startup. Navigate to:

Computer Configuration\Policies\Windows Settings\Security Settings\System Services

Select theWindows Remote Management (WS-Management)service and set it for automatic startup.

  1. Enabling PowerShell remoting in a workgroup

When your computers exist outside of an Active Directory domain, PowerShell remoting is certainly possible, but it is quite a bit more tedious to set up. One approach involves the use of digital certificates; the other, which we’ll use here, implements the TrustedHosts list.

The TrustedHosts list records the hostnames of any other systems that you want to grant remote access permissions to the local machine. First, you should verify that your workgroup computer’s TrustedHosts list is empty by running the following command:

Get-Item –Path WSMan:\localhost\Client\TrustedHosts

To grant another computer permission to establish a PowerShell remoting session with the localhost, run the following:

Set-Item –Path WSMan\localhost\Client\TrustedHosts –Value "computername"

  1. A few things can go wrong here. If any of your NICs have a location set to “Public,” you’ll get a failure. One way around this problem is to set the problematic NIC to use the Private location profile; this shouldn’t be a problem assuming that you are secure inside your network perimeter.

Set-NetConnectionProfile –InterfaceIndexnic_index> -NetworkCategory Private

Few Example for testing

$s = New-PSSession -ComputerName "DC1"

Invoke-Command -Session $s -ScriptBlock {$services = Get-Service}

Invoke-Command -Session $s -ScriptBlock {$services | Where-Object {$_.Status -eq "Stopped"}}

Remove-PSSession $s

------END OF THE DOCUMENT------