WinRMCheck.psm1

# WinRM Enable & Disable Module



# Takes no action, just purely for checking current status.

Function Get-WinRMStatus {
    if (!([bool](Test-WSMan -ComputerName $env:COMPUTERNAME -ErrorAction SilentlyContinue))){
         return "WinRM Not Enabled, Try Running Enable-WinRM"
        }
    elseif ((Get-Service -Name "WinRM" | Where-Object {$_.Status -eq "Running"}) -and (Test-Path -Path WSMan:\Localhost\listener\listener*)){
        return "WinRM Enabled, if you need to disable run Disable-WinRM"
        }
     }    
    

# If you wish to disable Windows Remote Management, this function will check the listener and service, and will remove both.

Function Disable-WinRM {
     if ([bool](Get-Service -Name "WinRM" | Where-Object {$_.Status -eq "Running"})){
        Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse -ErrorAction SilentlyContinue
        (Disable-PSRemoting) | Out-Null
        Set-NetFirewallRule -DisplayName 'Windows Remote Management (HTTP-In)' -Enabled False -PassThru | Select -Property DisplayName, Profile, Enabled
        Stop-Service "WinRM" -ErrorAction SilentlyContinue -Force
        return "WinRM Disbled"
        }
     elseif ([bool](Get-Service -Name "WinRM" | Where-Object {$_.Status -eq "Stopped"}))
        {return "Service Already Disabled"}
     }


# Checks the current status of the running service, deletes any leftover listener files, then recreates the config.

Function Enable-WinRM {
    if (([bool](Get-Service -Name "WinRM" | Where-Object {$_.Status -eq "Stopped"}))){
        Start-Service -Name "WinRM" -ErrorAction SilentlyContinue
        Remove-Item -Path WSMan:\Localhost\listener\listener* -Recurse
        winrm quickconfig /q | Out-Null
        Enable-PSRemoting | Out-Null
        Restart-Service -Name "WinRM" -Force -ErrorAction SilentlyContinu
        return "WinRM Has Been repaired, and enabled"
    }
    elseif (([bool](Get-Service -Name "WinRM" | Where-Object {$_.Status -eq "Running"}) -and (Test-Path -Path WSMan:\Localhost\listener\listener*))) {
        return "WinRM Enabled"
    }
}