Archive for the ‘VB Script’ Category

Been using this script to check my servers after maintenance. Just lazy I guess. Remember you must have relevant permissions for the script to execute.

‘—- Start of Script —

strComputer = “PutServer/PCnameHere”

Set ServiceSet = GetObject(”winmgmts:{impersonationLevel=impersonate}!\\” & _
strComputer).ExecQuery _
(”select * from Win32_Service where State=’Stopped’ and StartMode=’Auto’”)

for each Service in ServiceSet
MsgBox Service.Name, 64, “Auto Started Service has Stopped :”
next

If ServiceSet.Count = 0 Then
WScript.echo “No services found meeting query criteria.”
End If

msgbox “Done”

I had to come up with a way to vaildate some domain user credentials for a support package. Here’s the code in it’s simplest vbs form :

‘“““““““““““““““““““““““““““““

Dim objDSO, objDomain, objSysInfo, strDomain

Set objSysInfo = CreateObject(”ADSystemInfo”)
strDomain = objSysInfo.DomainShortName

Set objDSO = GetObject(”WinNT:”)
On Error Resume Next
Set objDomain = objDSO.OpenDSObject(”WinNT://” & strDomain, “username”, “password”, ADS_SECURE_AUTHENTICATION)

If Err.Number <> 0 Then
   
    Select Case Err.Number
      Case -2147023570
        MsgBox “Unknown user name or password.”, 64, “Windows Authentication”
      Case -2147022987
        MsgBox “User account is locked.”, 64, “Windows Authentication”
      Case -2147023565
        MsgBox “User account is disabled.”, 64, “Windows Authentication”
      Case Else
         MsgBox “Unknown error, unable to verify credentials.” & vbCRLF & _
         “Error Code: ” & Err.Number, 64, “Windows Authentication”
    End Select
   
    Set objDSO = Nothing
    Set objDomain = Nothing
    Set objSysInfo = Nothing
   
    Wscript.Quit
   
End If

Set objDSO = Nothing
Set objDomain = Nothing
Set objSysInfo = Nothing

MsgBox “Credentials verified.”, 64, “Windows Authentication”

Const ForAppending = 8

Set objFSO = CreateObject(”Scripting.FileSystemObject”)

resLog = “r35uLt.log”

objStartFolder = InputBox (”Enter Folder Path :” & vbCRLF & _
vbCRLF & “\\servername\path to folder”, “Create File List”)

If Len(objStartFolder) = 0 Then
MsgBox “A valid path must be entered.”, _
64, “Create File List”
WScript.Quit(0)
End If

If objFSO.FileExists(objStartFolder & “\” & resLog) = False Then
objFSO.CreateTextFile objStartFolder & “\” & resLog
End if

Set oFile = objFSO.OpenTextFile(objStartFolder & “\” & resLog, ForAppending)

Set objFolder = objFSO.GetFolder(objStartFolder)
oFile.WriteLine objFolder.Path & Chr(59)
Set colFiles = objFolder.Files

For Each objFile in colFiles
oFile.WriteLine objFile.Name & Chr(59)
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)
oFile.Close

MsgBox “File List created :” & vbCRLF & vbCRLF & _
objStartFolder & “\” & resLog, 64, “Create File List”

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
oFile.WriteLine Subfolder.Path & Chr(59)
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
oFile.WriteLine objFile.Name & Chr(59)
Next

ShowSubFolders Subfolder
Next
End Sub

Use the below script to disable the windows firewall on XP. To enable the firewall, change the second last line from false to true. Tested on XP SP1-3.

‘Start of script :

Set objFirewall = CreateObject(”HNetCfg.FwMgr”)
Set objPolicy = objFirewall.LocalPolicy.CurrentProfile

objPolicy.FirewallEnabled = FALSE

Msgbox “Done”

Use the below script to display the local pc BIOS version and build number. Tested against XP sp3.

‘Start of Script :

strComputer =”.”
Set objWMI = GetObject(”winmgmts:\\” & strComputer & “\root\CIMV2″)
Set colItems = objWMI.ExecQuery(”SELECT * FROM Win32_BIOS”)
For Each itm In colItems
strBIOSVersion = Join(itm.BIOSVersion, “,”)
WScript.Echo “BIOSVersion: ” & strBIOSVersion
WScript.Echo “BuildNumber: ” & itm.BuildNumber
WScript.Echo “SMBIOSBIOSVersion: ” & itm.SMBIOSBIOSVersion
WScript.Echo “SMBIOSMajorVersion: ” & itm.SMBIOSMajorVersion
WScript.Echo “SMBIOSMinorVersion: ” & itm.SMBIOSMinorVersion
WScript.Echo “Version: ” & itm.Version
Next

Use the below script to kill a process on a remote machine. In this example I’m killing off an instance of outlook.

NB. permissions are required on the remote pc or the script will fail.

‘Start of script :

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

Remotekill()

‘———————————————————-
Function RemoteKill()

strComputer = “remotepcname”

On Error Resume Next

Set oWMIKill = GetObject(”winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)

Set colPClogin = oWMIKill.ExecQuery _
(”Select * from Win32_Process Where Name = ‘outlook.exe’”)

For Each objProcess in colPClogin
objProcess.Terminate()
Next

Set objProcess = Nothing
Set colKeyacc = Nothing
Set colPClogin = Nothing
Set oWMIKill = Nothing

MsgBox “Kill sent to remote computer”, 64, “Remote Process Kill”

End Function
‘———————————————————-

Use the below script to discover the members of the local administrator’s group on a windows XP pc.

‘Start of script :

strComputer = “.”
Set colGroups = GetObject(”WinNT://” & strComputer & “”)
colGroups.Filter = Array(”group”)

For Each objGroup In colGroups

If objGroup.Name = “Administrators” Then

For Each objUser in objGroup.Members
szStr = szStr & objUser.Name & vbCRLF
Next

MsgBox objGroup.Name & ” Group” & vbCRLF & vbCRLF & “Members :” & vbCRLF & _
vbCRLF & szStr, 64, “Local Admin Group Members”

Exit For
End If

Next

MsgBox wanIPaddress, 64, “Find WAN Address”

Function wanIPaddress( )

‘ //Function uses WhatIsMyIP.com’s automation page:
‘ //http://www.whatismyip.com/automation/n09230945.asp
‘ //to find the WAN IP Address.

Dim szURL, objHTTP

On Error Resume Next

‘//Request data from whatismyip.com

szURL = “http://www.whatismyip.com/automation/n09230945.asp”
Set objHTTP = CreateObject( “WinHttp.WinHttpRequest.5.1″ )
objHTTP.Open “GET”, szURL
objHTTP.Send

If objHTTP.Status = 200 Then
wanIPaddress = objHTTP.ResponseText
Else
wanIPaddress = “IP = 0.0.0.0″ & vbCRLF & vbCRLF & _
“Unable to retrieve WAN IP from : ” & vbCRLF & _
“http://www.whatismyip.com/automation/n09230945.asp”
End If

Set objHTTP = Nothing

End Function

set objShell = CreateObject(”UserAccounts.CommonDialog”)
objShell.Filter = “All Files*.*”
objshell.ShowOpen
on error resume next

Const wdFormatXML = 11