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
  1. Dim objShell : set objShell = CreateObject("Wscript.Shell")  
  2. Dim objIE :Set objIE = CreateObject("InternetExplorer.Application")  
  3.   
  4. objShell.Run "<path>\AskUser.vbs",1,True  
  5.   
  6. objIE.Navigate("about:blank")  
  7. WScript.echo objIE.document.parentwindow.clipboardData.GetData("text")  
  8. </path>  


AskUser.vbs
  1. Dim objIE : Set objIE = CreateObject("InternetExplorer.Application")  
  2.   
  3. If Msgbox("Please press YES or NO", vbYesNo) = vbYes Then  
  4.  YourText = "YES was pressed"  
  5. Else  
  6.  YourText = "NO was pressed"  
  7. End If  
  8.   
  9. 'Write to clipboard  
  10. objIE.Navigate("about:blank")  
  11. 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.
  1. Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")  
  2. Dim objShell : Set objShell = CreateObject("Wscript.Shell")  
  3. Dim objFile : Set objFile = objFSO.OpenTextFile("c:\computers.txt")   
  4. Dim strOldIP : strOldIP = "1.1.1.1"  
  5. dim strNewIP : strNewIP = "2.2.2.2"  
  6. Dim arrDNSServer(2)  
  7. dim i : i = 0  
  8.    
  9. Do While Not objFile.AtEndOfStream  
  10.         currentserver = objFile.ReadLine  
  11.         Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & currentserver & "\root\cimv2")  
  12.         Set colNicConfigs = objWMIService.ExecQuery  ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")  
  13.    
  14.         For Each objNicConfig In colNicConfigs  
  15.                 If Not IsNull(objNicConfig.DNSServerSearchOrder) Then  
  16.                         For Each strDNSServer In objNicConfig.DNSServerSearchOrder  
  17.                          arrDNSServer(i) = strDNSServer  
  18.                          i=i+1  
  19.                         Next  
  20.                 End If  
  21.         Next  
  22.   
  23.         If arrDNSServer(0) = strOldIP Then  
  24.    'Changing the primary dns server  
  25.    objShell.Run "netsh -r " ¤tserver &" interface ip set dnsserver " &chr(34) &"local area connection" &Chr(34) &" static " &strNewIP &" primary",1,True  
  26.    objShell.Run "netsh -r " ¤tserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" " &arrDNSServer(1),1,True  
  27.    objShell.Run "netsh -r " ¤tserver &" interface ip add dns " &chr(34) &"local area connection" &Chr(34) &" " &arrDNSServer(1) &" index=2",1,True  
  28.   ElseIf arrDNSServer(1) = strOldIP Then  
  29.    'Changing the secondary DNS Server  
  30.    objShell.Run "netsh -r " ¤tserver &" interface ip delete dnsserver " &chr(34) &"local area connection" &Chr(34) &" " &arrDNSServer(0),1,True  
  31.    objShell.Run "netsh -r " ¤tserver &" interface ip set dnsserver " &chr(34) &"local area connection" &Chr(34) &" static " &arrDNSServer(0) &" primary",1,True  
  32.    objShell.Run "netsh -r " ¤tserver &" interface ip add dns " &chr(34) &"local area connection" &Chr(34) &" " &strNewIP &" index=2",1,True  
  33.   End If  
  34.  i = 0  
  35. 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.

  1. Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")  
  2. strComputer = "."  
  3. Dim EventTime, strUsername  
  4. strLogonSuccess = "528"  
  5. strLogonError = "529"  
  6. Set objWMIService = GetObject("winmgmts:" & "{(Security)}!\\" & strComputer & "\root\cimv2")  
  7.    
  8. 'Determine the last successful logon process  
  9. Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'Security' and EventCode = '" &strLogonSuccess &"'")  
  10. For Each objEvent in colLoggedEvents  
  11.  If Not IsNull(objEvent.User) And objEvent.User = objNetwork.UserDomain &"\" &objNetwork.UserName Then  
  12.   If cint(objEvent.Eventcode) = cint(strLogonSuccess) Then  
  13.    Call ConvertTime(objEvent.TimeWritten)  
  14.    Wscript.Echo "The last successful Logon from " &objEvent.User &" was at " &EventTime  
  15.    Exit For  
  16.   End If  
  17.  End If  
  18. Next  
  19.    
  20. 'Determine the last failed logon process  
  21. Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'Security' and EventCode = '" &strLogonError &"'")  
  22. For Each objEvent in colLoggedEvents  
  23.  'Convert username out of the eventvwr message  
  24.  Call GetUsername(objEvent.Message)  
  25.    
  26.  'Check if username from event matches local logged in user  
  27.  If lcase(cstr(strUsername)) = lcase(cstr(objNetwork.UserName)) Then  
  28.   If cint(objEvent.Eventcode) = cint(strLogonError) Then  
  29.    Call ConvertTime(objEvent.TimeWritten)  
  30.    Wscript.Echo "The last failed Logon from " &strUsername &" was at " &EventTime  
  31.    Exit For  
  32.   End If  
  33.  End If  
  34. Next  
  35.    
  36. Function ConvertTime(datetime)  
  37. EventTime = Mid(datetime, 5, 2) & "/" & Mid(datetime, 7, 2) & "/" & _  
  38.     Mid(datetime, 1, 4) & " " & Mid(datetime, 9, 2) & ":" & _  
  39.     Mid(datetime, 11, 2) & "." & Mid(datetime, 13, 2)  
  40. End Function  
  41.    
  42. Function GetUsername(strMessage)  
  43.  strStart = InStr(strMessage,"User Name:") + 10  
  44.  strEnd = InStr(strMessage,"Domain:")  
  45.  strLen = strEnd - strStart  
  46.    
  47.  strUsername = Replace(Replace(Replace(Replace(Mid(strMessage,strStart,strLen)," ",""),vbTab, ""),VbCrLf,""), vbNewLine,"")  
  48. End Function