This 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 } } }