Today I want to turn my some of the scripting efforts to public.
Sometimes I get requirements to see how many machines are online which are part of a AD security group. I worst method I used to follow was dumping the group member list to some text file and making use of a batch script to ping the machine and check the status. This is pretty good but consuming some of my time for dumping and analyzing. So why below script is born….
You just need to give the group DN in the script and execute it with cscript. That shows the machine status if it is online or not. Feel free to modify the script to match your requirements and let me know if I can be of any help.
'##########################################################################
'# Purpose : To check the ping status of computers part of a security group
'# Author : Sitaram Pamarthi
'#
'##########################################################################
'On Error Resume Next
' Replace with your group DN
GroupDN="ldap://CN=Your/ Group Name,OU=Your OU name,DC=domain,DC=com"
Set objGroup = GetObject(GroupDN)
objGroup.GetInfo
arrMemberOf = objGroup.GetEx("member")
For Each strMember in arrMemberOf
Set objGroup1 = GetObject("LDAP://" & strMember)
strHost=trim(objGroup1.dNSHostName)
set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_PingStatus where address = '" & strHost & "'")
for each objRetStatus in objPing
if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
WScript.Echo strhost & " ==> host not reachable"
else
Ping = True
wscript.echo strhost & " ==> Machine Reachable"
end if
next
Next
'#End of script.
Happy Learning…,
Sitaram Pamarthi
Sitaram Pamarthi
Here is the powershell equivalent code for this.
ReplyDelete$groupmembers = Get-QADComputer -memberof "domain.com/ou-name/group-name"
foreach ($member in $groupmembers) {
$comp = $member.name
$status = Get-WmiObject win32_Pingstatus -filter "Address = '$comp'"
if($status.statuscode -eq 0) {
write-host -foreground green "$comp(ONLINE)"
}Else {
write-host -foreground red "$comp(OFFLINE)"
}
}