By Lee Mason

TechRepublic
recently asked members to submit their favorite Network Administration scripts
for possible publication. One of the first to make a submission was Lee Mason. For his effort, Lee earned $200
and the satisfaction of seeing his scripts published on TechRepublic.


Earn $100 for your admin script

Let us pay
you for your original scripts so that we can publish them as downloads on
TechRepublic and allow your fellow IT professionals to benefit from your
scripting savvy. We only ask that you put in the appropriate comments to your
scripts so that it’s easy to tell what the script is doing and which variables
might need to be customized. Send us your original admin scripts
and we’ll pay you $100 for each one
that we publish as a TechRepublic download.


Editor’s note: These scripts are included in the downloadable Zip
file
for your convenience.

In their own words

Service Restart

‘Service restart script
‘Written by L Mason
‘Script checks to see if a service is running locally.
If it isn’t it restarts it.

Option Explicit

Const ADS_SERVICE_RUNNING          = &H0004
Const ADS_SERVICE_ERROR            = &H0008

Dim LocalComputerName
Dim LocalService
Dim DisplayNotices
Dim LogFileName
Dim TimeOut
Dim Retries

Dim FSys
Dim objComputer
Dim objService
Dim tFile
Dim wshShell

LocalService = “Themes”
Retries = 10

‘Create Object
Set wshShell = CreateObject(“WSCript.Shell”)

LocalComputerName =
wshShell.RegRead(“HKLM\SYSTEM\
CurrentControlSet\Control
\ComputerName
\ComputerName\ComputerName”)

Set objComputer = Nothing

‘enter your domain name here
Set objComputer = GetObject(“WinNT://yourdomain/”
& LocalComputerName & “,computer”)

Set objService = Nothing
Set objService = objComputer.GetObject(“service”, LocalService)

If Err.Number <> 0 Then
‘Error returned – could not connect to service
      MsgBox “The ‘” & LocalService & “‘ service
was not found on your computer. 
The operation will now exit.”,vbOkOnly+vbCritical
      Err.Clear
      ClearEnvironment -1
Else
      ‘Query Status / Start
      Timeout = 0
      While objService.Status <> ADS_SERVICE_RUNNING
      objService.Start
      Sleep 1
      TimeOut = TimeOut + 1
      If TimeOut => Retries OR objService.Status =
ADS_SERVICE_ERROR Then
            If DisplayNotices = True Then
            MsgBox “Unable to start the ‘” &
LocalService & “‘ service on your computer.
The operation will now exit.”, vbOkOnly+vbCritical, “Abort”
            End If
ClearEnvironment -1
      End If
      Wend
End If
ClearEnvironment 0

‘————————————-

Sub ClearEnvironment(ExitCode)
On Error Resume Next
Set FSys = Nothing
Set tFile = Nothing
Set objComputer = Nothing
Set objService = Nothing
WScript.ExitExitCode
End Sub

Sub Sleep(Seconds)
Dim Timer
Timer = Second(Time)+5
If Timer > 59 Then Timer = Timer – 59
While Timer > Second(Now)
Wend
End Sub

Function ResolveStatus(Status)
Const ADS_SERVICE_STOPPED          = &H0001
Const ADS_SERVICE_START_PENDING    = &H0002
Const ADS_SERVICE_STOP_PENDING     = &H0003
Const ADS_SERVICE_RUNNING          = &H0004
Const ADS_SERVICE_CONTINUE_PENDING = &H0005
Const ADS_SERVICE_PAUSE_PENDING    = &H0006
Const ADS_SERVICE_PAUSED           = &H0007
Const ADS_SERVICE_ERROR            = &H0008
Dim Color
Dim StatusText
Select Case Status
      Case ADS_SERVICE_STOPPED
      StatusText = “Stopped”
      Case ADS_SERVICE_START_PENDING
      StatusText = “Start Pending”
      Case ADS_SERVICE_STOP_PENDING
      StatusText = “Stop Pending”
      Case ADS_SERVICE_RUNNING
      StatusText = “Running”
      Case ADS_SERVICE_CONTINUE_PENDING
      StatusText = “Continue Pending”
      Case ADS_SERVICE_PAUSE_PENDING
      StatusText = “Pause Pending”
      Case ADS_SERVICE_PAUSED
      StatusText = “Paused”
      Case ADS_SERVICE_ERROR
      StatusText = “ERROR”
End Select
ResolveStatus = StatusText
end Function

LDAP Query Script

‘LDAP Query Script
‘v1.0
‘Written By Lee Mason
‘Purpose: Uses ADSI To Query an OU and its
subcontainers for users that are
‘NOT members of a specified group
‘then writes out to a text file.
‘———————————————–

Option Explicit

‘Declare and obtain Variables
Dim ObjLocation
Dim ObjMember
Dim ObjGroup
Dim FilOutputMembers
Dim FilOutputNonMembers
Dim fso
Dim ObjectType
Dim arrSubContainers(4)
dim i

dim strtargetgroup ‘the group you want to find
dim strtoplevelOU ‘the first part of your domain,
ie for “mycompany.co.uk”, this would be “mycompany”
dim strsecondlevelOU ‘co
dim strthirdlevelOU ‘uk
‘add more in here if you need them – don’t forget to
add them to the getobject statement below!

‘enumerate subcontainer names in an array for
looping through
ObjectType = “<subOU1>”
arrsubcontainers(0) = “<subOU2>”
arrsubcontainers(1) = “<subOU3>”
arrsubcontainers(2) = “<subOU3>”
arrsubcontainers(3) = “<subOU4>”

‘set reference to PC-Duo Group
set ObjGroup = GetObject(“LDAP://cn=” & strtargetgroup & “,
ou=” & strtoplevelOU & “,
ou=” & strsecondlevelOU & “,dc=” & strthirdlevelOU)

‘Instantiate Output Files using FilesystemObject
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set FilOutputMembers =
fso.CreateTextFile(“\\springboardha.org.uk\dfs
\track-IT\Reporting\Group Lists\”
& Objgroup.name & ” Members.txt”, True)
With FiloutputMembers
      .WriteLine(“Member List”)
      .writeline(“—————–“)
      .writeline(“Generated ” & Date())
      .writeline(” “)
End With

Set FilOutputNonMembers =
fso.CreateTextFile(“\\springboardha.org.uk\dfs
\track-IT\Reporting\Group Lists\”
& Objgroup.name & ” NonMembers.txt”, True)
With FiloutputNonMembers
      .WriteLine(“Non-Member List”)
      .writeline(“—————–“)
      .writeline(“Generated ” & Date())
      .writeline(” “)
End With

‘Bind to Ads group for each subcontainer
for i=0 to 4
     
      ‘Set reference to tehsubcontainers contained in the array
      Set objLocation = GetObject(“LDAP://ou=”
& arrsubcontainers(i)
& “,ou=” & Objecttype & strtoplevelOU
& “,dc=” & strsecondlevelOU
& “,dc=” & strthirdlevelOU)
      MsgBoxObjLocation.name

      ‘test for membership and write result out to appropriate file
      For Each ObjMember In ObjLocation
            If ObjGroup.IsMember(“LDAP://”
& ObjMember.name & “,
ou=” & arrsubcontainers(i)
& “,ou=” & Objecttype
&  “,dc=” & strsecondlevelOU
& “,dc=” & strthirdlevelOU) Then
                  filoutputMembers.WriteLine(arrsubcontainers(i)
& ” ”
& ObjMember.name)
            Else
                  filoutputNonMembers.WriteLine(arrsubcontainers(i)
& ” ” & ObjMember.name)
            End If
      next