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.
- $username = "username"
- $password = "password"
- $servers = "machine1", "machine2"
- $cred = New - Object System.Management.Automation.PSCredential - ArgumentList@ ($username, (ConvertTo - SecureString - String $password - AsPlainText - Force))
- Foreach($s in $servers) {
- "Machine Name $s"
- if (Test - Connection - Cn $s - BufferSize 16 - Credential $username - Count 1 - ea 0 - quiet) {
- Write - Host "Successfully Connected to $s" - ForegroundColor Green
- } Else {
- "Problem connecting to $s Flushing DNS"
- ipconfig / flushdns | out - null "Registering DNS"
- ipconfig / registerdns | out - null "Re-pinging $s"
- if (!(Test - Connection - Cn $s - BufferSize 16 - Count 1 - ea 0 - quiet)) {
- Write - Host "Problem still exists in connecting to $s" - ForegroundColor Red
- } ELSE {
- Write - Host "Resolved problem connecting to $s" - ForegroundColor Green
- }#end
- if
- }#end
- if
- }#end foreach