Developer's Closet A place where I can put my PHP, SQL, Perl, JavaScript, and VBScript code.

2Jul/100
Query a List of Servers from Active Directory

I needed a list of all servers listed in AD. There are many scripts out there that do this, but they didn't quite fit my needs, so I put together one from a few different scripts.

strADDomainName = "domainname.com"

strBase = ";"
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objFileOut = objFSO.OpenTextFile ("listofservers.txt", 2, True)

strFilter = "(&(objectclass=computer)(objectcategory=computer)(operatingSystem=*Server*)(cn=*));"
strAttrs = "cn;"
strScope = "subtree"
Set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"

Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
objRS.MoveFirst
While Not objRS.EOF
	WScript.Echo objRS.Fields(0).Value
	objFileOut.WriteLine(LCase(objRS.Fields(0).Value) & ", " & LCase(objRS.Fields(0).Value) & "." & strADDomainName)
	objRS.MoveNext
WEnd

objFileOut.Close
Set objFileOut = Nothing
Set objFSO = Nothing

Reference: http://www.intellipool.se/forum/index.php?showtopic=849

29Jun/100
SQL Server CPU Utilization

Some helpful SQL stats from this article.

-- From the SQL Server Performance Dashboard (sys.dm_os_ring_buffers)
-- Query CPU utilization for each four minute intervals
declare @ts_now bigint
select @ts_now = cpu_ticks / convert(float, cpu_ticks_in_ms) from sys.dm_os_sys_info
select record_id,
      dateadd(ms, -1 * (@ts_now - [timestamp]), GetDate()) as EventTime,
      SQLProcessUtilization,
      SystemIdle,
      100 - SystemIdle - SQLProcessUtilization as OtherProcessUtilization
from (
      select
            record.value('(./Record/@id)[1]', 'int') as record_id,
            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') as SystemIdle,
            record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') as SQLProcessUtilization,
            timestamp
      from (
            select timestamp, convert(xml, record) as record
            from sys.dm_os_ring_buffers
            where ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
            and record like '%%') as x
      ) as y
order by record_id desc

-- 4:12 (two procs CPU=8,9,9,9)
DECLARE @count int
SELECT @count =100000000
WHILE @count > 0
	BEGIN
	SELECT @count=@count-1
	--PRINT @count
END
6May/100
List Scheduled Tasks from a Windows Server

This script will query a server and output a list of scheduled tasks as a csv (Excel) file. This file is useful when auditing servers.

Note: This script works only for Server 2003, Server 2008, and Windows XP (requires schtasks.exe).


Dim TimeStamp: TimeStamp = Right("0" & Month(now()), 2) & Right("0" & Day(now()), 2) & Right("0" & Year(now()), 2) & Right("0" & Hour(now()), 2) & Right("0" & Minute(now()), 2) & Right("0" & Second(now()), 2)

TmpFile = "C:\Temp\ScheduledTasks\schtasks.txt"
LogFile = "C:\Temp\ScheduledTasks\output_" & TimeStamp & ".csv"

servers = "SERVER-01,SERVER-02,SERVER-03,SERVER-04"
serverArr = Split(servers, ",")

Const ForReading = 1
Const ForWriting = 2

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

cmdstr = "cmd /c echo >" & TmpFile
retval = objShell.Run(cmdstr, 1, True)

For Each Computer In serverArr
'WScript.Echo Computer

If Computer  "" Then
    cmdstr = "cmd /c schtasks /query /s \\" & Computer & " /v /fo list >>" & TmpFile
Else
    cmdstr = "cmd /c schtasks /query /v /fo list >>" & TmpFile
End If
retval = objShell.Run(cmdstr, 1, True)
Next

Dim objLog: Set objLog = objFSO.CreateTextFile(LogFile)
Set objLog = Nothing
Set objLog = objFSO.OpenTextFile(LogFile, 2)

If objFSO.FileExists(TmpFile) Then
    Dim linecount, objFSO, objFile, ln, strLine

    linecount = 0 
servercount = 0
 
    Set objFile = objFSO.OpenTextFile(TmpFile, ForReading)
    Do Until objFile.AtEndOfStream
        strLine = Trim(objFile.Readline)

        If Left(strLine, 9) = "HostName:" Then
            hostname = Mid(strLine, 39)
            'wscript.echo vbTab & ""
If (servercount = 0) Then 
objLog.Write """HostName"",""TaskName"",""Next Run Time"",""Status"",""Logon Mode"",""Last Run Time"",""Last Result"",""Creator"",""Schedule"",""Task To Run"",""Start In"",""Comment"",""Scheduled Task State"",""Scheduled Type"",""Start Time"",""Start Date"",""End Date"",""Days"",""Months"",""Run As User"",""Delete Task If Not Rescheduled"",""Stop Task If Runs X Hours and X Mins"",""Repeat: Every"",""Repeat: Until: Time"",""Repeat: Until: Duration"",""Repeat: Stop If Still Running"",""Idle Time"",""Power Management"""
End If
objLog.Write vbCrLf
servercount = servercount + 1
     	 objLog.Write """" & hostname & """"
        ElseIf Left(strLine, 9) = "TaskName:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
            'wscript.echo vbTab & vbTab & "" & hostname & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 14) = "Next Run Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 7) = "Status:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """" & "," & """" & "Interactive/Background" & """"
     
        ElseIf Left(strLine, 14) = "Last Run Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 12) = "Last Result:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 8) = "Creator:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 9) = "Schedule:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 12) = "Task To Run:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 9) = "Start In:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 8) = "Comment:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 21) = "Scheduled Task State:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 15) = "Scheduled Type:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 11) = "Start Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 11) = "Start Date:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 9) = "End Date:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 5) = "Days:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
     
        ElseIf Left(strLine, 7) = "Months:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 12) = "Run As User:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 31) = "Delete Task If Not Rescheduled:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 37) = "Stop Task If Runs X Hours and X Mins:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 14) = "Repeat: Every:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 20) = "Repeat: Until: Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 24) = "Repeat: Until: Duration:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 30) = "Repeat: Stop If Still Running:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 10) = "Idle Time:" Then
            'wscript.echo vbTab & vbTab & "" & Mid(strLine, 39) & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        ElseIf Left(strLine, 17) = "Power Management:" Then
            'wscript.echo vbTab & ""
objLog.Write "," & """" & Mid(strLine, 39) & """"
        End If
        linecount = linecount + 1
    Loop
    objFile.Close
    Set objFSO = Nothing
 
Else
    wscript.echo "fail: file not found"
End If

WScript.Echo "End"