Home

Google
 

VBScript to find old users in a domain


On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject(”ADODB.Connection”)
Set objCommand = CreateObject(”ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
Set objCommand.ActiveConnection = objConnection
Set objDSE = GetObject(”LDAP://RootD
SE”)
strDomain = objDSE.Get(”DefaultNamingContext”)
objCommand.CommandText = “SELECT adspath,Name FROM ‘LDAP://” & strDomain & “‘ ” & “WHERE objectClass=’user’ and objectcategory=’person’ and lastlogontimestamp <=’00000000000′”
objCommand.Properties(”Page Size”) = 2000
objCommand.Properties(”Timeout”) = 30
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE
objCommand.Properties(”Cache Results”) = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strUserName = objRecordSet.Fields(”Name”).Value
WScript.Echo strUserName
objRecordSet.MoveNext
Loop

How to get IP Address Information from Network Adapters Using Vbscript

The following Vbscript will gather Ip address information from network cards:

Set objWMIService = GetObject(”winmgmts:\\”& strComputer & “\root\cimv2″)
Set colAdapters = objWMIService.ExecQuery (”SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True”)
n = 1
For Each objAdapter In colAdapters
wIELine
WriteLineBoldIE “Network Adapter Info”
wIETableHeaderStart “100″
wIERowStart
wIETableHeaderItem “Adapter”
wIETableHeaderItem “IP”
wIETableHeaderItem “Subnet”
wIETableHeaderItem “Gateway”
wIETableHeaderItem “DNS”
wIETableHeaderItem “WINS”
wIERowStop
If Not IsNull(objAdapter.IPAddress) Then
For i = 0 To UBound(objAdapter.IPAddress)
wIERowStart
If i=0 Then
wIERowItem “Adapter ” & objAdapter.Index
‘ wIERowItem left(objAdapter.Description,15)
Else
wIERowItem “No Name “
End If
If objAdapter.DHCPEnabled Then
wIERowItem objAdapter.IPAddress(i) & “(DHCP)”
Else
wIERowItem objAdapter.IPAddress(i)
End If
Next
End If
If Not IsNull(objAdapter.IPSubnet) Then
For i = 0 To UBound(objAdapter.IPSubnet)
wIERowItem objAdapter.IPSubnet(i)
Next
End If

If Not IsNull(objAdapter.DefaultIPGateway) Then
For i = 0 To UBound(objAdapter.DefaultIPGateway)
wIERowItem objAdapter.DefaultIPGateway(i)
Next
Else
wIERowItem “Not configured”
End If

If Not IsNull(objAdapter.DNSServerSearchOrder) Then
DNSInfo = “”
For i = 0 To UBound(objAdapter.DNSServerSearchOrder)
DNSInfo = DNSInfo & objAdapter.DNSServerSearchOrder(i) & “<BR>”
Next
wIERowItem DNSInfo
End If
If objAdapter.WINSPrimaryServer <> “” Then
wIERowItem objAdapter.WINSPrimaryServer & “<BR>” & objAdapter.WINSSecondaryServer
End If

n = n + 1
wIETableStop
Next

How do I Disable Windows Product Activation Notices with VBscript?

If you are interested in disabling the Windows product activation notices with a vbscript then the following steps maybe of interest to you:

  1. In the root of your system drive save the following in a text file
  2. Set objWMI = GetObject(”winmgmts:{impersonationLevel=
    impersonate}!\\” & computer & “\root\cimv2″)
    Set objWPA = objWMI.ExecQuery(”Select * from
    Win32_WindowsProductActivation”)

    For Each PA in objWPA
    PA.SetNotification(0)
    Next

  3. Replace computer with the name of the target computer. Bolded items should be on the same line in the script.
  4. Name the file deactivate.vbs
  5. open a CMD prompt and change directory to c:>
  6. Run the following command:
  7. c:> cscript deactivate.vbs

How do I script the unlocking of domain user accounts?

To use this script you will need to enter the relevant username and domain into message boxes that prompt.

UserName = InputBox(”Enter the user’s login name that you want to unlock:”)

If UserName = “” Then
WScript.Echo “No username was given or you clicked Cancel”
WScript.Quit(1)
End If

DomainName = InputBox(”Enter the domain name in which the user account exists:”)

If DomainName = “” Then
WScript.Echo “No domain was given or you clicked Cancel”
WScript.Quit(1)
End If

Set UserObj = GetObject(”WinNT://”& DomainName &”/”& UserName &”")

If UserObj.IsAccountLocked = -1 then UserObj.IsAccountLocked = 0

UserObj.SetInfo

If err.number = 0 Then

Wscript.Echo “The Account Unlock Failed. Check that the account is, ” & _
“in fact, locked-out.”

Else

Wscript.Echo “The Account Unlock was Successful”

End If

How do I Remotely Shutdown all Domain Computers -VBscript

If you every need to shutdown all you domain computers due to a expected power outage, security breach etc. then this Vbscript should help:
On Error Resume Next

Set objNet = CreateObject(”wscript.network”)
strCurrentPC = objNet.ComputerName

‘The following section will connect to any Active Directory domain

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject(”ADODB.Connection”)
Set objCommand = CreateObject(”ADODB.Command”)
objConnection.Provider = “ADsDSOObject”
objConnection.Open “Active Directory Provider”
Set objCommand.ActiveConnection = objConnection
Set objDSE = GetObject(”LDAP://RootDSE”)
strDomain = objDSE.Get(”DefaultNamingContext”)
objCommand.CommandText = “SELECT Name, Location FROM ‘LDAP://” & strDomain & “‘ ” & “WHERE objectClass=’computer’”
objCommand.Properties(”Page Size”) = 1000
objCommand.Properties(”Timeout”) = 30
objCommand.Properties(”Searchscope”) = ADS_SCOPE_SUBTREE
objCommand.Properties(”Cache Results”) = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strPCName = objRecordSet.Fields(”Name”).Value
If strPCName <> strCurrentPC then
set colOS = getobject(”winmgmts:{impersonationlevel=impersonate,(shutdown)}//” & strPCName).instancesof(”win32_operatingsystem”)
For each objOS in colOS
objOS.win32shutdown(1)
Next
End if
objRecordSet.MoveNext
Loop

======================================

This script can be easily modified to include other shutdown options which include reboots and log offs. Reference the Microsoft MSDN site: http://msdn2.microsoft.com/en-us/library/aa394058.aspx for more details on the win32shutdown class.


How do I Remotely Terminate all Windows Terminal Server Sessions with Vbscript?

‘ This vbscript finds all active Terminal Server sessions on server HOST defined
‘ below and terminates them. Does not terminate the
‘ current session if being run remotely.
‘ If running on local host, set HOST value to “.”

HOST = “.”

‘ Get disconnected sessions and log them off
sessions = DisconnectedSessions(HOST)

For each session in sessions
TerminateWinSession HOST, sessionId
Next

‘ Now get active sessions and log them off
sessions = ActiveSessions(HOST)

For each session in sessions
TerminateWinSession HOST, sessionId
Next

Sub TerminateWinSession(Host, sessionId)
Dim Sh, tmpHost
Set Sh = createobject(”WScript.Shell”)
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
Sh.Run “%COMSPEC% /C rwinsta ” & sessionId & tmpHost, 0, False
End Sub

Function ActiveSessions(Host)
Dim tmpHost, aTmp, aTmp1(), i
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
aTmp = Split(cmd(”qwinsta” & tmpHost & ” | find “”Active”"”), _
vbCrLf)
ReDim aTmp1(-1)
For i = 0 to UBound(aTmp)
If Left(aTmp(i),1) <> “>” Then
Redim Preserve aTmp1(UBound(aTmp1) + 1)
aTmp1(UBound(aTmp1)) = Trim(Mid(aTmp(i), 42, 6))
End If
Next
ActiveSessions = aTmp1
End Function

Function DisconnectedSessions(Host)
Dim tmpHost, aTmp, aTmp1(), i
if trim(Host)=”" Then
tmpHost = “”
Else
tmpHost = ” /SERVER:” & Host
End If
aTmp = Split(cmd(”qwinsta” & tmpHost & ” | find “”Disconnected”"”), _
vbCrLf)
ReDim aTmp1(-1)
For i = 0 to UBound(aTmp)
If Left(aTmp(i),1) <> “>” Then
Redim Preserve aTmp1(UBound(aTmp1) + 1)
aTmp1(UBound(aTmp1)) = Trim(Mid(aTmp(i), 42, 6))
End If
Next
DisconnectedSessions = aTmp1
End Function

Function Cmd(cmdline)
‘ Wrapper for getting StdOut from a console command
Dim Sh, FSO, fOut, OutF, sCmd
Set Sh = createobject(”WScript.Shell”)
Set FSO = createobject(”Scripting.FileSystemObject”)
fOut = FSO.GetTempName
sCmd = “%COMSPEC% /c ” & cmdline & ” >” & fOut
Sh.Run sCmd, 0, True
If FSO.FileExists(fOut) Then
If FSO.GetFile(fOut).Size>0 Then
Set OutF = FSO.OpenTextFile(fOut)
Cmd = OutF.Readall
OutF.Close
End If
FSO.DeleteFile(fOut)
End If
End Function

How do I disable Inactive Active Directory User Accounts with Vbscripting?

If you are a system administrator managing an Active Directory environment and you need to disable inactive user accoutns then the following vbscript may assist:

‘Queries AD for all WIndows domain accounts inactive for 12 weeks or more and disables these accounts

dsquery user -inactive 12 | dsmod user -disabled yes

‘Queries AD for all active directory disabled accounts and moves the accounts to the OU -disabled accounts. Script will not run if the # of domain accounts exceed 100
dsquery user -disabled | dsmove “ou=Disabled Accounts,dc=example,dc=net” -safety 100

Reference the Microsoft article: http://support.microsoft.com/kb/322684 for more details on dsquery and dsmod usage.


powered by FreeFind