Enable WinRM Remotely

# Set variables

# Enable WinRM Remotely
psexec.exe \\$remoteHost -s C:\Windows\system32\winrm.cmd qc -quiet
# Invoke-WmiMethod -ComputerName $server -Class Win32_Process -Name Create -ArgumentList "C:\Windows\system32\winrm.cmd -q"

# Test to see if WinRM is indeed installed
test-netconnection $remoteHost -port 5985

# Enter PowerShell session on remote host
enter-pssession $remotehost

function enableRemoteWinRM{

  Write-Host "checking $computername..."

  function pingTest{
        Return Test-Connection $node -Count 1 -Quiet -ea Stop;
      catch{Return $False}

  if (pingTest $computername){
      if (!(Test-WSMan $computername -ea SilentlyContinue)){
        if(!(get-command psexec)){
            if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {
                Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
            choco install sysinternals -y
        psexec.exe \\$computername -s C:\Windows\system32\winrm.cmd qc -quiet
        }else{Write-Host "WinRM has been already enabled. No changes to WinRM have been made."}
  Else{Write-Host "Unable to determine if WinRM is enabled on $computername`.`n Ping test has failed. Check if this computer is online and whether there's a firewall blocking of ICMP";}

Leave a Reply

Your email address will not be published. Required fields are marked *