This solution seems inelegant because of the reliance on hard-coded dictionary labels to trigger a method’s switch.
- Should a child class replace the dictionary?
- If so, how would the class property names be called in order to trigger the switch without adding overhead (i.e., addition of PowerShell cmdlets,
Where-Object)? - If the dictionary is retained, should I use
[SortedDictionary [string, string]]instead?- Properties & elements throughout the parenting cmdlet are arranged a-z by personal principle.
Background
[ordered]@{}is avoided for fewer .NET conversions later (custom cmdlet is workflow member).$times, in validation, has appeared to have the correct sorting to continue using$times[n].$times[n]is joined with other elements in a Hash key for a later SQL Upsert (not shown).- Our Active Directory has large management variations (national scope: departmentally and regionally managed).
Get-ADUseris scoped to only a few service account OUs in the parent custom cmdlet (not shown).
Remarks
- AD values are typed as
[datetime]and[Int64]; and may be null, empty/ws, year -lt1900, or0. if/elseinforeach($i in $x.Keys)was introduced late—after a raised type conversion error on the$valueparameter.- This redundancy might be removed in the method in the next iteration.
Applies to
–targeted features only; additional parts removed
using namespace System.Collections;
using namespace System.Collections.Generic;
Enum SystemDateType
{
AccountLockoutTime = 0;
Created = 1;
LastBadPasswordAttempt = 2;
LastLogonDate = 3;
Modified = 4;
PasswordLastSet = 5;
BadPasswordTime = 16;
LastLogoff = 17;
LastSuccessfulInteractiveLogonTime = 18;
LockOutTime = 19;
}
Class InventoryItem
{
[datetime] $AccountLockoutTime;
[datetime] $BadPasswordTime;
<# various properties, 28 total arranged a-z #>
[datetime] $PasswordLastSet;
[datetime]ConvertToSQLDateTime([string] $value, [string] $key)
{
$convert = Switch($value)
{
{[string]::IsNullOrWhiteSpace($_) -or $_ -eq 0}{'1900-01-01'; break}
{[SystemDateType]::$key.value__ -le 5 -and ([datetime]$_).Year -lt 1900}{'1900-01-01'; break}
{[SystemDateType]::$key.value__ -gt 5}{[datetime]::FromFileTime($_); break}
default{$_}
}
return $convert;
}
}
Class InventoryCollection {<#... has AddItem() method for instances of InventoryItem #>}
$invenColl = [InventoryCollection]::new();
foreach($u in (Get-ADUser -Filter{ name -like <#[...]#>} <#[other params...]#>))
{
$results = [InventoryItem]::new();
$x = [dictionary[string,string]]::new();
$x['AccountLockoutTime'] = "$($u.AccountLockoutTime)";
$x['BadPasswordTime'] = "$($u.BadPasswordTime)";
# [...]
$x['PasswordLastSet'] = "$($u.PasswordLastSet)";
$times = [ArrayList]::new();
foreach($i in $x.Keys)
{
if([string]::IsNUllOrEmpty($x[$i]))
{
[void]$times.Add($results.ConvertToSQLDateTime(0,$i));
}
else
{
[void]$times.Add($results.ConvertToSQLDateTime($x[$i],$i));
}
}
$results.AccountLockoutTime = $times[0];
$results.BadPasswordTime = $times[1];
# [...]
$results.PasswordLastSet = $times[9];
$invenColl.AddItem($results);
}