Enable-PSRemotingRemote.psm1

<#
.SYNOPSIS
Enable PSRemoting on a remote computer.
 
.DESCRIPTION
This module will run the cmdlet "Enable-PSRemoting" on the remote computer using a third party tool (PsExec).
 
Is very useful when you can't (or not want) connect to the remote computer by RDP, TeamViever, etc... to run the cmdlet "Enable-PSRemoting".
 
In fact, it will run Enable-PSRemoting on the remote computer such as you would type the cmdlet right there.
 
VERY IMPORTANT:
Running this module you are accepting the Sysinternals EULA.
You must have PsExec.exe in $env:SystemRoot\PsExec.exe
See the links to download and get it.
 
.INPUTS
None. You cannot pipe objects. No accept objects, only computernames as string.
 
.OUTPUTS
Will show you the command result.
 
.NOTES
Developed by: Victor Marttinez
E-mail: victormartinezsv@hotmail.com
 
.LINK
www.sysinternals.com
https://docs.microsoft.com/en-us/sysinternals/downloads/psexec
 
.EXAMPLE
 
C:\PS> Enable-PSRemotingRemote -ComputerName workingcomputer
Trying to enable PSRemoting with PsExec on workingcomputer...
PsExec.exe :
En C:\PS> Enable-PSRemotingRemote: 89 Carácter: 6
+ PsExec.exe \\$ComputerName -h powershell.exe "Enable-PSRemoting ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
  
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
WinRM is already set up to receive requests on this computer.
WinRM has been updated for remote management.
WinRM firewall exception enabled.
 
Connecting to workingcomputer...Starting PSEXESVC service on workingcomputer...Connecting with PsExec service on workingcomputer...Starting powershell.exe on workingcomputer...
powershell.exe exited on alicia with error code 0.
 
Seems all went fine, it should be working now!
 
C:\PS>
 
Successfully enable PSRemoting on the remote computer.
 
.EXAMPLE
 
PsExec.exe :
C:\PS> Enable-PSRemotingRemote -ComputerName shouldworkcomputer
Trying to enable PSRemoting with PsExec on shouldworkcomputer...
PsExec.exe :
En C:\PS> Enable-PSRemotingRemote: 89 Carácter: 6
+ PsExec.exe \\$ComputerName -h powershell.exe "Enable-PSRemoting ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
  
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
Set-WSManQuickConfig : <f:WSManFault
WinRM is already set up to receive requests on this computer.
xmlns:f="http://schemas.microsoft.com/wbem/wsman/1/wsmanfault" Code="2150859113"
Machine="localhost"><f:Message><f:ProviderFault provider="Config provider"
Connecting to shouldworkcomputer...Starting PSEXESVC service on shouldworkcomputer...Connecting with PsExec service on shouldworkcomputer...Starting powershell.exe on shouldworkcomputer...
powershell.exe exited on shouldworkcomputer with error code 1.
 
ADVERTENCIA: Seems not all went fine, but it could work. Try it!
 
C:\PS>
 
Enable-PSRemoting cmdlet couldn't apply some of the changes needed, but doesn't mean that PSRemoting not work. Try it!
 
.EXAMPLE
 
C:\PS> Enable-PSRemotingRemote -ComputerName notworkingcomputer
Trying to enable PSRemoting with PsExec on notworkingcomputer...
PsExec.exe :
En C:\PS> Enable-PSRemotingRemote: 89 Carácter: 6
+ PsExec.exe \\$ComputerName -h powershell.exe "Enable-PSRemoting ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
  
PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com
Controlador no válido.
Connecting to notworkingcomputer...Couldn't access notworkingcomputer:
Connecting to notworkingcomputer...
Enable-PSRemotingRemote : Ups... something was wrong :(
En línea: 1 Carácter: 1
+ Enable-PSRemotingRemote -ComputerName notworkingcomputer
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Enable-PSRemotingRemote
 
C:\PS>
 
Fail to connect and run Enable-PsRemoting on remote computer.
 
#>


Function Enable-PSRemotingRemote{
 [CmdletBinding()]
 Param(
     [Parameter(Mandatory=$True)]
     [String[]]$ComputerName
 )
 
IF (Test-Path $env:SystemRoot\PsExec.exe -PathType Leaf) {
    foreach ($computer in $ComputerName) {
    Write-Host
    Write-Output "Trying to enable PSRemoting with PsExec on $Computer..."
    PsExec.exe -accepteula \\$Computer -h powershell.exe "Enable-PSRemoting -Force"
            }
     }
Else {
      Write-Error "Can't found `"$env:SystemRoot\PsExec.exe`" to remotely enable PSRemoting"
      }
IF ($LASTEXITCODE -eq '0') {
   Write-Host
   Write-Output "Seems all went fine, it should be working now!"
   Write-Host
   }
ELSEIF ($LASTEXITCODE -eq '1') {
   Write-Host
   Write-Warning "Seems not all went fine, but it could work. Try it!"
   Write-Host
       }
Else {
      Write-Error "Ups... something went wrong :("
      }
}