A pretty simple request came across my desk today. I was requested to send a report of users that were created after a specific date. Do you remember the Windows Server 2003 and prior? Before PowerShell? We had to use what I’ll term loosely as “wonderful” LDAP queries. Also the query result was pure text, we couldn’t do anything easily with it short of magical text parsing! Well here is my PowerShell script I quickly whipped up!

param (
    $startDate = (Get-Date "January 1, 2017"),
    [ValidateSet("User", "Computer")][string]
    $objectType = "User",
    [string]$domain = ($env:USERDNSDOMAIN),
    [PSCredential]$cred = $null
)

# Get Today's date so we can figure out the last time an object logged in
$Today = Get-Date

$report = @()

# Query AD for the object type and build the report
If ($objectType -eq "Computer") {
    $ADObjs = Get-ADComputer -Server $domain -Filter * -Properties SamAccountName, whenCreated, lastlogonDate, Description, DistinguishedName | Where {$_.whenCreated -ge $startDate} | Sort-Object -Property samAccountName
    $report = $ADObjs | Select-Object SamAccountName, whenCreated, @{Name = "Days Since Last Logon"; expression = {($Today - $_.lastlogonDate).Days} }, Description, DistinguishedName, lastlogonDate
} else {
    $ADObjs = Get-ADUser -Server $domain -Filter * -Properties SamAccountName, whenCreated, GivenName, Surname, lastlogonDate, Department, Description, DistinguishedName | Where {$_.whenCreated -ge $startDate} | Sort-Object -Property samAccountName
    $report = $ADObjs | Select-Object SamAccountName, whenCreated, @{Name = "Days Since Last Logon"; expression = {($Today - $_.lastlogonDate).Days} }, GivenName, Surname, Department, Description, DistinguishedName, lastlogonDate
}

# Display the report to the screen and also to the clipboard. Great for copying directly into excel!
if ($report) {
    $report | ConvertTo-Csv -Delimiter `t -NoTypeInformation | Set-Clipboard
    $report | ft -AutoSize
}

Now with most scripts, they display the output on the screen in a nice formatted display. Well I do that too. I want to see what is actually happening. The downside is a lot of scripts will do this by using Format-Table, its alias ft, Select-Object or even with Write-Host. This works great for reporting and displaying the object’s property values, but the downside is it that it brings us back to the Pre-PowerShell era! When you use Format-Table, it breaks the data out of an object form and returns it as pure text to standard out… the monitor!

Scripts are great for doing work, but sometimes I want to retain the output so that I can work and massage the data a little bit more. To do this, it’s very simple, just return your object and capture it into a variable.

# It is nice we displayed the results, let's return it as an object for any extra manipulation
return $report

Now you need to call the script using a variable to capture the output.

$Users = .\AD-AccountCreation.ps1
$Computers = .\AD-AccountCreation.ps1 -objectType Computer

 

Hope this gives you a few ideas!

Advertisements