Find users using printer server

Right now we are disposing  two old printer servers that we thought no one were using. After we began to log all prints we noticed that it still was hundreds of users using the old printer servers. We could link some of the prints to an old GPO with an VB logon-script but most of the users have probably mapped the printers manually, therefore the client-guys needed a list of users who had users the printer server the last 90 days. First of all you need to start logging all prints, to do so on an Windows 2008 R2 start Print Management and right-click on the server and select properties and check Log spooler information events.

printer_logging

 

Now you just have to wait 90 days and then run this script to get an report of which user that have users printer server.

 

#-------------------------------------------------------------
#NAME: List_priter_users.ps1
#AUTHOR: Viktor Lindström
#
#COMMENTS: List all users that have used printer server last 90 days.
#-------------------------------------------------------------

$date = get-date
$priterservers = "srv-print04", "srv-print05"

foreach ($server in $priterservers)
{$con = get-eventlog -ComputerName $server -log  system  -After $date.AddDays(-90) | where {$_.eventID -eq 10} | select UserName
$uni = $con | Sort-Object username | Get-Unique -AsString
$uni | Export-Csv c:\temp\$server.txt
    }

Bulk modify AD users from CSV

The last post showed how to create AD users from CSV, here is how to bulk modify attributes from CSV

#Modify AD user from CSV file, in this case i Change the extensionAttribute1 and extensionAtribute2
#anvandare,test,Url
#viklin,test1,https://www.contoso.com
#tonrim,test2,https://www.contoso.com
#steloz,test3,https://www.contoso.com
$Import=Import-Csv C:\temp\csv.txt
    foreach($user1 in $Import)
        {
    set-aduser $user1.anvandare -Replace @{extensionAttribute1=$user1.test; extensionAttribute2=$user1.Url} 
    }

Bulk create AD users from CSV

In a Facebook group a user asked for a script to create users from CSV, it is very easy:

# $sourceuser store the the user you whant to create users from in an variable
# $import imports CSV in an variable with the users the scv looks like this:
# 
# logon,FirsName,LastName,DisplayName
# klitom,Tommy,Stenstrom,Stenstrom Tommy
# klidoo,Sonny,Ramos,Ramos Sonny
# klikri,Conny,Edwards,Edwards Conny
#
# $ou specifies wich OU you whant to put the users in. 
$sourceuser = get-aduser klidan
$import = import-csv C:\temp\användare.txt
$ou = "OU=Admin,OU=Data,DC=adm,DC=contoso,DC=se"
foreach ($användare in $import)
{

New-ADUser -SamAccountName $användare.logon -UserPrincipalName $användare.logon -Name $användare.DisplayName -GivenName $användare.FirstName -Surname $användare.LastName -DisplayName $användare.DisplayName  -Instance $sourceuser -Enabled $true -path $ou -AccountPassword (Read-Host -AsSecureString "AccountPassword") -ChangePasswordAtLogon $true }

Count users from AD groups

countThis week I got involved in a project regarding licensing on our remote desktop servers. In phase one I had to do a current situation analysis on how many users that can access our different servers. Since we have 5 different departments the project manager also wanted to have information of how many users the different departments had on each server and a separate excel document for each department with all their users listed. The project manager also hinted that in phase 2 after we made actions and before each license audit we might have to create new reports. Since I am lazy and don´t like to manually create reports I made a function in Powershell  where anyone with the active directory-module can create reports.

function get-UsersFromGroup 
{
param
([parameter(mandatory)]
[ValidateSet("srv-rcard01","srv-rds02","srv-procapts01")]
[string] $server)

# Variables for date and exportfile-summary
$date = get-date -Format d
$outfile = "C:\temp\Sammanfattning $server $date.txt"
$tempdir = "C:\temp"

# Get users from AD group depending on server.
switch ($server) {
srv-rcard01 {$users = Get-ADGroupMember r-card_srv-rco01}
srv-rds02 {$users = Get-ADGroupMember "BG Fjärrskrivbord" }
#Since 2 groups with some users in both broups I do a sort-object and unique
srv-procapts01 {$group1 = Get-ADGroupMember Procapita-Funktion-Remoteapp
$group2 = Get-ADGroupMember Procapita-Aldreoms-Remoteapp
$userspro = $group1 + $group2 
$users = $userspro | Sort-Object | Get-Unique}
}

#Get users from group
#$users = Get-ADGroupMember "$group"
$users2 = $users | Get-ADUser -properties * -ErrorAction SilentlyContinue

#build empty arrays
$KSF = @()
$SAF = @()
$BUF = @()
$KUF = @()
$MSB = @()
$Övriga = @()

#Sort by apartment.
foreach ($user in $users2)
{if($user.DistinguishedName -like "*OU=KSF*")
{$KSF +=$user}
 elseif ($user.DistinguishedName -like "*OU=SAF*")
{$SAF +=$user}
 elseif
($user.DistinguishedName -like "*OU=BUF*")
{$BUF +=$user}
 elseif
($user.DistinguishedName -like "*OU=KUF*")
{$KUF +=$user}
 elseif
($user.DistinguishedName -like "*OU=MSB*")
{$MSB +=$user}
else
{$Övriga +=$user}
}

# Write result.
Write-host Sammanlagt är det $users2.count användare
write-host KSF har $ksf.count användare
write-host SAF har $saf.count användare
write-host BUF har $Buf.count användare
write-host KUF har $kuf.count användare
write-host MSB har $msb.count användare
write-host Övriga användare är $övriga.count stycken

#Delete summary-file if it exists.
if (test-path $outfile)
{Remove-Item -Path $outfile
    
}

#Check if dircetory c:\temp exist and if not create it.
if ( -Not (Test-Path $tempdir))
 { New-Item -Path $tempdir -ItemType Directory
 }

# Output count-result to file.
Write-Output "Sammanlagt är det $($users2.count) användare" | Out-File $outfile -Append
Write-Output "KSF har $($ksf.count) användare" | Out-File $outfile -Append
Write-Output "SAF har $($saf.count) användare" | Out-File $outfile -Append
Write-Output "BUF har $($Buf.count) användare" | Out-File $outfile -Append
Write-Output "KUF har $($kuf.count) användare" | Out-File $outfile -Append
Write-Output "MSB har $($msb.count) användare" | Out-File $outfile -Append
Write-Output "Övriga användare är $($övriga.count) stycken" | Out-File $outfile -Append



#Export to CSV to c:\temp\
Write-Host Exporterar alla användare till CSV som läggs i c:\temp\[förvaltning.csv]
$msb | select SamAccountName, GivenName, SurNAme, Description | Export-Csv c:\temp\msb-$server.csv -NoTypeInformation -Encoding UTF8 -Delimiter ";"
$ksf | select SamAccountName, GivenName, SurNAme, Description | Export-Csv c:\temp\ksf-$server.csv -NoTypeInformation -Encoding UTF8 -Delimiter ";"
$buf | select SamAccountName, GivenName, SurNAme, Description | Export-Csv c:\temp\buf-$server.csv -NoTypeInformation -Encoding UTF8 -Delimiter ";"
$kuf | select SamAccountName, GivenName, SurNAme, Description | Export-Csv c:\temp\kuf-$server.csv -NoTypeInformation -Encoding UTF8 -Delimiter ";"
$saf | select SamAccountName, GivenName, SurNAme, Description | Export-Csv c:\temp\saf-$server.csv -NoTypeInformation -Encoding UTF8 -Delimiter ";"
$Övriga | select SamAccountName, GivenName, SurNAme, Description | Export-Csv c:\temp\övriga-$server.csv -NoTypeInformation -Encoding UTF8 -Delimiter ";"

#Capture if there is a group in the result.
foreach ($grupp in $users)
{
if ($grupp.objectclass -like "group") {Write-Host det finns en grupp som heter $grupp.name -BackgroundColor Red }
    }
}