none
rpcping equivalent RRS feed

  • Question

  • I am writing an RPC health check functionality in PowerShell. It should not call any external tools (like rpcping or portqry). Instead, I need the logic to perform this check. Can someone throw light on this please?

    thanks in advance.


    Kannan
    Wednesday, March 23, 2011 10:34 AM

All replies

  • Hi,

    You may use System.Net.Sockets.TcpClient object to check it.

    $computername = "server1"
    $port = "135"
    
    try
    {
     $tcp=New-Object System.Net.Sockets.TcpClient($computername, $port)
    }
    catch
    {
        
    }
    
    if($tcp.client.connected)
    {
     write-host "OK"  
    }
    else
    {
     write-host "Not OK" 
    }
    
    Wednesday, March 23, 2011 11:54 AM
  • This is a good idea... the concern that I have here is - how would this validate the health of RPC service. It would be good to ask the RPC service "are you okay" and get a response like "I am okay - RPC service" which ensures that RPC service is really really okay. I need something like this... hope I am able to present my need here to a reasonalbe extent.

    Thanks,


    Kannan
    Wednesday, March 23, 2011 12:24 PM
  • This is a good idea... the concern that I have here is - how would this validate the health of RPC service. It would be good to ask the RPC service "are you okay" and get a response like "I am okay - RPC service" which ensures that RPC service is really really okay. I need something like this... hope I am able to present my need here to a reasonalbe extent.

    Thanks,


    Kannan

    You may check status service via WMI or use Get-Service.

    Get-Service  *rpc* -ComputerName Test or gwmi Win32_Service -Filter "Name like '%rpc%'" -Comp Test

    Wednesday, March 23, 2011 12:32 PM
  • Thanks Kazun,

    Yes, this is a good approach. Here when the service plunges into a zombie state (all appears okay, byt internally it's dead and not responding), we may not be able to get the real health. It would be good to ask the RPC service "are you okay" and get a response like "I am okay - RPC service" which ensures that RPC service is really really okay. I need something like this... Any guidance would be greatly appretiated.

    Thanks,


    Kannan
    Wednesday, March 23, 2011 12:48 PM
  • Thanks Kazun,

    Yes, this is a good approach. Here when the service plunges into a zombie state (all appears okay, byt internally it's dead and not responding), we may not be able to get the real health. It would be good to ask the RPC service "are you okay" and get a response like "I am okay - RPC service" which ensures that RPC service is really really okay. I need something like this... Any guidance would be greatly appretiated.

    Thanks,


    Kannan
    If service is not Ok ,Get-Service will return an error,because  RPC is unavailable.
    Wednesday, March 23, 2011 12:59 PM
  • Guessed I missed an important point - The check is going to be performed by an agent in the subject machine. So there will not be a WMI call to a remote machine to perform this check. Also there is a timebound of about 5 seconds for this operation to be wrapped up. it would be great if you throw light on the internals of how RPCPing works and how the same functionality could be accomplished using C# / powershell / C++ to check the health of RPC service.

    Thanks,


    Kannan
    Wednesday, March 23, 2011 2:25 PM
  • Hi,

    You may use System.Net.Sockets.TcpClient object to check it.

    $computername = "server1"
    
    $port = "135"
    
    
    
    try
    
    {
    
     $tcp=New-Object System.Net.Sockets.TcpClient($computername, $port)
    
    }
    
    catch
    
    {
    
      
    
    }
    
    
    
    if($tcp.client.connected)
    
    {
    
     write-host "OK" 
    
    }
    
    else
    
    {
    
     write-host "Not OK" 
    
    }
    
    

     

    I think you need to add the next line at the end of the script:

    $tcp.Close()

    Or else the script will only work once I think. That's what I just experienced any way.

    Hope this helps.

    Wednesday, August 3, 2011 9:05 AM
  • I think the WMI call is a good option, if you write it out you can control the timeout. i think a simple use of RPC is a good indication if its working correctly or not. Im not aware of any special health check functions 

     

    try ######### wmi
    {	
    	$w = [wmi] ''
    	$w.psbase.options.timeout = 15000000
    	$w.path = "\\$ComputerName\root\cimv2:Win32_ComputerSystem.Name='$ComputerName'"
    	$w | select none | Out-Null
    	write-host "Successful"
    }
    catch
    	{write-host "Failed"}
    

     


    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, August 3, 2011 12:23 PM
  • How can i save the results to a csv file?

    thanks

    Tuesday, November 22, 2011 9:01 PM
  • $servers = get-content serverlist.txt
    $results = @()
    
    foreach($server in $servers)
    {
    $temp = "" | select name,state
    $temp.name = $server
    try {	
    	$w = [wmi] ''
    	$w.psbase.options.timeout = 15000000
    	$w.path = "\\$server\root\cimv2:Win32_ComputerSystem.Name='$ComputerName'"
    	$w | select none | Out-Null
    	$temp.state = $true
    }
    catch
    	{$temp.state = $false}
    
    $results += $temp
    }
    

    something like that will work


    Justin Rich
    http://jrich523.wordpress.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, November 25, 2011 7:15 PM
  • Your script could still use some of the suggestions already given on the computer it is running on by simply not giving a -computer parameter, or specifying it explicitly with its name or generically as ".".

    As to information about the internals of RPCPing, that seems somewhat outside the scope of the Powershell forum. But you might find some useful information here: http://www.sapien.com/forums/scriptinganswers/forum_posts.asp?TID=4014

    Friday, November 25, 2011 10:06 PM
  • Even simpler?

    $computer = "Server1"

    try
    {
    $obj = gwmi win32_computersystem -computername $computer -ErrorAction Stop
    write-host "Successful"
    }
    catch
    {
    write-host "Failed" 
    }
    $obj = ""


    • Edited by ThomasLuxton Thursday, July 19, 2012 4:55 PM Correction
    Thursday, July 19, 2012 4:54 PM
  • This solution worked great. Thanks for putting it together!
    Monday, October 17, 2016 7:55 PM