9/04/2008

Retrieving "exit strings" from vbScript

Retrieving exit codes from vbScript is not very tricky, but there's no method to retrieve "exit strings" from a script.
Below there's listed a little workaround to keep strings in "computers mind" after a script finished without using tempfiles, registry keys or environment variables as data store.

Step by Step:

  1. ShowResult.vbs calls the script AskUser.vbs

  2. AskUser.vbs captures somes user input

  3. AskUser.vbs writes the user input into the windows clipboard

  4. AskUser.vbs ends

  5. ShowResults.vbs queries the clipboard for the written text

  6. ShowResults.vbs shows the result



ShowResult.vbs

Dim objShell : set objShell = CreateObject("Wscript.Shell")
Dim objIE :Set objIE = CreateObject("InternetExplorer.Application")

objShell.Run "\AskUser.vbs",1,True

objIE.Navigate("about:blank")
WScript.echo objIE.document.parentwindow.clipboardData.GetData("text")


AskUser.vbs

Dim objIE : Set objIE = CreateObject("InternetExplorer.Application")

If Msgbox("Please press YES or NO", vbYesNo) = vbYes Then
YourText = "YES was pressed"
Else
YourText = "NO was pressed"
End If

'Write to clipboard
objIE.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", YourText

9/03/2008

vbScript to automatically changing a specific DNS Server on all Servers

The following vbScript replaces a specific DNS Server IP with another one, independed if the IP is the Primary or Secondary DNS Server.

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
Dim objFile : Set objFile = objFSO.OpenTextFile("c:\computers.txt")
Dim strOldIP : strOldIP = "1.1.1.1"
dim strNewIP : strNewIP = "2.2.2.2"
Dim arrDNSServer(2)
dim i : i = 0

Do While Not objFile.AtEndOfStream
currentserver = objFile.ReadLine
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & currentserver & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each objNicConfig In colNicConfigs
If Not IsNull(objNicConfig.DNSServerSearchOrder) Then
For Each strDNSServer In objNicConfig.DNSServerSearchOrder
arrDNSServer(i) = strDNSServer
i=i+1
Next
End If
Next

If arrDNSServer(0) = strOldIP Then
'Changing the primary dns server
objShell.Run "netsh -r " ¤tserver &" interface ip set dnsserver " &chr(34) &"local area connection" &Chr(34) &" static " &strNewIP &" primary",1,True
objShell.Run "netsh -r " ¤tserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" " &arrDNSServer(1),1,True
objShell.Run "netsh -r " ¤tserver &" interface ip add dns " &chr(34) &"local area connection" &Chr(34) &" " &arrDNSServer(1) &" index=2",1,True
ElseIf arrDNSServer(1) = strOldIP Then
'Changing the secondary DNS Server
objShell.Run "netsh -r " ¤tserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" " &arrDNSServer(0),1,True
objShell.Run "netsh -r " ¤tserver &" interface ip set dnsserver " &chr(34) &"local area connection" &Chr(34) &" static " &arrDNSServer(0) &" primary",1,True
objShell.Run "netsh -r " ¤tserver &" interface ip add dns " &chr(34) &"local area connection" &Chr(34) &" " &strNewIP &" index=2",1,True
End If
i = 0
Loop

9/02/2008

Get User's last successful and failed logon

The following VBScript will show the last successful and the last failed logon of the current user. Depending of the OS Version the event id's from eventvwr has to be customized.


Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
strComputer = "."
Dim EventTime, strUsername
strLogonSuccess = "528"
strLogonError = "529"
Set objWMIService = GetObject("winmgmts:" & "{(Security)}!\\" & strComputer & "\root\cimv2")

'Determine the last successful logon process
Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'Security' and EventCode = '" &strLogonSuccess &"'")
For Each objEvent in colLoggedEvents
If Not IsNull(objEvent.User) And objEvent.User = objNetwork.UserDomain &"\" &objNetwork.UserName Then
If cint(objEvent.Eventcode) = cint(strLogonSuccess) Then
Call ConvertTime(objEvent.TimeWritten)
Wscript.Echo "The last successful Logon from " &objEvent.User &" was at " &EventTime
Exit For
End If
End If
Next

'Determine the last failed logon process
Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'Security' and EventCode = '" &strLogonError &"'")
For Each objEvent in colLoggedEvents
'Convert username out of the eventvwr message
Call GetUsername(objEvent.Message)

'Check if username from event matches local logged in user
If lcase(cstr(strUsername)) = lcase(cstr(objNetwork.UserName)) Then
If cint(objEvent.Eventcode) = cint(strLogonError) Then
Call ConvertTime(objEvent.TimeWritten)
Wscript.Echo "The last failed Logon from " &strUsername &" was at " &EventTime
Exit For
End If
End If
Next

Function ConvertTime(datetime)
EventTime = Mid(datetime, 5, 2) & "/" & Mid(datetime, 7, 2) & "/" & _
Mid(datetime, 1, 4) & " " & Mid(datetime, 9, 2) & ":" & _
Mid(datetime, 11, 2) & "." & Mid(datetime, 13, 2)
End Function

Function GetUsername(strMessage)
strStart = InStr(strMessage,"User Name:") + 10
strEnd = InStr(strMessage,"Domain:")
strLen = strEnd - strStart

strUsername = Replace(Replace(Replace(Replace(Mid(strMessage,strStart,strLen)," ",""),vbTab, ""),VbCrLf,""), vbNewLine,"")
End Function