Checking Connection with Servers/Machine

Hi Folks,
 
Here is the code to check the servers/machine is connected or not.
 
First, you should be Admin on that machine, which machine/server you want to test.
 
Then put your username, password and servers or machine names in the below variables.
  1. $username = "username"  
  2. $password = "password"  
  3. $servers = "machine1""machine2"  
  4. $cred = New - Object System.Management.Automation.PSCredential - ArgumentList@ ($username, (ConvertTo - SecureString - String $password - AsPlainText - Force))  
  5. Foreach($s in $servers) {  
  6.     "Machine Name $s"  
  7.     if (Test - Connection - Cn $s - BufferSize 16 - Credential $username - Count 1 - ea 0 - quiet) {  
  8.         Write - Host "Successfully Connected to $s" - ForegroundColor Green  
  9.     } Else {  
  10.         "Problem connecting to $s Flushing DNS"  
  11.         ipconfig / flushdns | out - null "Registering DNS"  
  12.         ipconfig / registerdns | out - null "Re-pinging $s"  
  13.         if (!(Test - Connection - Cn $s - BufferSize 16 - Count 1 - ea 0 - quiet)) {  
  14.             Write - Host "Problem still exists in connecting to $s" - ForegroundColor Red  
  15.         } ELSE {  
  16.             Write - Host "Resolved problem connecting to $s" - ForegroundColor Green  
  17.         }#end  
  18.         if  
  19.     }#end  
  20.     if  
  21. }#end foreach