Home > Windows > En samling VBS skript

En samling VBS skript

'______________________________________________________________________________________
' SKRIPT FÖR ATT KONFIGURERA LOTUS NOTES 8.5.1
'
' v1.00 2010-06-21 Första versionen /Tobias Holm
' v1.01 2010-06-23 Klarar nu frånvaro av NOTES.INI + skapande av mapp /Tobias Holm
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
On Error Resume Next

‘ Hämta inloggad användare & datornamn
Set oNetwork = WScript.CreateObject(“WScript.Network”)
sUSERID = oNetwork.UserName
sWKSTA = oNetwork.ComputerName
‘ Hämta fulla namnet på användaren
sDomain = oNetwork.UserDomain
sADSPath= sDomain & “/” & sUSERID
Set oUser = GetObject(“WinNT://” & sADSPath & “,user”)
sFULLNAME = oUser.FullName

‘ Hämta TEMP-mapp
Set oShell = WScript.CreateObject(“WScript.Shell”)
sTemp = oShell.ExpandEnvironmentStrings( “%TEMP%” )

lRet = Logg(sTemp, “\KonfigNotes.LOG”, “————————————-“)
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Konfigurerar Notes 8”)
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “User: ”  & sUSERID)
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Name: ”  & sFULLNAME)
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Comp: ”  & sWKSTA)

‘ Kolla programmapp
sDPF = RegRead(“HKLM\Software\Microsoft\Windows\CurrentVersion”, “ProgramFilesDir”)

‘ Kolla var Notes.EXE finns
sRet = RegRead(“HKLM\SOFTWARE\Classes\.nsf\shell\Open\Command”, “”)
sNPATH = Mid(sRet, 2, InStr(sRet, “\notes.exe”)-2)

‘ Kolla OM Notes.EXE finns
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
If Not oFSO.FileExists(sNPATH & “\nlnotes.exe”) Then
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Hittar inte ” & Chr(34) & _
sNPATH & “\nlnotes.exe” & Chr(34))
WScript.Quit(0)
End If

‘ Kolla var Notes datafiler finns. WinXP
sDLS = RegRead(“HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”, “Local Settings”)
If sDLS = “” Then
‘ eller Win7
sDLS = RegRead(“HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders”, “Local AppData”)
End If
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Local AppData ” & Chr(34) & _
sDLS & Chr(34))

‘ Skapa Notes datamapp om den inte finns
lRet = MkDir(sDLS & “\Application Data\Lotus\Notes\Data”)

sID = sUSERID & “.id”
sDPATH = sDLS & “\Application Data\Lotus\Notes\Data”
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Letar datafiler i ” & Chr(34) & _
sDPATH & Chr(34))

‘ Kolla om vi ska konfigurera NOTES.INI
bConfig = False
If oFSO.FileExists(sDPATH & “\notes.ini”) Then
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Kollar om NOTES.INI konfig read är utförd”)
If ReadINI(sDPATH & “\notes.ini”, “ITService”, “Config”) <> “Yes” Then
bConfig = True
End If
Else
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “NOTES.INI finns inte”)
bConfig = True
End If
If bConfig = True Then
‘ Ta en säkerhetskopia på befintlig ID-fil
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Säkerhetskopierar befintlig ID-fil”)
oFSO.CopyFile sDPATH & “\” & sID, _
sDPATH & “\” & sUSERID & “.föreAutoConfig.id”

‘ Kopiera in en ny ID-fil
sSrc = “\\server\idfiler\” & sID
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Letar ” & sSrc)
If oFSO.FileExists(sSrc) Then
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Hittade ID ” & Chr(34) & _
sSrc & Chr(34))
oFSO.CopyFile sSrc, sDPATH & “\” & sID, True
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Kopierar ” & sSrc & ” -> ” & sDPATH & “\” & sID)
End If

sSrc = “M:\Backup ” & sWKSTA & “\NOTES\” & sID
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Letar ” & sSrc)
If oFSO.FileExists(sSrc) Then
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Hittade ID ” & Chr(34) & _
sSrc & Chr(34))
oFSO.CopyFile sSrc, sDPATH & “\” & sID, True
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Kopierar ” & sSrc & ” -> ” & sDPATH & “\” & sID)
End If

‘ Kollar om ID-fil finns, avsluta om filen inte finns
If Not oFSO.FileExists(sDPATH & “\” & sID) Then
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Hittade ingen ID!”)
WScript.Quit(0)
End If

sX = sTEMP & “\notesini.txt”
WriteINI sX, “Notes”, “Username”, sFULLNAME & “/Company”
WriteINI sX, “Notes”, “KeyfileName”, sDPATH & “\” & sID
WriteINI sX, “Notes”, “Domino.Name”, “server”
WriteINI sX, “Notes”, “Domino.Address”, “server”
WriteINI sX, “Notes”, “Domino.Port”, “TCPIP”
WriteINI sX, “Notes”, “Domino.Server”, “1”
WriteINI sX, “Notes”, “AdditionalServices”, “0”
WriteINI sX, “Notes”, “Replication.Schedule”, “1”

‘ Ta en säkerhetskopia på NOTES.INI
oFSO.CopyFile sDPATH & “\notes.ini”, sDPATH & “\notes.föreAutoConfig.ini”
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Konfigurerar Notes.INI”)

‘ Aktivera automatisk Notes konfigurering
sX = sDPATH & “\notes.ini”
WriteINI sX, “Notes”, “ConfigFile”, sTEMP & “\notesini.txt”
WriteINI sX, “Notes”, “IM_NO_SETUP”, “1”
WriteINI sX, “Notes”, “NAMES”, “names.nsf, companyadressbook.nsf”
WriteINI sX, “Notes”, “ExitNotesPrompt”, “1”
WriteINI sX, “Notes”, “MAXIMIZED”, “1”
WriteINI sX, “Notes”, “UserInterface”, “sv”
‘ Slå upp namn i alla adressböcker
WriteINI sX, “Notes”, “LocAllExhaustiveNameLookup”, “1”
‘ Svensk stavningskontroll
WriteINI sX, “Notes”, “SPELL_LANG”, “1053”
WriteINI sX, “Notes”, “SPELL_PREFERENCES”, “9”

‘ Flagga att automatisk Notes konfigurering är genomförd
WriteINI sX, “ITService”, “Config”, “Yes”
Else
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Gjorde ingen NOTES.INI konfig!”)
End If

lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Klart!”)
WScript.Quit(0)

‘______________________________________________________________________________________
‘ SKAPA MAPP REKURSIVT / Tobias Holm 2010-06-21

‘ Ex: lRet = MkDir(“C:\TEMP\UNDERMAPP”)
‘¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function MkDir(x_sMapp)
Set x_oFSO = CreateObject(“Scripting.FileSystemObject”)

x_lPos = 4
x_lFound = InStr(x_lPos, x_sMapp, “\”)
While x_lFound > 0
x_sFolder = mid(x_sMapp, 1, x_lFound-1)
If Not x_oFSO.FolderExists(x_sFolder) Then
x_oFSO.CreateFolder(x_sFolder)
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Skapade mapp ” & Chr(34) & _
x_sFolder & Chr(34) & ” – Err:” & Err)
End If
x_lPos = x_lFound+1
x_lFound = InStr(x_lPos, x_sMapp, “\”)
Wend
If Not x_oFSO.FolderExists(x_sMapp) Then
x_oFSO.CreateFolder(x_sMapp)
lRet = Logg(sTemp, “\KonfigNotes.LOG”, “Skapade mapp ” & Chr(34) & _
x_sMapp & Chr(34) & ” – Err:” & Err)
End If
If Err Then
MkDir = Err.Description
Else
MkDir = 0
End If

Set x_oFSO = Nothing
End Function

‘______________________________________________________________________________________
‘ LÄS REGISTRET / Tobias Holm 2010-06-17

‘ Klarar REG_SZ och DWORD
‘ Ex: lRet = RegRead(“HKCU\Software\Intel”, “Main”)
‘ Returnerar “~err” om inte Reg-root specificeras eller vid läsfel.
‘¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function RegRead(x_sPath, x_sKey)
‘ Search for Reg-root
x_Root = 0
Select Case Left(x_sPath, 4)
Case “HKCU”
x_Root = &H80000001
Case “HKLM”
x_Root = &H80000002
End Select

If x_Root <> 0 Then
Err.Clear
x_sValueD = “”
x_strComputer = “.”
Set x_oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” &_
x_strComputer & “\root\default:StdRegProv”)

x_sPath = Right(x_sPath, Len(x_sPath)-5)
x_oReg.GetStringValue x_Root, x_sPath, x_sKey, x_sValue
x_oReg.GetDWORDValue x_Root, x_sPath, x_sKey, x_sValueD

If x_sValueD <> “” Then
x_sValue = x_sValueD
End If

If Err = 0 Then
RegRead = x_sValue
Else
RegRead = “~err”
End If
Else
RegRead = “~err”
End If
End Function

‘______________________________________________________________________________________
‘ SKRIV TILL REGISTRET / Tobias Holm 2010-06-17

‘ Klarar REG_SZ och DWORD
‘ Ex: lRet = RegWrite(“HKCU\Software\Intel”, “Main”, “KALLE”, “REG_SZ”)
‘ Ex: lRet = RegWrite(“HKCU\Software\Intel”, “Count”, “35”, “DWORD”)
‘ Returnerar “~err” om inte Reg-root specificeras. Vid skrivfel returneras felkoden.
‘¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function RegWrite(x_sPath, x_sKey, x_sValue, x_sType)
‘ Search for Reg-root
x_Root = 0
Select Case Left(x_sPath, 4)
Case “HKCU”
x_Root = &H80000001
Case “HKLM”
x_Root = &H80000002
End Select

If x_Root <> 0 Then
Err.Clear
x_strComputer = “.”
Set x_oReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” &_
x_strComputer & “\root\default:StdRegProv”)

x_sPath = Right(x_sPath, Len(x_sPath)-5)
Select Case UCase(x_sType)
Case “REG_SZ”
x_oReg.SetStringValue x_Root, x_sPath, x_sKey, x_sValue
Case “DWORD”
x_oReg.SetDWORDValue x_Root, x_sPath, x_sKey, x_sValue
End Select

RegWrite = Err
Else
RegWrite = “~err”
End If
End Function

‘______________________________________________________________________________________
‘ SKRIV LOGGFIL / Tobias Holm 2010-06-21

‘ Ex: lRet = Logg(“C:\Logs”, “\Fil.LOG”, “Inga fel”)
‘ Returnerar ev felkod under körning.
‘¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function Logg(x_sPath, x_sFile, x_sText)
Set x_oFSO = CreateObject(“Scripting.FileSystemObject”)

‘ Check that the strDirectory folder exists
If x_oFSO.FolderExists(x_sPath) Then
Set x_oFolder = x_oFSO.GetFolder(x_sPath)
Else
Set x_oFolder = x_oFSO.CreateFolder(x_sPath)
End If

If x_oFSO.FileExists(x_sPath & x_sFile) Then
Set x_oFolder = x_oFSO.GetFolder(x_sPath)
Else
Set x_oFile = x_oFSO.CreateTextFile(x_sPath & x_sFile)
End If
Set x_oFile = Nothing
Set x_oFolder = Nothing

‘ForAppending = 8, ForReading = 1, ForWriting = 2
Set objTextFile = x_oFSO.OpenTextFile(x_sPath & x_sFile, 8, True)

‘ Writes strText every time you run this VBScript
objTextFile.WriteLine(Date() & ” ” & Time() & ” ” & x_sText)
objTextFile.Close

Logg = Err.Number
Set x_oFSO = Nothing
End Function

‘______________________________________________________________________________________
‘ LÄS INI / http://www.robvanderwoude.com/vbstech_files_ini.php
‘¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Function ReadIni( myFilePath, mySection, myKey )
‘ This function returns a value read from an INI file

‘ Arguments:
‘ myFilePath  [string]  the (path and) file name of the INI file
‘ mySection   [string]  the section in the INI file to be searched
‘ myKey       [string]  the key whose value is to be returned

‘ Returns:
‘ the [string] value for the specified key in the specified section

‘ CAVEAT:     Will return a space if key exists but value is blank

‘ Written by Keith Lacelle
‘ Modified by Denis St-Pierre and Rob van der Woude

Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8

Dim intEqualPos
Dim objFSO, objIniFile
Dim strFilePath, strKey, strLeftString, strLine, strSection

Set objFSO = CreateObject( “Scripting.FileSystemObject” )

ReadIni     = “”
strFilePath = Trim( myFilePath )
strSection  = Trim( mySection )
strKey      = Trim( myKey )

If objFSO.FileExists( strFilePath ) Then
Set objIniFile = objFSO.OpenTextFile( strFilePath, ForReading, False )
Do While objIniFile.AtEndOfStream = False
strLine = Trim( objIniFile.ReadLine )

‘ Check if section is found in the current line
If LCase( strLine ) = “[” & LCase( strSection ) & “]” Then
strLine = Trim( objIniFile.ReadLine )

‘ Parse lines until the next section is reached
Do While Left( strLine, 1 ) <> “[”
‘ Find position of equal sign in the line
intEqualPos = InStr( 1, strLine, “=”, 1 )
If intEqualPos > 0 Then
strLeftString = Trim( Left( strLine, intEqualPos – 1 ) )
‘ Check if item is found in the current line
If LCase( strLeftString ) = LCase( strKey ) Then
ReadIni = Trim( Mid( strLine, intEqualPos + 1 ) )
‘ In case the item exists but value is blank
If ReadIni = “” Then
ReadIni = ” ”
End If
‘ Abort loop when item is found
Exit Do
End If
End If

‘ Abort if the end of the INI file is reached
If objIniFile.AtEndOfStream Then Exit Do

‘ Continue with next line
strLine = Trim( objIniFile.ReadLine )
Loop
Exit Do
End If
Loop
objIniFile.Close
Else
WScript.Echo strFilePath & ” doesn’t exists. Exiting…”
Wscript.Quit 1
End If
End Function

‘______________________________________________________________________________________
‘ SKRIV INI / http://www.robvanderwoude.com/vbstech_files_ini.php
‘¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Sub WriteIni( myFilePath, mySection, myKey, myValue )
‘ This subroutine writes a value to an INI file

‘ Arguments:
‘ myFilePath  [string]  the (path and) file name of the INI file
‘ mySection   [string]  the section in the INI file to be searched
‘ myKey       [string]  the key whose value is to be written
‘ myValue     [string]  the value to be written (myKey will be
‘                       deleted if myValue is <DELETE_THIS_VALUE>)

‘ Returns:
‘ N/A

‘ CAVEAT:     WriteIni function needs ReadIni function to run

‘ Written by Keith Lacelle
‘ Modified by Denis St-Pierre, Johan Pol and Rob van der Woude

Const ForReading   = 1
Const ForWriting   = 2
Const ForAppending = 8

Dim blnInSection, blnKeyExists, blnSectionExists, blnWritten
Dim intEqualPos
Dim objFSO, objNewIni, objOrgIni, wshShell
Dim strFilePath, strFolderPath, strKey, strLeftString
Dim strLine, strSection, strTempDir, strTempFile, strValue

strFilePath = Trim( myFilePath )
strSection  = Trim( mySection )
strKey      = Trim( myKey )
strValue    = Trim( myValue )

Set objFSO   = CreateObject( “Scripting.FileSystemObject” )
Set wshShell = CreateObject( “WScript.Shell” )

strTempDir  = wshShell.ExpandEnvironmentStrings( “%TEMP%” )
strTempFile = objFSO.BuildPath( strTempDir, objFSO.GetTempName )

Set objOrgIni = objFSO.OpenTextFile( strFilePath, ForReading, True )
Set objNewIni = objFSO.CreateTextFile( strTempFile, False, False )

blnInSection     = False
blnSectionExists = False
‘ Check if the specified key already exists
blnKeyExists     = ( ReadIni( strFilePath, strSection, strKey ) <> “” )
blnWritten       = False

‘ Check if path to INI file exists, quit if not
strFolderPath = Mid( strFilePath, 1, InStrRev( strFilePath, “\” ) )
If Not objFSO.FolderExists ( strFolderPath ) Then
WScript.Echo “Error: WriteIni failed, folder path (” _
& strFolderPath & “) to ini file ” _
& strFilePath & ” not found!”
Set objOrgIni = Nothing
Set objNewIni = Nothing
Set objFSO    = Nothing
WScript.Quit 1
End If

While objOrgIni.AtEndOfStream = False
strLine = Trim( objOrgIni.ReadLine )
If blnWritten = False Then
If LCase( strLine ) = “[” & LCase( strSection ) & “]” Then
blnSectionExists = True
blnInSection = True
ElseIf InStr( strLine, “[” ) = 1 Then
blnInSection = False
End If
End If

If blnInSection Then
If blnKeyExists Then
intEqualPos = InStr( 1, strLine, “=”, vbTextCompare )
If intEqualPos > 0 Then
strLeftString = Trim( Left( strLine, intEqualPos – 1 ) )
If LCase( strLeftString ) = LCase( strKey ) Then
‘ Only write the key if the value isn’t empty
‘ Modification by Johan Pol
If strValue <> “<DELETE_THIS_VALUE>” Then
objNewIni.WriteLine strKey & “=” & strValue
End If
blnWritten   = True
blnInSection = False
End If
End If
If Not blnWritten Then
objNewIni.WriteLine strLine
End If
Else
objNewIni.WriteLine strLine
‘ Only write the key if the value isn’t empty
‘ Modification by Johan Pol
If strValue <> “<DELETE_THIS_VALUE>” Then
objNewIni.WriteLine strKey & “=” & strValue
End If
blnWritten   = True
blnInSection = False
End If
Else
objNewIni.WriteLine strLine
End If
Wend

If blnSectionExists = False Then ‘ section doesn’t exist
objNewIni.WriteLine
objNewIni.WriteLine “[” & strSection & “]”
‘ Only write the key if the value isn’t empty
‘ Modification by Johan Pol
If strValue <> “<DELETE_THIS_VALUE>” Then
objNewIni.WriteLine strKey & “=” & strValue
End If
End If

objOrgIni.Close
objNewIni.Close

‘ Delete old INI file
objFSO.DeleteFile strFilePath, True
‘ Rename new INI file
objFSO.MoveFile strTempFile, strFilePath

Set objOrgIni = Nothing
Set objNewIni = Nothing
Set objFSO    = Nothing
Set wshShell  = Nothing
End Sub

Categories: Windows Tags:
  1. No comments yet.
  1. No trackbacks yet.