email
Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Thursday, November 19, 2009

Script to ping machines part of Active Directory Security Group

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

Monday, April 6, 2009

Quick way to find hotfix installation status

I know there are varioud menthods for finding the hotfix installation status, but I felt this as very easy one.

To find the hotfix installation status on local machine:

wmic qfe where hotfixid="KB958644" list full

To find on a remote machine:

wmic /node: qfe where hotfixid="KB958644" list full

To find on list of machines:

o Place all machines into a text file(machine.txt)
o Run the below batch file

=== File name: get-hotfix-status.bat ===

echo off
for /f "tokens=* delims= " %a in (test.txt) do wmic/Node:%a qfe where hotfixid="KB958644" list full

=== End of file ===

Please comment if you know any better way.

Happy Learning,
Sitaram Pamarthi,

Wednesday, January 21, 2009

Script to generate last logon time of all users

SBelow code give the last logon time of all users in active directory. Please drop me a mail, if you want any further fine tuning to this script or incase of queries.

I am providing this script without any warranty. Use at your own choice. :-)

Code:

############# Getlastlogon.vbs ################

on error resume next

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

objCommand.CommandText = _
"<
LDAP://dc=mydomain,dc=com>;" & _
"(&(objectCategory=person)(objectClass=user));" & _
"ADsPath;subtree"

Set objRecordSet = objCommand.Execute

While Not objRecordset.EOF
strADsPath = objRecordset.Fields("ADsPath")
Set objUser = GetObject(strADsPath)
set objLogon = objUser.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo "Approx last logon timestamp for " & strADsPath & intLogonTime + #1/1/1601#
objRecordset.MoveNext
Wend
objConnection.Close
#################
#################


Note: you can redirect the script output from command prompt to clipboard by using http://www.sitaram-pamarthi.com/2009/01/copy-command-output-to-clip-board.html

Syntax: cscript
Getlastlogon.vbs clip

Happy Learning...
Sitaram Pamarthi