kay5
kay5

Reputation: 57

powershell to Ping,RDP,RemoteRegistry,WMI

I wrote a Powershell script that will perform Ping, RDP(3389), Remote Registry and WMI connectivity to a remote computer. Below is the script that I have come up with. Now I would like to get the output in a short format such as:

    PING             :    SUCCESS
    RDP              :    FAIL
    Remote Registry  :    FAIL
    WMI              :    SUCCESS

    Remote Registry check has FAILED.
    RDP check has FAILED.

    PING check SUCCEEDED
    WMI check SUCCEEDED

I am looking for help as I am new to Powershell scripting. Thank you in advance!!


Write-Host `n
$inputfromuser = Read-Host "Enter Server Name"

If($inputfromuser -like "")
{
    Write-Host "User Input cannot be blank. Please enter the server name"
    Write-Host `n
}

else 
{

    Write-Host `n
    Write-Host -ForegroundColor Yellow "CHECKING PING CONNECTIVITY TO SERVER  $inputfromuser"
    Test-Connection -ComputerName $inputfromuser -Quiet -Count 1
    Write-Host `n
    Write-Host -ForegroundColor Yellow "CHECKING RDP PORT 3389 CONNECTIVITY ...."
    Test-NetConnection -ComputerName $inputfromuser -CommonTCPPort RDP -InformationLevel Quiet
    Write-Host `n

    Write-Host -ForegroundColor Yellow "CHECKING REMOTE REGISTRY CONNECTIVITY ...."
    $regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$inputfromuser)
    $ref = $regkey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
    If (!$ref) {
        Write-Host -ForegroundColor Red "REMOTE REGISTRY CHECK FAILED"
    }

   Else {
        Write-Host -ForegroundColor Green "REMOTE REGISTRY CHECK SUCCESS"  
   }
   Write-Host `n

   Write-Host -ForegroundColor Yello "CHECKING REMOTE WMI CONNECTIVITY ...."
   $wmi = GWMI -Query "Select * from Win32_PingStatus where Address = '$inputfromuser'"
   If (!$wmi) {
        Write-Host -ForegroundColor Red "REMOTE WMI CHECK FAILED"
    }

   Else {
        Write-Host -ForegroundColor Green "REMOTE WMI CHECK SUCCESS"
    }
   Write-Host `n

 }

Upvotes: 0

Views: 4536

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

I'd recommend separating tests from output creation. Assign the results of your checks to separate variables:

$ping = Test-Connection ...
$rdp  = Test-NetConnection ...
$regkey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $inputfromuser)
$ref  = $regkey.OpenSubKey("...")
$wmi  = Get-WmiObject -Query "..."

create 2 hashtables:

$state_noun = @{
  $true  = 'SUCCESS'
  $false = 'FAIL'
}
$state_verb = @{
  $true  = 'SUCCEEDED'
  $false = 'FAILED'
}

and create the output with a here-string:

$result = @"
PING             :    $($state_noun[$ping])
RDP              :    $($state_noun[$rdp])
Remote Registry  :    $($state_noun[[bool]$ref])
WMI              :    $($state_noun[[bool]$wmi])

Remote Registry check has $($state_verb[[bool]$ref]).
RDP check has $($state_verb[$rdp]).

PING check $($state_verb[$ping])
WMI check $($state_verb[[bool]$wmi])
"@

Write-Host $result

If you must have highlighted/colored values in your output, use a custom output function like this:

function Write-Result {
  [CmdletBinding()]
  Param(
    [Parameter()][string]$text,
    [Parameter()][bool]$value
  )

  Write-Host $text -NoNewline
  if ($value) {
    Write-Host $value -NoNewline -ForegroundColor Green
  } else {
    Write-Host $value -NoNewline -ForegroundColor Red
  }
  Write-Host '.'
}

Write-Result 'PING             :    ' $state_noun[$ping]
...
Write-Result 'Remote Registry check has ' $state_verb[[bool]$ref]
...

Upvotes: 3

Related Questions