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

7/10/2008

vbScript to backup and restore the windows taskbar toolbars

1. Create a backup of the toolbar settings
The following vbscript creates a backup of the current users toolbar settings.
It's also possible to target other userprofiles or computers. For this the variables HKEY_CURRENT_USER and strComputer must be customized.
The backupfile will be written at D:\ToolbarBackup.txt

Dim strComputer
Dim objFile, objFSO
Dim objRegistry
Dim strKeyPath
Dim arrValues, strValue
Const HKEY_CURRENT_USER = &H80000001

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop"
strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\ToolbarBackup.txt")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

objRegistry.GetBinaryValue HKEY_CURRENT_USER,strKeyPath, "TaskbarWinXP",arrValues

For Each strValue In arrValues
objFile.WriteLine strValue
Next

objFile.Close
Set objRegistry = Nothing
Set objFile = Nothing
Set objFSO = Nothing


2. Restore backup of the toolbar settings
To restore the toolbar settings, the following vbscript must be executed. It's important that the explorer.exe is terminated when restoring the settings, because else the settings are discarded.

Dim strComputer
Dim objFile, objFSO
Dim objRegistry
Dim strKeyPath, strValues, arrStrValues, arrValues
Const HKEY_CURRENT_USER = &H80000001

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop"
strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\ToolbarBackup.txt")
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strValues=objFile.ReadAll
arrStrValues=Split(strValues, vbCrLf)

ReDim arrValues(UBound(arrStrValues)-1)
for i=0 To UBound(arrStrValues)-1
arrValues(i)=CInt(arrStrValues(i))
Next

For Each Process in GetObject("winmgmts:").ExecQuery ("select * from Win32_Process where name='explorer.exe'")
Process.Terminate(0)
Next

objRegistry.SetBinaryValue HKEY_CURRENT_USER,strKeyPath, "TaskbarWinXP",arrValues

objFile.Close
Set objRegistry = Nothing
Set objFile = Nothing
Set objFSO = Nothing

4/16/2008

vbscript to check each line in a file if there are included keywords defined in another file


Dim objSourceFile, objKeywordFile, objNewFile, objFSO
Dim currentline, currentvariable, bIncluded

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFile = objFSO.OpenTextFile("D:\sourcefile.txt")
Set objKeywordFile = objFSO.OpenTextFile("D:\keywordfile.txt")
Set objNewFile = objFSO.CreateTextFile("D:\result.txt")
bIncluded = False


Do While Not objSourceFile.AtEndOfStream
currentline = objSourceFile.ReadLine
Call CheckIfIncluded
Loop



Function CheckIfIncluded

Do While Not objKeywordFile.AtEndOfStream
currentvariable = objKeywordFile.ReadLine

If InStr(currentline,currentvariable) Then
bIncluded = False
Exit Do
Else
bIncluded = True
End If
Loop
If bIncluded = True Then
objNewFile.WriteLine currentline
End If
Set objKeywordFile = Nothing
Set objKeywordFile = objFSO.OpenTextFile("D:\variablefile.txt")

End Function

3/18/2008

Unattended installation of a single Office 2007 Language Pack (MUI)

1. Copy the MUI language pack sources into your office source folder (do not replace available items)
2. In the OMUI.XX-YY folder for the specific language customize the config.xml as listed below:

3. Install the Language with the following command, where the setup.exe is the original from the office 2007 source, not from the language pack.

\\server\office\setup.exe /config \\server\office\omui.xx-yy\config.xml

3/02/2008

Office 2007: Automatically restore custom.dic

The next file to restore automatically, when migrating to Office 2007 is the custom.dic file. It contains all user self-defined words.
The Problem is that the directory for .dic files changes in Office 2007. So the following Script can be implemented in your Logon Script.
It creates the neccesary folder UProof, which is only available when Word is started the first time and then copies all .dic files from the old directory into the new UProof folder.

'Restore Custom.dic
Option Explicit

dim objFSO, objShell, objFile, objFolder
dim AppData
dim OldProofPath, NewProofPath


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

AppData = objShell.ExpandEnvironmentStrings("%APPDATA%")

OldProofPath = Appdata &"\Microsoft\Proof\"
NewProofPath = Appdata &"\Microsoft\UProof"


'*********************************************************************************************'
'First create Folder if the folder doesn't exists
If Not objFSO.FolderExists (NewProofPath) Then
objFSO.CreateFolder NewProofPath
End If
'*********************************************************************************************'

'*********************************************************************************************'
Set objFolder = objFSO.GetFolder(OldProofPath)
'If source folder does not exists...dont copy'
If objFSO.FolderExists(OldProofPath) Then
For Each objFile In objFolder.Files
'Copy all files with DIC extension'
If Right(objFile.Name,3) = "DIC" Then
objFile.Copy NewProofPath & "\" & objFile.Name
End If
Next
End If
'*********************************************************************************************'

Set objFile = Nothing
Set objFSO = Nothing
Set objFolder = Nothing

3/01/2008

Office 2007: Automatically restore AutoText entries from normal.dot

As you maybe know, there seems to be no possibility to automatically restore AutoText entries from previous Office versions to Office 2007.
Microsoft released a Workaround how to restore the AutoText entries from your normal.dot to Office 2007.
The trick is to copy the old normal.dot into one of the following folders:
%APPDATA%\Microsoft\Document Building Blocks\{Language ID}
or
%APPDATA%\Microsoft\Word\STARTUP
See also Microsoft article: http://technet2.microsoft.com/Office/en-us/library/ca7d3d97-6970-45c6-9b05-6a64cf1fbe711033.mspx?mfr=true

I see two problems in this Workaround:
1. I don't trust the STARTUP folder when i only wanna restore the AutoText entries, so i prefer the Document Building Blocks\{Language ID} folder
2. If you want to copy the normal.dot automatically for example with your Logon Script to this folder when the user logs on the first time, you can't copy the file, because the folder Document Building Blocks\{Language ID} only exists when Word is started the first time.

So....i wrote a script, which can be added to your Logonscript
The Script first creates the required folder and then copies the file!


'Restore AutoText entries from normal.dot
Option Explicit

dim objFSO, objShell
dim AppData
dim OldTemplatePath, NewTemplatePath, OldTemplate, NewTemplate
dim LanguageID

LanguageID = "1033"

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

AppData = objShell.ExpandEnvironmentStrings("%APPDATA%")

'*********************************************************************************************'
'First create Folder if Word was not started yet'
If Not objFSO.FolderExists (AppData &"\Microsoft\Document Building Blocks\" &LanguageID) Then
objFSO.CreateFolder AppData &"\Microsoft\Document Building Blocks\"
objFSO.CreateFolder AppData &"\Microsoft\Document Building Blocks\" &LanguageID
End If
'*********************************************************************************************'

'*********************************************************************************************'
'Define office template paths here'
'OldTemplate = "normal10.dot" 'If languages match the file is renamed (OfficeXP)
'OldTemplate = "normal11.dot" 'If languages match the file is renamed (Office2003)
OldTemplate = "Normal.dot"
NewTemplate = "AutoText.dot"

'OldTemplatePath = AppData &"\Microsoft\Vorlagen\" german folder
OldTemplatePath = AppData &"\Microsoft\Templates\"

'NewTemplatePath = AppData &"\Microsoft\Word\STARTUP\"
NewTemplatePath = AppData &"\Microsoft\Document Building Blocks\" &LanguageID &"\"
'*********************************************************************************************'


'*********************************************************************************************'
'Determine if the old template exists'
If objFSO.FileExists(OldTemplatePath &OldTemplate) then
'Determine if the new template already exists'
If Not objFSO.FileExists(NewTemplatePath &OldTemplate) then
objFSO.CopyFile OldTemplatePath &OldTemplate, NewTemplatePath &NewTemplate
End If
End If
'*********************************************************************************************'