An Analog Man in a Digital World

The ramblings of an old IT Pro travelling the digital byways.

Tuesday, June 28, 2016

Convert string to date and time in PowerShell

Sometimes, the output of a command will include date and time information as a system.string, but you need it in a true datetime format.  Simple solution.

In this case the queueTime property of my $objItem object is a system.string.

$objItem.queueTime

Output:
2016-06-17T13:27:22.3998361Z

Just use the [datetime] accelerator to convert the string to datetime object

[datetime]$objItem.queueTime

Output:
Friday, June 17, 2016 8:27:22 AM


Want to take it a step further?  Want to format that datetime?

"{0:MM/dd/yyyy HH:mm:ss}" -f [datetime]$objItem.queueTime

Output:
06/17/2016 08:27:22

Tuesday, June 21, 2016

Move Azure Resources

Sometimes it becomes necessary to move an Azure resource from one resource group to another within a subscription.  While this is technically not difficult, it can be a bit of a pain.  Let's let the computers do what they do best.

Following is a quick PowerShell script.  It assumes you've already authenticated to your subscription (Login-AzureRmAccount) and selected the appropriate context (Select-AzureRMSubscription).


<#
.NOTES
===================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.122
Created on:   5/19/2016 3:11 PM
Created by:   TowerBE
Organization: XXXXXXX
Filename:             move-resource.ps1
===================================================================
.SYNOPSIS
Moves an Azure Resource.

.DESCRIPTION
A quick and dirty script to move an Azure resource from one Resource
Group to another

.PARAMETER resName
The name of the Azure Resource to be moved.

.PARAMETER oldRg
The name of the Azure Resource Group where the resource currently exists.

.PARAMETER newRg
The name of the Azure Resource Group where you're moving the resource.

.EXAMPLE
move-resource.ps1 -resName "myservicebus" -oldRg "busted" -newRg "hotness"
This will move the resource myservicebus from the busted resource group
to the hotness resource group.
#>

[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[string]$resName,
[Parameter(Mandatory = $true)]
[string]$oldRg,
[Parameter(Mandatory = $true)]
[string]$newRg
)

$resource = Get-AzureRmResource -ResourceName "$resName" -ResourceGroupName "$oldRg"
Move-AzureRmResource -DestinationResourceGroupName "$newRg" -ResourceId $resource.ResourceId

Please note that not all resources can be moved, and not all resources support moving fully.  I've used this script for moving storage accounts, service bus namespaces and other resources types, but don't make any guarantees. If you want to read more about moving resources, go to https://azure.microsoft.com/en-us/documentation/articles/resource-group-move-resources/.

Sunday, June 12, 2016

Reload PowerShell Profile

When editing your PowerShell profile, you need to reload the profile to check or implement your changes.  There are three ways to accomplish this.


  1. Start a new PowerShell session
  2. Type the command & $Profile
  3. Type the command . $Profile


Way one is inconvenient.
Way two invokes the $Profile script.
Way three sources the $Profile script.

Are two and three really different?  Dunno, the end result is the same.

Saturday, June 11, 2016

Persisting Azure RM Credentials: Profile switch in PowerShell

I manage a number of Azure subscriptions for my employer. One of the things that used to drive me nuts is having to constantly go through the Login process, especially since we use Multi Factor Authentication, and then having to switch back and forth between subscriptions.

I recently stumbled across an blog about the Save-AzureRmProfile command. Did I mention I'm relatively new to Azure? While blog provided useful information and simplified things for me, I took it one step further (have to give my buddy Rick credit for sparking the idea).

 I saved a profile file for each one of my subscriptions,

    Save-AzureRmProfile -Path C:\users\Brian\Documents\Azure\Profiles\AzureRmDevProfile.json 

then edited my PowerShell profile to include a function for each profile (hint: if you don't have a PowerShell profile, New-Item –type file –force $profile).

    function goDev { 
        Select-AzureRmProfile -Path C:\users\Brian\Documents\Azure\Profiles\AzureRmDevProfile.json 
    }

Now, if I want to switch to my development subscription while in PowerShell, I simply type "goDev" and I'm there. I have a separate go function for each of the subscriptions I manage.

Saving my Azure profiles and modifying my PowerShell profile: 20 minutes.
Eliminating yet another pesky annoyance: Priceless!

Friday, March 5, 2010

Computer Repair Problem

You know it's gonna be a long day at the old Tech Support line when the day starts like this!


Caller:  Hi, our printer is not working.

Customer Service:  What seems to be the problem?

Caller:  Umm, the mouse is jammed.


Customer Service:  Mouse?  Printers don't have a mouse.


Caller:  Mmmmmmmmmm?  Really?   I'll send you a picture.






Sunday, October 26, 2008

More Fun with Windows Printers

Okay, so I suck at posting on a regular basis. I'm busy. Sorry.

Anyway...

If you follow this blog at all, you've probably reached the conclusion that I hate printers and print servers. You're partially right. I hate them because they're a pain in the butt, but I also enjoy them because they give me an opportunity to still do some technical stuff. Remember, I'm a manager now. I'm not supposed to be technical.

Previously I posted some code I adapted to monitor and generate statistics from my company's Windows print servers. Now I've worked on a script that runs with user rights to unmap and remap printers, correcting printer names and forcing the download of new drivers for each printer from the server. This is still a work in progress, but it works pretty darned well, even if I do say so myself. I was never very happy with anything I found on the Net, so this is my stab at this functionality. Please, feel free to give me advice for improvements, but also feel free to use this code at your own risk. I make no warranty of any of this code.


'****************************************************
' Script by Brian E. Tower
' October 2008
'
' This script enumerates printers, determines which printers are
' mapped to specific print servers, makes a list of these printers
' corrects the server they are mapped to, deletes the mappings, deletes the drivers,
' remaps the printers with correct server names and resets the default printer.
'****************************************************

CheckRunner 'makes sure your running with cscript, not wscript

Dim arrMappedPrinters()
Dim arrNewMappings()
intSize = 0
Dim i, strCurrentDefaultPrinter, strRoot, strDelCmd, WshNetwork, strPrtr, strBits

On Error Resume Next

strBits = GetOS()

If strBits = 0 Then
WScript.Echo
WScript.Echo "You are running a 32 bit OS. Proceeding..."
WScript.Echo

'find default printer currently set and set a variable equal to it and change to the right print server
strCurrentDefaultPrinter = ChgPrntSrv(GetDefaultPrinter)

'find the system root, save for later
Set objShell = WScript.CreateObject( "WScript.Shell" )
strRoot = objShell.ExpandEnvironmentStrings("%SYSTEMROOT%")
'this is the executable to delete all unused print drivers
strDelCmd = strRoot & "\system32\cscript.exe " & strRoot & "\system32\prndrvr.vbs -x"

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objPrinters = WshNetwork.EnumPrinterConnections

WScript.Echo "Cataloging printer mappings:"
WScript.Echo
For i = 0 to objPrinters.Count - 1 Step 2
strPrtr = lcase(objPrinters.Item(i+1))
If InStr(1, strPrtr, "\\n1") <> 0 Then
'push unc into array
ReDim Preserve arrMappedPrinters(intSize)
ReDim Preserve arrNewMappings(intSize)
arrMappedPrinters(intSize) = strPrtr
'change server names and put in newprinter array
arrNewMappings(intSize) = ChgPrntSrv(arrMappedPrinters(intSize))
intSize = intSize +1
ElseIf InStr(1, strPrtr, "\\ac") <> 0 Then
'push unc into array
ReDim Preserve arrMappedPrinters(intSize)
ReDim Preserve arrNewMappings(intSize)
arrMappedPrinters(intSize) = strPrtr
'change server names and put in newprinter array
arrNewMappings(intSize) = ChgPrntSrv(arrMappedPrinters(intSize))
intSize = intSize +1
End If
Next

' remove printers
WScript.Echo "Deleting current mappings:"
For each strPrinterPath in arrMappedPrinters
WScript.Echo "Deleting " & strPrinterPath
WshNetwork.RemovePrinterConnection strPrinterPath, true, true
Next

' delete drivers
WScript.Echo
WScript.Echo "Deleting unused print drivers:"
Set oExec = objShell.Exec(strDelCmd)

' remap printers
WScript.Echo
WScript.Echo "Remapping printers:"
For Each strPrinterAdd in arrNewMappings
WScript.Echo "Mapping " & strPrinterAdd
WshNetwork.AddWindowsPrinterConnection strPrinterAdd
If Err <> 0 Then
WScript.Echo "Error mapping " & strPrinterAdd & ". You will need to map this printer manually."
DisplayErrorInfo
End If
'WScript.Sleep 1000
Next

'set default printer
WScript.Echo
WScript.Echo "Setting Default Printer:"
If Not strCurrentDefaultPrinter = "" then
WScript.Echo "Setting default printer to " & strCurrentDefaultPrinter
WshNetwork.SetDefaultPrinter strCurrentDefaultPrinter
Else
WScript.Echo "No default printer defined"
End If

Else
WScript.Echo
WScript.Echo
WScript.Echo "64 bit OS detected. This utility does not run on 64 bit OS"
WScript.Echo
WScript.Echo
End If


'****************************************************
' Functions and Notes below this point
'****************************************************
Function GetDefaultPrinter() 'this function finds the default printer setting for the current user
Set objShell = CreateObject("WScript.Shell")
strRegVal = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
strDefault = ""
On Error Resume Next
strDefault = objShell.RegRead(strRegVal)
strDefault = Left(strDefault ,InStr(strDefault, ",") - 1)
On Error Goto 0
GetDefaultPrinter = strDefault
End Function


Function ChgPrntSrv(strServer) 'this function converts old print server names to new print server names
If InStr(1, strServer, "\\n1ps01") <> 0 Then
ChgPrntSrv = Replace(strServer, "n1ps01", "n1print01")
ElseIf InStr(1, strServer, "\\n1ps02") <> 0 Then
ChgPrntSrv = Replace(strServer, "n1ps02", "n1print01")
ElseIf InStr(1, strServer, "\\n1ps03") <> 0 Then
ChgPrntSrv = Replace(strServer, "n1ps03", "n1print02")
ElseIf InStr(1, strServer, "\\n1ps04") <> 0 Then
ChgPrntSrv = Replace(strServer, "n1ps04", "n1print02")
ElseIf InStr(1, strServer, "\\n1prntsrv01") <> 0 Then
ChgPrntSrv = Replace(strServer, "n1prntsrv01", "n1print01")
ElseIf InStr(1, strServer, "\\n1prntsrv02") <> 0 Then
ChgPrntSrv = Replace(strServer, "n1prntsrv02", "n1print02")
ElseIf InStr(1, strServer, "\\acprnt01") <> 0 Then
ChgPrntSrv = Replace(strServer, "acprnt01", "acprint02")
ElseIf InStr(1, strServer, "\\acprnt02") <> 0 Then
ChgPrntSrv = Replace(strServer, "acprnt02", "acprint02")
ElseIf InStr(1, strServer, "\\acps02") <> 0 Then
ChgPrntSrv = Replace(strServer, "acps02", "acprint02")
ElseIf InStr(1, strServer, "\\acprnt03") <> 0 Then
ChgPrntSrv = Replace(strServer, "acprnt03", "acprint01")
ElseIf InStr(1, strServer, "\\acprnt04") <> 0 Then
ChgPrntSrv = Replace(strServer, "acprnt04", "acprint01")
ElseIf InStr(1, strServer, "\\acps01") <> 0 Then
ChgPrntSrv = Replace(strServer, "acps01", "acprint01")
Else
ChgPrntSrv = strServer
End If
End Function

Function GetOS() 'this function determines if the OS is 64 or 32 bit
Dim oWMI
Dim oOS, xOS
Dim oRX_Bit
Dim b64 'Boolean value. If true, it is 64Bit, else it's 32Bit.

Set oWMI = GetObject("winmgmts:")
Set oRX_Bit = New RegExp
oRX_Bit.Pattern = "x64"
oRX_Bit.IgnoreCase = True

Set oOS = oWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each xOS In oOS
If oRX_Bit.Test(xOS.Name) Then
b64 = True
Else
b64 = False
End If
Next
GetOS = b64
End Function

Sub DisplayErrorInfo
WScript.Echo "Error: : " & Err
WScript.Echo "Error (hex) : &H" & Hex(Err)
WScript.Echo "Source : " & Err.Source
WScript.Echo "Description : " & Err.Description
Err.Clear
End Sub

Sub CheckRunner
dim arrProgRun, intEnd, strExecute

arrProgRun = split(WScript.FullName, "\")
intEnd = Ubound(arrProgRun)
strExecute = arrProgRun(intEnd)

if not lcase(strExecute) = "cscript.exe" then
WScript.Echo "You must run this script with CScript. " & vbCrLf & "Please read the instructions!!!" & vbCrLf & vbCrLf & "Bet you clicked an icon."
WScript.Quit (1)
end if
End Sub

Tuesday, September 9, 2008

Auditing Windows 2003 Print Servers

As many of you know I lead a team that manages a large number of Windows servers - along with other stuff - for a large corporation. One of the most dreaded things we deal with are print servers. In general, they're just a pain, but I digress.

A while back I was trying to come up with a way to audit the print servers to determine who was using each queue, how many pages were being printed by each queue, which queues were actually being used, etc. Much to my surprise, there were no programs or tools available that do what I want. Not even from Uncle Bill's big blue company. Not even Open Source. Then I found this really good blog with a script that did about 90 percent of what I need, written by a gentleman in Brisbane Australia named Wayne. For a full explanation of how to use these scripts, please see Wayne's blog.



To make a long story short, I took Wayne's script and in the spirit of open source modified it to do what I needed it to do. Now I'm returning the script to the "wild" and hoping some other lucky slob finds it useful.

So, here's my version of the ProcessPrinterLogs.vbs script originally posted by Wayne.

---

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

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

Main()


Sub Main()
If WScript.Arguments.Named.Exists("f") Then
sSource = Wscript.Arguments.Named("f")
Else
Wscript.Arguments.ShowUsage()
Wscript.Echo "Source file or directory must be supplied"
Wscript.Quit(2)
End If

If Wscript.Arguments.Named.Exists("o") Then
sOutputFile = Wscript.Arguments.Named("o")
Else
dNow = Now
dLogDate = DatePart("yyyy", dNow)
dLogDate = dLogDate & String(2 - Len(DatePart("m", dNow)),"0") & DatePart("m", dNow)
dLogDate = dLogDate & String(2 - Len(DatePart("d", dNow)),"0") & DatePart("d", dNow)
sOutputFile = Left(WScript.ScriptName, InStrRev(WScript.ScriptName,".vbs")-1) & "_" & dLogDate & ".csv"
End If

wscript.echo "Input file/dir: '" & sSource & "'"
wscript.echo "Output file: '" & sOutputFile & "'"


If objFSO.FileExists(sSource) Then
sFileSet = sSource ' Process a single file
wscript.echo "Single file specified - " & sFileSet
ElseIf objFSO.FolderExists(sSource) Then
wscript.echo "Source specified was a directory, reading files from '" & sSource & "'"
sFileSet = ""
Set oFolder = objFSO.GetFolder(sSource) ' Get the folder
Set oFiles = oFolder.Files
For Each oFile in oFiles ' For each file
sFileset = sFileset & vbCRLF & oFile.Path ' Append to the fileset
Next
If Len(sFileSet) > Len(vbCRLF) Then sFileSet = Right(sFileSet, Len(sFileSet) - Len(vbCRLF)) ' Trim the leading CRLF
End If

Set dPrinters = CreateObject("Scripting.Dictionary") ' Create the dictionary objects
Set dUsersPerPrinter = CreateObject("Scripting.Dictionary")
Set dusers = CreateObject("Scripting.Dictionary")
Set dDates = CreateObject("Scripting.Dictionary")
Set dJobs = CreateObject("Scripting.Dictionary")

For Each sFile in Split(sFileset, vbCRLF) ' For Each file

Set objFile = objFSO.GetFile(sFile)
If objFile.size > 0 then ' Don't process the file if it is a 0 byte file
wscript.echo "Processing '" & sFile & "'"
sBuffer = ""
Set objTextStream = objFSO.OpenTextFile(sFile, ForReading)
sBuffer = objTextStream.ReadAll

For Each sLine in Split(sBuffer, vbCRLF) ' For each line in this file
Call ProcessLogEntry(sLine, dPrinters, dUsers, dDates, dJobs, dUsersPerPrinter) ' Process the log entry
Next
End If
Next


Call ProduceOutput(sOutput, dPrinters, dUsers, dDates, dJobs, dUsersPerPrinter) ' Produce the output
Set objTextStream = objFSO.OpenTextFile(sOutputFile, ForWriting, True)
objTextStream.Write sOutput
wscript.echo "Output saved to '" & sOutputFile & "', " & Len(sOutput) & " characters."

End Sub

Function ProduceOutput(ByRef sOutput, ByRef dPrinters, ByRef dUsers, ByRef dDates, ByRef dJobs, ByRef dUsersPerPrinter)
Dim strPrinter, strPort, dtmDate, strUser, strserver, strDocumentName, intSize, intPages, strInformation, strTotal
Dim strUserTotal, strPrinterTotal, strDateTotal, strJobTotal, aJobTotal

sOutput = ""
For Each strPrinter in dPrinters.Keys
sOutput = sOutput & vbCRLF & strPrinter & "," & dPrinters.Item(strPrinter)
Next

sOutput = sOutput & vbCRLF
For Each strUser in dUsers.Keys
sOutput = sOutput & vbCRLF & strUser & "," & dUsers.Item(strUser)
Next

'new portion to output list of users per print queue
sOutPut = sOutput & vbCRLF
For Each strPrinter in dUsersPerPrinter.Keys
sOutput = sOutput & vbCRLF & strPrinter & "," & dUsersPerPrinter.Item(strPrinter)
Next
'end of new output

sOutput = sOutput & vbCRLF
For Each dtmDate in dDates.Keys
sOutput = sOutput & vbCRLF & dtmDate & "," & dDates.Item(dtmDate)
Next

sOutput = sOutput & vbCRLF
For Each strTotal in dJobs.Keys
strJobTotal = dJobs.Item(strTotal)
aJobTotal = Split(strJobTotal, ",")
sOutput = sOutput & vbCRLF & "Total Jobs," & aJobTotal(0)
sOutput = sOutput & vbCRLF & "Total Pages," & aJobTotal(1)
sOutput = sOutput & vbCRLF & "Total Size (MB)," & aJobTotal(2)
Next

sOutput = sOutput & vbCRLF
strUserTotal = UBound(dUsers.Keys)+1
strPrinterTotal = UBound(dPrinters.Keys)+1
strDateTotal = UBound(dDates.Keys)+1
sOutput = sOutput & vbCRLF & "Printers," & strPrinterTotal
sOutput = sOutput & vbCRLF & "Users," & strUserTotal
sOutput = sOutput & vbCRLF & "Days," & strDateTotal

aJobTotal = Split(strJobTotal, ",")
sOutput = sOutput & vbCRLF

sOutput = sOutput & vbCRLF & "Average jobs/person," & CInt(aJobTotal(0)/strUserTotal)
sOutput = sOutput & vbCRLF & "Average pages/person," & CInt(aJobTotal(1)/strUserTotal)
sOutput = sOutput & vbCRLF & "Average pages/person/day," & CInt(CInt(aJobTotal(1)/strUserTotal) / strDateTotal)
sOutput = sOutput & vbCRLF & "Average pages/minute," & CInt(aJobTotal(1) / (strDateTotal * 8 * 60))

End Function

Function ProcessLogEntry(ByRef sLine, ByRef dPrinters, ByRef dUsers, ByRef dDates, ByRef dJobs, ByRef dUsersPerPrinter)
Dim strPrinter, strPort, dtmDate, strUser, strserver, strDocumentName, intSize, intPages, strInformation
Dim aPrintJob, intOffset, strTemp, aTemp

aPrintJob = Split(sLine, vbTAB)


If UBound(aPrintJob) = 9 Then
dtmDate = aPrintJob(0) ' & " " & aPrintJob(1)
aTemp = Split(dtmDate, "/")
dtmDate = Right("00" & Trim(aTemp(1)), 2) & "/" & Right("00" & Trim(aTemp(0)), 2) & "/" & aTemp(2) ' Trim, pad and switch to dd/mm/yyyy instead of mm/dd/yyyy
strServer = aPrintJob(8)

strInformation = Trim(aPrintJob(9))
strInformation = Right(strInformation, Len(strInformation) - InStr(strInformation, " ")) ' Remove the job ID
intOffset = InStrRev(strInformation, " ")
intPages = Right(strInformation, Len(strInformation) - intOffset) ' Extract the number of pages from the end
strInformation = Left(strInformation, intOffset-1) ' Trim the string

intOffset = InStrRev(strInformation, " ")
intSize = Right(strInformation, Len(strInformation) - intOffset) ' Extract the number of bytes from the end
strInformation = Left(strInformation, intOffset-1) ' Trim the string

intOffset = InStrRev(strInformation, " ")
strPort = Right(strInformation, Len(strInformation) - intOffset) ' Extract the port from the end
strInformation = Left(strInformation, intOffset-1) ' Trim the string

intOffset = InStrRev(strInformation, " ")
strPrinter = Right(strInformation, Len(strInformation) - intOffset) ' Extract the printer from the end
strInformation = Left(strInformation, intOffset-1) ' Trim the string

intOffset = InStrRev(strInformation, " ")
strUser = Right(strInformation, Len(strInformation) - intOffset) ' Extract the user from the end
strInformation = Left(strInformation, intOffset-1) ' Trim the string

strDocumentName = strInformation

If dPrinters.Exists(strPrinter) Then ' Does this printer already exist in the dictionary?
aTemp = Split(dPrinters.Item(strPrinter), ",") ' Find the existing printer job/page count
aTemp(0) = aTemp(0) + 1 ' Increment the job count
aTemp(1) = aTemp(1) + CInt(intPages) ' Add to the page count
aTemp(2) = aTemp(2) + CInt(intSize/1024/1024) ' Add to the byte count
dPrinters.Item(strPrinter) = Join(aTemp, ",") ' Update the dictionary
Else
aTemp = Array(1, intPages, CInt(intsize /1024/1024)) ' Start the job/page count
dPrinters.Add strPrinter, Join(aTemp, ",") ' Create this item
End If

If dUsers.Exists(strUser) Then ' Does this user already exist in the dictionary?
aTemp = Split(dUsers.Item(strUser), ",") ' Find the existing user job/page count
aTemp(0) = aTemp(0) + 1 ' Increment the job count
aTemp(1) = aTemp(1) + CInt(intPages) ' Add to the page count
aTemp(2) = aTemp(2) + CInt(intSize/1024/1024) ' Add to the byte count
dUsers.Item(strUser) = Join(aTemp, ",") ' Update the dictionary
Else
aTemp = Array(1, intPages, CInt(intsize /1024/1024)) ' Start the job/page count
dUsers.Add strUser, Join(aTemp, ",") ' Create this item
End If

If dDates.Exists(dtmDate) Then ' Does this date already exist in the dictionary?
aTemp = Split(dDates.Item(dtmDate), ",") ' Find the existing date job/page count
aTemp(0) = aTemp(0) + 1 ' Increment the job count
aTemp(1) = aTemp(1) + CInt(intPages) ' Add to the page count
aTemp(2) = aTemp(2) + CInt(intSize/1024/1024) ' Add to the byte count
dDates.Item(dtmDate) = Join(aTemp, ",") ' Update the dictionary
Else
aTemp = Array(1, intPages, CInt(intsize /1024/1024)) ' Start the job/page count
dDates.Add dtmDate, Join(aTemp, ",") ' Create this item
End If

If dJobs.Exists(JOB_TOTAL) Then ' Does the total already exist in the dictionary?
aTemp = Split(dJobs.Item(JOB_TOTAL), ",") ' Find the existing total counts
aTemp(0) = aTemp(0) + 1 ' Increment the job count
aTemp(1) = aTemp(1) + CInt(intPages) ' Add to the page count
aTemp(2) = aTemp(2) + CInt(intSize/1024/1024) ' Add to the byte count
dJobs.Item(JOB_TOTAL) = Join(aTemp, ",") ' Update the dictionary
Else
aTemp = Array(1, intPages, CInt(intsize /1024/1024)) ' Start the job/page count
dJobs.Add JOB_TOTAL, Join(aTemp, ",") ' Create this item
End If

' This section creates a list of users that are using each print queue
If dUsersPerPrinter.Exists(strPrinter) Then ' Does the printer exist as a key in the dictionary?
dim bTemp
bTemp = dUsersPerPrinter.Item(strPrinter) & "," & strUser ' build up the list of users
dUsersPerPrinter.Item(strPrinter) = DedupeString(bTemp,",") ' dedupe sting of users and populate dictionary
Else
dUsersPerPrinter.Add strPrinter, strUser ' Create this item
End If
' End of user list creation

Else
wscript.echo "skipped '" & sLine & "'" ' line skipped because number of elemnts in array not equal to 9 ( need to figure this one out)
End If
End Function


' Deduping a string
' must use this function so we end up with a unique list of users with no duplicates
Function DedupeString(inString,strSeperate)
Dim vObjects, myDict, index, strFinal
strFinal = ""
Set myDict = CreateObject("Scripting.Dictionary")
vObjects = split(inString,strSeperate)
for index = 0 to UBound(vObjects)
if ( not myDict.Exists(vObjects(index)) ) then
myDict.Add vObjects(index), vObjects(index)
if (Len(strFinal)) > 0 Then
strFinal = strFinal & strSeperate & myDict(vObjects(index))
else
strFinal = myDict(vObjects(index))
end if
end if
next
DedupeString = strFinal
End Function


---

The other big change I made was in the wrapper batch file that pulls down the log files. I changed it so that a single script loops through a list of print servers.

Here's my version of the auditprinters.bat script.

---


:: bet_auditprint.bat
:: batch wrapper script to collect relevant event log entries from multiple print servers and then process these logs with ProcessPrinterLogs.vbs

for %%S in (Server1, Server2, Server3) do (

Set print_server=%%S
CALL :s_get_logs

)
GOTO eof

:s_get_logs
set MainDir=d:\BETPrinters\%print_server%
Set PrintDir=%MainDir%\printdir
Set LogDir=%MainDir%\logs

:: Delete log files older than 30 days
forfiles /p %LogDir% /d -30 /c "CMD /C del @FILE"

:: Create needed directories
if not exist %PrintDir% md %PrintDir%
if not exist %LogDir% md %LogDir%

:: Set date format
for /f "tokens=1-8 delims=/:. " %%i in ('echo %date%') do Set DateFlat=%%j%%k%%l

:: Set log an backup files
Set LogFile=%print_server%_jobs_%DateFlat%.csv
Set BackFile=PrintJobs_%DateFlat%.csv

:: Dump the printer log
:: Using full path for safety
D:\SystemApps32\ResourceKit\dumpel -s \\%print_server% -l System -e 10 -m Print -d 1 >> %logDir%\%LogFile%

:: Make a backup copy
copy %logDir%\%print_server%_jobs_%DateFlat%.csv %PrintDir%\%BackFile% /y

:: Process the logs
cscript ProcessPrinterLogs.vbs /f:%LogDir% /o:%MainDir%\%print_server%_auditoutput.csv
:eof

---

Okay, so there it is. Enjoy! Use it in good health and for non nefarious purposes!

About Me

My photo
A living, breathing contradiction.