Showing posts with label vbscript. Show all posts
Showing posts with label vbscript. Show all posts

Thursday, June 5, 2014

Bulk disable AD users script

I wrote this quick handy script to disable a bunch of users from a text file. The client I was working at still had a 2003 level domain so I had to old school it with VBScript :)

Run this on a DC in your environment and fill in users in a text file named disableList.txt. I have it set to output to a disableUsers.bat file so you can inspect it before you run it.

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("disableList.txt", ForReading)

'Read in users
Dim ListToProcess()
i = 0
Do Until objFile1.AtEndOfStream
Redim Preserve ListToProcess(i)
ListToProcess(i) = objFile1.ReadLine
i = i + 1
Loop

objFile1.Close

outFile="disableUsers.bat"
Set objFile = objFSO.CreateTextFile(outFile,True)

For Each strLine in ListToProcess
 if strLine <> "" then
 objFile.Write "dsquery user -samid " & strLine & " | dsmod user -disabled yes " & vbCrLf
 End if
Next

objFile.Close

Friday, September 7, 2012

VBScript to rename Program Neighborhood Paths

The client I was working at had a multi-farm XenApp environment so I ended up needing a script that would run on XA 4.5 as well. This is essentially the same script I wrote in powershell translated to vbscript, edit appropriately as you need for your environment.


option explicit
Const MetaFrameWinAppObject = 3
Const MetaFrameWinFarmObject = 1
Dim aMFApp, hMFAppTemp, aMFTemp
Dim MFFarm : Set MFFarm = WScript.CreateObject ("MetaFrameCOM.MetaFrameFarm")
MFFarm.Initialize(MetaFrameWinFarmObject)
Dim mode, count, strReplace
mode = "Debug"
count = 0
Dim objECA, objAPAC, objAGLNG
Set objECA = New RegExp
Set objAPAC = New RegExp
Set objAGLNG = New RegExp
With objECA
                .Pattern = "ECA\\"
                .IgnoreCase = True
End With
With objAGLNG
                .Pattern = "AGLNG\\"
                .IgnoreCase = True
End With
With objAPAC
                .Pattern = "APAC\\"
                .IgnoreCase = True
End With
For Each aMFApp in MFFarm.Applications
Set hMFAppTemp = CreateObject("MetaFrameCOM.MetaFrameApplication")
hMFAppTemp.Initialize MetaFrameWinAppObject,aMFApp.DistinguishedName
hMFAppTemp.LoadData(True)
Set aMFTemp = hMFAppTemp.WinAppObject2
                If objAGLNG.Test(aMFTemp.PNFolder) = True Then
                                count = count + 1
                                strReplace = Replace(aMFTemp.PNFolder,"AGLNG\","")
                                If mode = "Debug" Then
                                                Wscript.Echo aMFTemp.AppName & " - old: " & aMFTemp.PNFolder & " new: " & strReplace
                                Elseif mode = "Real" Then
                                                aMFTemp.PNFolder = strReplace
                                                aMFTemp.SaveData()
                                End If   
                ElseIf objAPAC.Test(aMFTemp.PNFolder) = True Then
                                count = count + 1
                                strReplace = Replace(aMFTemp.PNFolder,"APAC\","")
                                If mode = "Debug" Then
                                                               
                                                Wscript.Echo aMFTemp.AppName & " - old: " & aMFTemp.PNFolder & " new: " & strReplace
                                Elseif mode = "Real" Then
                                                aMFTemp.PNFolder = strReplace
                                                aMFTemp.SaveData()
                                End If   
                ElseIf objECA.Test(aMFTemp.PNFolder) = True Then
                                count = count + 1
                                strReplace = Replace(aMFTemp.PNFolder,"ECA\","")
                                If mode = "Debug" Then
                                                Wscript.Echo aMFTemp.AppName & " - old: " & aMFTemp.PNFolder & " new: " & strReplace
                                Elseif mode = "Real" Then
                                                aMFTemp.PNFolder = strReplace
                                                aMFTemp.SaveData()
                                End If   
                End If
Set aMFTemp = Nothing
Set hMFAppTemp = Nothing
Next
Wscript.Echo "Total applications updated : " & count

Wednesday, June 13, 2012

VBScript to Enumerate Installed Applications and Versions

Here is a script I wrote to discover the installed application versions across a list of servers. I have it formatted so it will export a csv file with the information. Note: I also wrote some conditional statements to exclude some Windows update patches/hotfixes/security updates, etc.

To use: "cscript script.vbs" and will generate a results.csv file, also provide a ServerList.txt filled with the list of servers (by line) which you wish to scan the applications and their version numbers.

Here it is:

Dim args,computername, count
count = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("ServerList.txt", ForReading)
Const ForReading = 1
'Read Compare To List
Dim ListToProcess()
i = 0
Do Until objFile1.AtEndOfStream
Redim Preserve ListToProcess(i)
ListToProcess(i) = objFile1.ReadLine
i = i + 1
Loop
objFile1.Close
For Each strLine in ListToProcess
 if strLine <> "" then
 computername = strLine
 Call enumApps(strLine,1)
 End if
Next

Function enumApps(computername,flag)
On Error Resume Next
Dim strComputer, strLine

strComputer = computername
Const HKLM        = &H80000002
Const strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
Wscript.echo "===============" & "Parsing " & computername & "==============="
Dim oReg, arrSubKeys, strProduct, strDisplayName, strVersion
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &  strComputer & "\root\default:StdRegProv")
oReg.EnumKey HKLM, strKeyPath, arrSubKeys
For Each strProduct In arrSubKeys
    oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
    oReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion

 Dim Filter
 If InStr(1, strDisplayName, "", vbTextCompare) > 0 Then
  If InStr(1, strDisplayName, "Security", vbTextCompare) = 0 Then
   If InStr(1, strDisplayName, "Update", vbTextCompare) = 0 Then
    If InStr(1, strDisplayName, "Hotfix", vbTextCompare) = 0 Then
     strVersion = Replace(strVersion, ",", "")
     strLine = strLine & strDisplayName & "," & strVersion & "," & strComputer & vbCrLf
     'Wscript.echo strLine
    End If
   End If
  End If
 End If


Next

Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "results.csv"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Opening the file
Set ObjFile = ObjFso.OpenTextFile (StrFileName, 8, True)
If count = 0 then
 objFile.WriteLine "Application Name, Version,Server"
 count = 1
end if
objFile.WriteLine strLine
objFile.Close
End Function

Tuesday, August 30, 2011

Vbscript to compare services among hosts inside domain

Below is a script I wrote to compare services on a list of hosts versus a "template" list of services and show the differences, very useful if your trying to find differences among roles/services running on a large number of hosts.


How to use:

Run inside domain or do runas and make sure your vpn

C:\Windows\System32\runas.exe /u:<domain>\<account> /netonly cmd

Cscript services.vbs

Cscript makeTemplate /computer:<Computer to make template services>

Template.txt = The list of services that is used to compare against
compareList.txt = Dump of Hosts to look against

services.vbs
----------------------------------------------------------------
Dim args,computername

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("template.txt", ForReading)

Const ForReading = 1

'Read template
Dim arrFileLinesTemplate()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile1 = objFSO.OpenTextFile("CompareList.txt", ForReading)


'Read Compare To List
Dim ListToProcess()
i = 0
Do Until objFile1.AtEndOfStream
Redim Preserve ListToProcess(i)
ListToProcess(i) = objFile1.ReadLine
i = i + 1
Loop
objFile1.Close

For Each strLine in ListToProcess

    if strLine <> "" then
    computername = strLine
    Call enumServices(strLine,1)
    End if

Next

Function enumServices(computername,flag)
On Error Resume Next
Dim objsvc,svccount,count, tempStr, found, strCompName
found = 1
count = 0
strCompName = computername
wscript.echo "==============Checking " & ucase(computername) & "=============="
set objsvc = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery _
            ("SELECT * FROM Win32_Service")
if not (errorChecking (computername)) then
    for each svc in objsvc
        count = count + 1
        Call writetoLog(svc.displayname,svc.state,svc.startmode,svc.pathname,svc.processid,computername)
        if flag = 1 then
            tempStr = svc.displayname
            For Each strLine in arrFileLines
                intCompare = StrComp(strLine, tempStr, vbTextCompare)
               
                if intCompare = 0 then
                    'Wscript.Echo svc.displayname & " xxxx found in template xxxx"
                    found=0
                    exit For
                end if
            Next
            if found = 1 then
                Wscript.Echo svc.displayname & " hosted on " & strCompName & " was not found in template "
               

            end if
            found = 1
        end if
    next       
    wscript.echo "There are " & count & " Services on " & ucase(strCompName)
    wscript.echo "Disconnecting from " & ucase(strCompName)
    wscript.echo ""
end if
set objsvc = nothing
End Function



Function writetoLog(displayname,state,startmode,pathname,processid,computername)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile(logPath & "\services_" & computername & ".txt", 8,True)
    objFSOwriteline.WriteLine(displayname)
    objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

----------------------------------------------------------------

makeTemplate.vbs
----------------------------------------------------------------
Dim args,computername,adwg,logPath
Set args = Wscript.Arguments.Named
computername = args.Item("computer")
logPath = getLogPath()

if wscript.arguments.count = 0 then
    wscript.echo "Script Usage:"
    wscript.echo "Enum Service on Single PC: cscript makeTemplate.vbs /computer:[ComputerName]"
   
elseif args.exists("computer") then
    if computername = "" then
        computername = getComputer()
    else
        computername = computername
    end if
    Call enumServices(computername,1)

End if


Function enumServices(computername,flag)
On Error Resume Next
Dim objsvc,svccount,count
count = 0
wscript.echo "Connecting to " & ucase(computername) & " building template..... "
set objsvc = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & computername & "\root\cimv2").ExecQuery _
            ("SELECT * FROM Win32_Service")
if not (errorChecking (computername)) then
    for each svc in objsvc
        count = count + 1
        Call writetoLog(svc.displayname,svc.state,svc.startmode,svc.pathname,svc.processid,computername)
        if flag = 1 then
            'wscript.echo svc.displayname & "," & svc.state & "," & svc.startmode
        end if
    next       
    wscript.echo "There are " & count & " Services on " & ucase(computername)
    wscript.echo "Disconnecting from " & ucase(computername)
    wscript.echo ""
end if
set objsvc = nothing
End Function


Function writetoLog(displayname,state,startmode,pathname,processid,computername)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFSOwriteline = FSO.OpenTextFile("Template.txt", 8,True)
    objFSOwriteline.WriteLine(displayname)
    objFSOwriteline.close
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
    temp2 = temp2 & temp(i) & "\"
next
getLogPath = temp2
End Function

----------------------------------------------------------------