|
| 1 | +Function Rename-PaperCutUser { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Renames a user in PaperCut db |
| 5 | + .DESCRIPTION |
| 6 | + Renames a user in PaperCut db (usually because of name change) |
| 7 | + .PARAMETER Id |
| 8 | + The user's current employee/student ID used as the primary key by PaperCut |
| 9 | + .PARAMETER NewFirstName |
| 10 | + (Optional) The user's current AD first name (whatever PaperCut should now reflect) |
| 11 | + .PARAMETER NewLastName |
| 12 | + (Optional) The user's current AD last name (whatever PaperCut should now reflect) |
| 13 | + .PARAMETER NewUserName |
| 14 | + The user's current AD SAMaccountName (whatever username PaperCut should now reflect) |
| 15 | + .OUTPUTS |
| 16 | + |
| 17 | + .EXAMPLE |
| 18 | + Rename-PaperCutUser -Id 1110005 -NewUserName SallyJones |
| 19 | + .FUNCTIONALITY |
| 20 | + PaperCut Management |
| 21 | + #> |
| 22 | + [cmdletbinding()] |
| 23 | + Param( |
| 24 | + [Parameter(Mandatory = $true)] |
| 25 | + [string]$Id, |
| 26 | + [Parameter(Mandatory = $false)] |
| 27 | + [string]$NewFirstName, |
| 28 | + [Parameter(Mandatory = $false)] |
| 29 | + [string]$NewLastName, |
| 30 | + [Parameter(Mandatory = $true)] |
| 31 | + [string]$NewUserName |
| 32 | + ) |
| 33 | + |
| 34 | + # Requires PowerShell v4 or higher |
| 35 | + |
| 36 | + # Import PaperCut Management module from https://github.com/robp2175/PapercutManagementModule |
| 37 | + Import-Module "\\path\to\PapercutManagementModule\PapercutManagement\bin\Release\PapercutManagement.dll" |
| 38 | + |
| 39 | + # Set default values and create a session to PaperCutServer (must use https) |
| 40 | + Connect-PcutServer -ComputerName printing.local -Port 9192 -authToken 2wVpxLeY4NjQz7VvMvaCAWcQunW1r0zk |
| 41 | + |
| 42 | + $OldUserName = (Get-PcutUserByIdNo -Id $Id).Username |
| 43 | + |
| 44 | + # Find/rename the auto-created new stuff (right username, but we need to get rid of it to rename the old account to the new stuff) |
| 45 | + $NewUser = Get-PcutUser -UserName $NewUserName |
| 46 | + if ($NewUser) { |
| 47 | + # new username already exists, so get some info from it to store into old account so that old account reflects new info |
| 48 | + $NewFullname = $NewUser.Fullname |
| 49 | + $NewEmailAddress = $NewUser.Email |
| 50 | + # email is almost like a primary key - PaperCut won't let you have two accounts that use same email, so set to something diff |
| 51 | + #$tempEmail = $NewEmailAddress + ".notused" |
| 52 | + #Set-PcutUserProperty -Username $NewUserName -PropertyName email -PropertyValue $tempEmail |
| 53 | + # let's delete new account now so old account can be renamed to new account's name |
| 54 | + Write-Verbose "Removing $NewUserName" |
| 55 | + Remove-PcutUser -UserName $NewUserName |
| 56 | + } |
| 57 | + else { |
| 58 | + # user didn't exist, so shouldn't be a problem to just rename old username to new username |
| 59 | + Write-Verbose "$NewUserName doesn't currently exist in PaperCut database." |
| 60 | + } |
| 61 | + |
| 62 | + # Rename old name to new name |
| 63 | + Write-Verbose "Renaming $OldUsername to $NewUserName" |
| 64 | + Rename-PcutUser -currentUserName $OldUserName -newUsername $NewUserName |
| 65 | + |
| 66 | + # Update fullname of renamed account to have correct name |
| 67 | + If (!$NewFullname) { |
| 68 | + # make sure NewLastName and NewFirstName params were supplied at runtime |
| 69 | + If ($NewLastName -ne $null -and $NewLastName -ne $null) { |
| 70 | + Write-Verbose "New user did not exist. Guestimating Fullname." |
| 71 | + $NewFullname = "$NewLastName" + ", " + "$NewFirstName" |
| 72 | + } |
| 73 | + Else { |
| 74 | + Write-Error "Name info not complete. Manually check $NewUserName for accuracy" |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + Set-PcutUserProperty -Username $NewUserName -PropertyName full-name -PropertyValue $NewFullname |
| 79 | + |
| 80 | + # Update email of renamed account to have correct email address |
| 81 | + If (!$NewEmailAddress) { |
| 82 | + Write-Verbose "New user did not exist. Guestimating Email." |
| 83 | + $NewEmailAddress = "$NewUserName" + "@domain.local" |
| 84 | + } |
| 85 | + Set-PcutUserProperty -Username $NewUserName -PropertyName email -PropertyValue $NewEmailAddress |
| 86 | + |
| 87 | + # Close connection to PaperCut Server |
| 88 | + Disconnect-PcutServer |
| 89 | +} |
| 90 | + |
| 91 | +Function Find-PcutUsersToRename { |
| 92 | + <# |
| 93 | + .SYNOPSIS |
| 94 | + Reads in a PaperCut log file looking for a specific date |
| 95 | + .DESCRIPTION |
| 96 | + Reads in a PaperCut log file looking for a specific date |
| 97 | + .PARAMETER Path |
| 98 | + The path to the PaperCut server.log file |
| 99 | + .PARAMETER Lines |
| 100 | + (Optional) The number of lines, from the end of the log file, to read in. Default is 70 |
| 101 | + .OUTPUTS |
| 102 | + A PSCustomObject hash of all new usernames and associated ID numbers that can be passed to Rename-PcutUser |
| 103 | + .EXAMPLE |
| 104 | + Find-PcutUsersToRename -Path \\papercut.local\share$\server.log |
| 105 | + .FUNCTIONALITY |
| 106 | + PaperCut Management |
| 107 | + #> |
| 108 | + [cmdletbinding()] |
| 109 | + Param( |
| 110 | + [Parameter(Mandatory = $true)] |
| 111 | + [string]$Path, |
| 112 | + [Parameter(Mandatory = $false)] |
| 113 | + [int]$Lines = 70 |
| 114 | + ) |
| 115 | + |
| 116 | + # Get today's date in the format that server.log records it |
| 117 | + $Today = "{0:yyyy-MM-dd}" -f (Get-Date) |
| 118 | + |
| 119 | + # read server.log content into var |
| 120 | + $Content = Get-Content $Path | Select-Object -Last $Lines |
| 121 | + |
| 122 | + # filter out any logfile lines that don't match today's date, then just save those |
| 123 | + $TodaysContent = $Content.Where( { $_ -like ("$Today*") }, 'SkipUntil') |
| 124 | + |
| 125 | + # iterate through today's log entries and find the ones that correspond to username sync issues |
| 126 | + # indicating a username rename in the past |
| 127 | + $SyncErrors = $TodaysContent -match "The user's card number will not be updated." |
| 128 | + |
| 129 | + # define regex to match only text inside the double quotes within the string - that should be the username |
| 130 | + $rx1 = [regex]'(?<=")(.+)(?=")' |
| 131 | + # define regex to match only text inside the parentheses within the string - that should be the ID num |
| 132 | + $rx2 = [regex]'\(([^\)]+)\)' |
| 133 | + # create array to hold Custom PSobject used later |
| 134 | + $Output = @() |
| 135 | + # SyncErrors is an array of strings. Iterate through each element performing the PaperCut management actions |
| 136 | + ForEach ($string in $SyncErrors) { |
| 137 | + # the username in the log file in the only thing on that line surrounded by double quotes |
| 138 | + $Username = $rx1.Match($string).Groups[0].Value |
| 139 | + # the ID number is the only thing in each log line entry that's contained within parentheses |
| 140 | + $IdNum = $rx2.Match($string).Groups[1].Value |
| 141 | + |
| 142 | + # Prepare Custom PowerShell Object for output with the info later on |
| 143 | + $OutputInfo = New-Object PSObject |
| 144 | + |
| 145 | + $OutputInfo | Add-Member NoteProperty Username $Username |
| 146 | + $OutputInfo | Add-Member NoteProperty IdNumber $IdNum |
| 147 | + |
| 148 | + $Output += $OutputInfo |
| 149 | + } |
| 150 | + |
| 151 | + Return $Output |
| 152 | +} |
| 153 | + |
| 154 | +$UsersToRename = Find-PcutUsersToRename -Path "\\printers.domain.local\c$\Program Files\PaperCut NG\server\logs\server.log" |
| 155 | + |
| 156 | +If ($UsersToRename.count -gt 0) { |
| 157 | + # there were sync errors in the log file, indicating usernames needing to be renamed. Process those now |
| 158 | + ForEach ($User in $UsersToRename) { |
| 159 | + Rename-PaperCutUser -Id $User.IdNumber -NewUserName $User.Username |
| 160 | + } |
| 161 | +} |
| 162 | +Else { |
| 163 | + # there were no sync errors, so no usernames need to be renamed at this time |
| 164 | +} |
0 commit comments