scripts.team-holm.net
Hovedsiden
VisualBasic Scripts
Enterprise Scripts


Noen praktiske scripts i større miljøer. Scriptene som finnes her er:
  • getDesktopServers.vbs - lister ut alle servere tilhørende en publisert desktop
  • checkForHotfix.vbs - leter etter en hotfix på alle servere i en farm og lister ut status
  • logon2Loadx32.vbs - logger en bruker automatisk på alle servere i en publisert desktop

  • getDesktopServers.vbs
    '** Atle Holm 03.03.2012 - atle@team-holm.net for feedback.
    '** Vanligvis vil qfarm /app desktopNavn liste alle servere i en desktop, 
    '** men noen ganger ønsker mann å bare få servernavnene og ikke alt det andre med.
    '** Feks når man skal bruke listen i andre scripts.
    '
    'Brukes slik:
    'cscript getDesktopServers.vbs 'Desktop Name' generertListe.txt
    'Feks:
    'cscript getDesktopServers.vbs 102910 generertListe.txt
    '
    'Da får man både skrevet servernavnene ut i cmd vindu, og man får de listet i generertListe.txt
    
    Option Explicit
    
    Dim oShell, oDate, oFSO, oLogFile, oResultFile, iReturn, iCounter, sDesktop, sLogPath, sUntreatedString, sListPath
    Const ForReading = 1
    Const ForAppending = 8
    set oShell = wscript.createObject("wscript.shell")
    
    If WScript.Arguments.Count <> 2 Then
       Wscript.Echo "Usage: "
       Wscript.Echo "cscript getDesktopServers.vbs 'Desktop Name' generertListe.txt"
       Wscript.Quit
    End If
    
    Call forceUseCScript
    
    oDate = date()
    sDesktop = WScript.Arguments(0)
    sListPath = WScript.Arguments(1)
    sLogPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
    
    set oFSO = CreateObject("Scripting.FileSystemObject")
    
    Wscript.Echo "Date: " & oDate
    Wscript.Echo "Fetching servers from: " & sDesktop
    Wscript.Echo "Saving server list in file: " & sListPath
    Wscript.Echo "Logpath: " & sLogPath
    Wscript.Echo "Arguments are: '" & sDesktop & "' & '" & sListPath & "'"
    Wscript.Sleep 3000
    
    iReturn = oShell.Run("cmd.exe /c qfarm.exe /app """ & sDesktop & """ > """ & sLogPath & "\citrixServersUnTreated.txt""",1,True)
    set oLogFile = oFSO.OpenTextFile(sLogPath & "\citrixServersUnTreated.txt", ForReading)
    set oResultFile = oFSO.OpenTextFile(sLogPath & "\" & sListPath, ForAppending, True)
    iCounter = 0
    
    Wscript.echo vbCrlf & "Servers are in " & sDesktop & " are: "
    Do Until oLogFile.AtEndOfStream
       Dim arraySplit, strChars, i
       sUntreatedString = oLogFile.ReadLine
       iCounter = iCounter + 1
       If iCounter >= 5 Then
          For i = 39 to 2 Step -1
             strChars = Space(i)
             sUntreatedString = Replace(sUntreatedString, strChars, ";")
          Next
          arraySplit = split(sUntreatedString, ";")
          WScript.Echo arraySplit(1)
          oResultFile.WriteLine(arraySplit(1))
       End If
    Loop
    oLogFile.close
    oResultFile.close
    oFSO.DeleteFile sLogPath & "\citrixServersUnTreated.txt"
    
    Sub forceUseCScript()   
       If Not WScript.FullName = WScript.Path & "\cscript.exe" Then      
          oShell.Popup "Startet ved bruk av WScript. Starter igjen...",3,"WSCRIPT"
          oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName _
          & Chr(34) & " " & WScript.Arguments(0) & " " & WScript.Arguments(1),1,False
          WScript.Quit 0
       End If
    End Sub 

    logon2Loadx32.vbs
    '** Atle Holm - 02.08.2012 - ta kontakt på atle.@team-holm.net for informasjon 
    '** eller tilbakemeldinger. Scriptet logger en bruker
    '** automatisk på alle servere i en publisert desktop.
    '** NB! Pass på at sWFICAFile variabelen er satt til rett  
    '** plassering av wfcrun32.exe - scriptet er satt opp med standard plassering.
    
    'Scriptet brukes slik:
    'cscript logon2Load.vbs 'Desktop Name' domain\username password
    
    'Pseudo:
    '1. Ta imot brukernavn og passord
    '2. Generer liste over servere med qfarm /app desktop
    '3. Generer ICA filer for disse
    '4. Kjør hver ICA fil med wfica32.exe eller start.exe
    
    Option Explicit
    
    Dim oShell, oDate, oFSO, oLogFile, oResultFile, iReturn, iCounter, sDesktop, sLogPath
    Dim sUntreatedString, sUserCreds, sPassCreds
    Dim sUntreated, sTreated, sWFICAFile
    
    sUntreated = "\citrixServersUnTreated.txt"
    sTreated = "\citrixServersTreated.txt"
    sWFICAFile = "C:\Program Files\Citrix\ICA Client\wfcrun32.exe"
    
    Const ForReading = 1
    Const ForAppending = 8
    set oShell = wscript.createObject("wscript.shell")
    
    If WScript.Arguments.Count <> 3 Then
       Wscript.Echo "Usage: "
       Wscript.Echo "cscript logon2Load.vbs 'Desktop Name' domain\username password"
       Wscript.Quit
    End If
    oDate = date()
    
    Call forceUseCScript
    
    '1. Ta imot brukernavn og passord
    sDesktop = WScript.Arguments(0)
    sUserCreds = WScript.Arguments(1)
    sPassCreds = WScript.Arguments(2)
    sLogPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
    
    set oFSO = CreateObject("Scripting.FileSystemObject")
    
    Wscript.Echo "Date: " & oDate
    Wscript.Echo "Fetching servers from: " & sDesktop
    Wscript.Echo "Logpath and temporary folder: " & sLogPath
    Wscript.Echo "Arguments are: '" & sDesktop & "' & '" & sUserCreds & "' & '" & sPassCreds & "'"
    Wscript.Sleep 3000
    
    iReturn = oShell.Run("cmd.exe /c " & Chr(34) & "qfarm.exe /app """ & sDesktop & """ > """ & sLogPath _
    & sUntreated & """"  & Chr(34),1,True)
    set oLogFile = oFSO.OpenTextFile(sLogPath & sUntreated, ForReading)
    set oResultFile = oFSO.OpenTextFile(sLogPath & sTreated, ForAppending, True)
    iCounter = 0
    
    Wscript.echo vbCrlf & "Servers are in " & sDesktop & " are: "
    
    '2. Generer liste over servere med qfarm /app desktop
    Do Until oLogFile.AtEndOfStream
       Dim arraySplit, strChars, i
       sUntreatedString = oLogFile.ReadLine
       iCounter = iCounter + 1
       If iCounter >= 5 Then
          For i = 39 to 2 Step -1
             strChars = Space(i)
             sUntreatedString = Replace(sUntreatedString, strChars, ";")
          Next
          arraySplit = split(sUntreatedString, ";")
          WScript.Echo "   " & arraySplit(1)
          oResultFile.WriteLine(arraySplit(1))
       End If
    Loop
    oLogFile.close
    oResultFile.close
    oFSO.DeleteFile sLogPath & sUntreated
    set oResultFile = oFSO.OpenTextFile(sLogPath & sTreated, ForReading, True)
    
    '3. Generer ICA filer for disse
    Wscript.Echo ""
    Do Until oResultFile.AtEndOfStream
       Dim sServer, sIcaFile, oIcaFile
    
       sServer = oResultFile.ReadLine
    
       sIcaFile = "[WFClient]" & vbCrLf & "Version=2" & vbCrLf & "HttpBrowserAddress=" & sServer & vbCrLf & "LocTcpBrowserAddress=" _ 
       & sServer & vbCrLf & "LocTcpBrowserAddress2=" & sServer & vbCrLf & "ProxyType=Auto" & vbCrLf & "ConnectionBar=1" _
       & vbCrLf & "[ApplicationServers]" & vbCrLf & sServer & "=" & vbCrLf & "[" & sServer & "]" & vbCrLf & "Address=" _
       & sServer & vbCrLf & "ClientAudio=On" & vbCrLf & "AudioBandwidthLimit=1" & vbCrLf & _
       "CDMAllowed=On" & vbCrLf & "CPMAllowed=On" & vbCrLf & "username=" & sUserCreds & vbCrLf & "Password=" & vbCrLf _
       & "ClearPassword=" & sPassCreds & vbCrLf & "DesiredColor=8" & vbCrLf & "ConnectionBar=1" & vbCrLf & "TWIMode=Off" _
       & vbCrLf & "ScreenPercent=90" & vbCrLf & "Compress=On" & vbCrLf & "TransportDriver=TCP/IP" _
       & vbCrLf & "WinStationDriver=ICA 3.0" & vbCrLf & "BrowserProtocol=HTTPonTCP" & vbCrLf & "[Compress]" _
       & vbCrLf & "DriverName= PDCOMP.DLL" & vbCrLf & "DriverNameWin16= PDCOMPW.DLL" & vbCrLf & "DriverNameWin32= PDCOMPN.DLL"
    
       set oIcaFile = oFSO.OpenTextFile(sLogPath & "\" & sServer & ".ica", ForAppending, True)
       oIcaFile.WriteLine(sIcaFile)
       '4. Kjør hver ICA fil med wfica32.exe eller start.exe
       Wscript.Echo "Generer ICA fil for " & sServer & " og logger inn bruker " & sUserCreds & ":"
       iReturn = oShell.Run("cmd.exe /c """ & """" & sWFICAFile & """" & " """ & sLogPath & "\" & sServer & ".ica" & """""",0,False)
       If iReturn = 0 Then
          Wscript.Echo "   -->OK!"
       Else
          Wscript.Echo "    -->Unknown error!"
       End If
       Wscript.Sleep 0800
       oIcaFile.close
       oFSO.DeleteFile sLogPath & "\" & sServer & ".ica"
    Loop
    oResultFile.close
    oFSO.DeleteFile sLogPath & sTreated
    
    
    Sub forceUseCScript()
       If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
          oShell.Popup "Startet ved bruk av WScript. Starter igjen...",3,"WSCRIPT"
          oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34) _
          & " " & WScript.Arguments(0) & " " & WScript.Arguments(1) & " " & WScript.Arguments(2),1,False
          WScript.Quit 0
       End If
    End Sub

    checkForHotfix.vbs
    '** Atle Holm - 28.03.2012 - Scriptet leter etter en hotfix i farmen og lister ut to logger,    **
    '** en for servere der denne er funnet og en for servere der denne ikke er funnet.            **
    '** Kjøring av scriptet forutsetter at det kjøres i en Citrix farm hvor det skal sjekkes       **
    '** for hotfix.                                                               **
    
    'Pseudo:
    '1. Ta imot hotfiks det skal søkes etter som argument av typen KBxxxxxx
    '2. Søke opp alle server i en farm
    '3. Søke etter hotfikses på alle servere
    '4. Lage liste med servere som ikke har spesifisert hotfiks
    
    Option Explicit
    On Error Resume Next
    
    Dim oShell, oDate, oFSO, oLogFile, oResultFile1, oResultFile2, iReturn, iCounter, sPatchKB, sLogPath
    Dim sUntreatedString, sListPath, strComputer, colQuickFixes, objWMIService, objQuickFix, iFound
    Const ForReading = 1
    Const ForAppending = 8
    Set oShell = wscript.createObject("wscript.shell")
    
    Call forceUseCScript
    
    If WScript.Arguments.Count <> 2 Then
       Wscript.Echo "Usage: "
       Wscript.Echo "cscript checkForHotfix.vbs KB123456 loggliste.txt"
       Wscript.Quit
    End If
    
    oDate = date()
    '1. Ta imot hotfiks det skal søkes etter som argument av typen KBxxxxxx
    sPatchKB = WScript.Arguments(0)
    sListPath = WScript.Arguments(1)
    sLogPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    '2. Søke opp alle server i en farm
    iReturn = oShell.Run("cmd.exe /c qfarm.exe /CONTINUE > """ & sLogPath & "\citrixServersUnTreated.txt""",1,True)
    Set oLogFile = oFSO.OpenTextFile(sLogPath & "\citrixServersUnTreated.txt", ForReading)
    Set oResultFile1 = oFSO.OpenTextFile(sLogPath & "\" & sListPath & "-FoundServers.log", ForAppending, True)
    Set oResultFile2 = oFSO.OpenTextFile(sLogPath & "\" & sListPath & "-NotFoundServers.log", ForAppending, True)
    iCounter = 0
    
    Wscript.Echo "Running Script: " & WScript.scriptFullName
    Wscript.Echo "Date: " & oDate
    Wscript.Echo "Saving server list in file: " & sListPath
    Wscript.Echo "Logpath: " & sLogPath
    Wscript.Echo "Arguments are: '" & sPatchKB & "' & '" & sListPath & "'"
    Wscript.Sleep 3000
    
    '3. Søke etter hotfikses på alle servere
    Do Until oLogFile.AtEndOfStream
       Dim arraySplit, strChars, i
       sUntreatedString = oLogFile.ReadLine
       iCounter = iCounter + 1
       If iCounter >= 5 Then
          For i = 39 to 2 Step -1
             strChars = Space(i)
             sUntreatedString = Replace(sUntreatedString, strChars, " ")
          Next
          arraySplit = split(sUntreatedString, " ")
          WScript.Echo "Sjekker: " & arraySplit(0)
          strComputer = arraySplit(0)
          Err.Clear
          Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
          Set colQuickFixes = objWMIService.ExecQuery ("Select * from Win32_QuickFixEngineering")
          If  Err.Number <> 0  Then   
             ' An exception occurred
             oResultFile2.WriteLine("Exception when retreiving information from server " & strComputer & ":" & vbCrLf _
                & "    Error number: " & Err.Number & vbCrLf & "    Error description: '" & Err.Description & vbCrLf)
          Else
             iFound = 0
             For Each objQuickFix In colQuickFixes
                If objQuickFix.HotFixID<>"File 1" And StrComp(objQuickFix.HotFixID, sPatchKB, 1) = 0 Then 
                   Wscript.Echo objQuickFix.HotFixID & " found at server " & strComputer
                   If Not isNULL(objQuickFix.Description) Then Wscript.Echo "Description: " & objQuickFix.Description
                   If Not isNULL(objQuickFix.InstallDate) Then Wscript.Echo "Installation date is: " & objQuickFix.InstallDate
                   If Not isNULL(objQuickFix.InstalledBy) Then Wscript.Echo "Installed By: " & objQuickFix.InstalledBy
                   oResultFile1.WriteLine(vbCrlf & objQuickFix.HotFixID & " found at server " & strComputer)
                   If Not isNULL(objQuickFix.Description) Then oResultFile1.WriteLine(vbTab & "Description: " & objQuickFix.Description)
                   If Not isNULL(objQuickFix.InstallDate) Then oResultFile1.WriteLine(vbTab & "Installation date is: " & objQuickFix.InstallDate)
                   If Not isNULL(objQuickFix.InstalledBy) Then oResultFile1.WriteLine(vbTab & "Installed By: " & objQuickFix.InstalledBy)
                   iFound = 1
                End If
             Next
             If (iFound = 0) Then
                '4. Lage liste med servere som ikke har spesifisert hotfiks
                oResultFile2.WriteLine(strComputer & ": Did not find " & sPatchKB)
             End If
          End If
       End If
    Loop
    
    oLogFile.close
    oResultFile1.close
    oResultFile2.close
    oFSO.DeleteFile sLogPath & "\citrixServersUnTreated.txt"
    
    Sub forceUseCScript()   
       If Not WScript.FullName = WScript.Path & "\cscript.exe" Then      
          oShell.Popup "Startet ved bruk av WScript. Starter igjen...",3,"WSCRIPT"
          oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO " & Chr(34) & WScript.scriptFullName & Chr(34) _
             & " " & WScript.Arguments(0) & " " & WScript.Arguments(1),1,False
          WScript.Quit 0
       End If
    End Sub 

    Perl
    VisualBasic
    BASH
    Powershell