PowerShell: Check RPC Reachability of Remote Computer

# This is an ancient script that would work with PowerShell versions 2 to 5, but it won't work with version 6

  function checkRpc($server){
    #  WORKFLOW QUERIES THE PASSED ARRAY OF PORTS TO DETERMINE STATUS
    workflow Check-Port {
    param ([string[]]$RPCServer,[array]$arrRPCPorts)
    $comp = hostname

    ForEach -parallel ($RPCPort in $arrRPCPorts){
    $bolResult = InlineScript{Test-NetConnection -ComputerName $Using:RPCServer -port $Using:RPCPort -InformationLevel Quiet}
    If ($bolResult){
    Write-Output "$RPCPort on $RPCServer is reachable"
    }Else{
    Write-Output "$RPCPort on $RPCServer is unreachable"
    }
    }
    }
    #  INITIAL RPC PORT
    $strRPCPort = "135"
    #  MODIFY PATH TO THE PORTQRY BINARY IF NECESSARY
    $strPortQryPath = "C:\Sysinternals"
    #  TEST THE PATH TO SEE IF THE BINARY EXISTS
    If (get-command portqry.exe){
    $strPortQryCmd = "PortQry.exe -e $strRPCPort -n $Server"
    }Else{
    Write-Output "Could not locate Portqry.exe"
    Exit
    }
    #  CREATE AN EMPTY ARRAY TO HOLD THE PORTS RETURNED FROM THE RPC PORTMAPPER
    $arrPorts = @()
    #  RUN THE PORTQRY COMMAND TO GET THE EPHEMERAL PORTS
    $arrQuryResult = Invoke-Expression $strPortQryCmd
    # CREATE AN ARRAY OF THE PORTS
    ForEach ($strResult in $arrQuryResult)
    {
    If ($strResult.Contains("ip_tcp"))
    {
    $arrSplt = $strResult.Split("[")
    $strPort = $arrSplt[1]
    $strPort = $strPort.Replace("]","")
    $arrPorts += $strPort
    }
    }
    #  DE-DUPLICATE THE PORTS
    $arrPorts = $arrPorts | Sort-Object |Select-Object -Unique
    #  EXECUTE THE WORKFLOW TO CHECK THE PORTS
    Check-Port -RPCServer $Server -arrRPCPorts $arrPorts
  }

checkRpc remotecomputername

Leave a Reply

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