Test-WinRM PowerShell Cmdlet

Gosh, I was attempting to RDP to a remote server today at work when I realised that the RDP Session is dead with the infamous grey screen. I decided that it fine, I will use PowerShell to remotely view the processes on the remote server and I found WMI is leaking over 2000 handles.

Hmmm… I thought to myself, I should be able to issue an Invoke-Command to remotely kill the process since I don’t have a GUI on that remote 2003R2 server. But all I got is an error output stating that WinRM is not configured.

So I issued Get-Service to remotely query that 2003R2 server and found out that WinRM service does not exist. I am not surprised because 2003R2 does not have this WinRM service by default anyway.

I got home and decided to draft a PowerShell Cmdlet for testing WinRM and this is how these codes come about.

Download Link – http://gallery.technet.microsoft.com/Test-WinRM-91734e31

This is extremly handy for me now and you can download this free from the link above.

function Test-WinRM {

    Test Windows Remote Management
    Allows the administrator to test Windows Remote Management is working or not on 
    localhost and remote server. It will enable administrator to be able to establish 
    a quick test of the Windows Remote Management in their environment and assess 
    the possibility of executing powershell script remotely.

.PARAMETER ComputerName
    Specify a hostname for query.

    Specify to continous testing until stopped.
    To stop -type Control-C.
    Test-WinRM -ComputerName Redmond
    This initiate a Windows Remote Management test on Redmond server

    Test-WinRM -ComputerName Contoso -Test
    This initiate a continuous Windows Remote Management test on Contoso server until 
    user initiate to stop the test using Control-C. If Windows Remote Management not 
    running on Contoso, it will continously return False.

    Test-WinRM -ComputerName Contoso -Verbose
    VERBOSE: WinRM - Running
    This initiate a Verbose output.
    $Test = Test-WinRM -ComputerName Redmond ; if($Test -eq $True){ write-host "Yes!!! Eureka!!! It works!" }
    Yes!!! Eureka!!! It works!
    This utilises the returned boolean value as True or False for other condition statement.

    Windows Remote Management
    Installation and Configuration for Windows Remote Management

    Author  : Ryen Kia Zhi Tang
    Date    : 23/07/2012
    Blog    : ryentang.wordpress.com
    Version : 1.0

    Windows Server 2003 R2:  WinRM is not installed by default, but is available as 
    the Hardware Management feature through the Add/Remove System Components feature 
    in Control Panel under Management and Monitoring Tools.

#define command parameters
        $ComputerName = $env:computername,

        [Switch] $Test


    #clear variable
    $Result = ""


       try {
            #invoke a command to get WinRM service status
            $Result = Invoke-Command -ComputerName $ComputerName -ScriptBlock {Get-Service | Where-Object {($_.Name -eq "WinRM") -and ($_.Status -eq "Running")}} -ErrorAction Stop
            #success output
            if($PSBoundParameters['Verbose']) { Write-Verbose "WinRM - Running" }else{ $True}
            #failure output
            if($PSBoundParameters['Verbose']) { Write-Verbose "WinRM - Not Running"; Write-Error $_.ToString() }else{ $False }
        #verify if -Test parameter is specified
        if($Test) { Continue }else{ Break }

END { }

} #end of #function Test-WinRM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s