Posted On March 29, 2019

Check Servers NSLookup of a Listener to Match Active Node IP

kimconnect 0 comments
blog.KimConnect.com >> Codes , Database >> Check Servers NSLookup of a Listener to Match Active Node IP
$servers="SQL01","SQL02","SQL03","SQL04"
$listener="halistener01"
$activeNode="10.10.10.5"

# Dynamic Credential method 1
$who = whoami
	if ($who.Substring($who.length-2, 2)="-admin"){$username=$who;}
    else {$username=$who+"-admin";}
$password = Read-Host -Prompt "Input the password for account $username" -AsSecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
$elevate = New-PSSession -ComputerName localhost -Credential $cred

foreach ($server in $servers){
    #$ip=(invoke-command -Session $elevate -ScriptBlock {(nslookup $Args[0] | Select-String Address | Where-Object LineNumber -eq 5).ToString().Split(' ')[-1];} -Args $listener) 
    $ip=(invoke-command -Session $elevate -ScriptBlock {(Resolve-DnsName $Args[0]).IPAddress;} -Args $listener)
    $result= if ([IPAddress]$ip.Trim() -eq [IPAddress]$activeNode.Trim()){"Pass";}else{"fail";};
    $server+": $result"    
}

Leave a Reply

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

Related Post

PowerShell: Special Parameter datatype [switch]

# Demo of a simple function to turn lights on and offfunction setLight{ param( [switch]$on,…

PowerShell: Check IP Conflicts of Computers in Active Directory

We have ran into issues where a group of virtual machines living on a DHCP…

PowerShell: How To Disable and/or Stop Windows Service

Disable and Stop Using Native Command 'Set-Service' # To disable service as well as stopping…