Thursday, September 13, 2007

HowTo: Enum Citrix Server License using LMSTAT

Our PS4 server seems to fail to execute the license information scripts from Citrix.Com (Dependency on SDK).

So instead of relying on it, I decided to develop a script using LMSTAT and a short vb script to parse the output file and write it to a .CSV file for better viewing (Data Filtering and such...)

First I piped the result of lmstat -a to a text file then I used the script below to parse the contents then write it to a .CSV file

Code Snippet:

Parse the source file (lmstat output file): srcfile

  1. Sub GetData(srcfile)
  2. Set f = fso.GetFile(srcfile)
  3. Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
  4. Do While ts.AtEndOfStream <> True
  5. readResults = Trim(UCase(ts.ReadLine))
  6. If InStr(readResults, "/27000") Then
  7. mps = split(readResults, Chr(32))
  8. msg = mps(1) & "," & mps(3) &amp; "," & mps(4) &amp;amp; "," & mps(5) & mps(6) & mps(8) &amp;amp; " " & mps(9) &amp;amp; " " & mps(10)
  9. WriteToCSV msg, license_out
  10. End If
  11. Loop
  12. End Sub

Write to CSV function: oCsv(Output File), msg(parsed ReadLine results)


  1. Function WriteToCSV(oCsv,msg)
  2. If Not fso.FileExists(oCsv) Then fso.CreateTextFile(oCsv)
  3. Set f = fso.GetFile(oCsv)
  4. Set ts = f.OpenAsTextStream(ForAppending, TristateUseDefault)
  5. ts.Write msg & vbCrlf
  6. msg = ""
  7. ts.Close
  8. End Function
If at first you fail, call it version 1.0

Wednesday, September 12, 2007

Event Log

Event Log Manipulations:

1. Reading the Event Log
2. Clearing the Event Log
3. Creating backup of the Event Log

Code Snippet #1:

  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:" _
  3. & "{impersonationLevel=impersonate}!\\" _
  4. & strComputer & "\root\cimv2")
  5. Set colLoggedEvents = objWMIService.ExecQuery _
  6. ("Select * from Win32_NTLogEvent " _
  7. & "Where Logfile = 'System'")
  8. For Each objEvent in colLoggedEvents
  9. Wscript.Echo "Category: " &amp; objEvent.Category & VBNewLine _
  10. &amp;amp; "Computer Name: " & objEvent.ComputerName & VBNewLine _
  11. & "Event Code: " & objEvent.EventCode & VBNewLine _
  12. & "Message: " & objEvent.Message & VBNewLine _
  13. & "Record Number: " & objEvent.RecordNumber & VBNewLine _
  14. & "Source Name: " & objEvent.SourceName & VBNewLine _
  15. & "Time Written: " & objEvent.TimeWritten & VBNewLine _
  16. &amp;amp; "Event Type: " & objEvent.Type & VBNewLine _
  17. & "User: " & objEvent.User
  18. Next


Code Snippet #2:

  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:" _
  3. & "{impersonationLevel=impersonate,(Backup)}!\\" & _
  4. strComputer & "\root\cimv2")
  5. Set colLogFiles = objWMIService.ExecQuery _
  6. ("Select * from Win32_NTEventLogFile " _
  7. & "Where LogFileName='Application'")
  8. For Each objLogfile in colLogFiles
  9. objLogFile.ClearEventLog()
  10. WScript.Echo "Cleared application event log file"
  11. Next

Code Snippet #3:


  1. strComputer = "."
  2. Set objWMIService = GetObject("winmgmts:" _
  3. & "{impersonationLevel=impersonate,(Backup)}!\\" & _
  4. strComputer & "\root\cimv2")
  5. Set colLogFiles = objWMIService.ExecQuery _
  6. ("Select * from Win32_NTEventLogFile " _
  7. & "Where LogFileName='Application'")
  8. For Each objLogfile in colLogFiles
  9. errBackupLog = objLogFile.BackupEventLog( _
  10. "c:\scripts\application.evt")
  11. WScript.Echo "File saved as c:\scripts\applications.evt"
  12. Next
If at first you fail, call it version 1.0