Skip to content

Instantly share code, notes, and snippets.

@irwins
Last active October 21, 2018 18:36
Show Gist options
  • Save irwins/af063d9e18d2b8087380981077fba864 to your computer and use it in GitHub Desktop.
Save irwins/af063d9e18d2b8087380981077fba864 to your computer and use it in GitHub Desktop.
Infrastructure validation script using the Arrange Act Assert principle for creating users from a CSV file
<#
Author: I.C.A. Strachan
Version:
Version History:
Purpose: Infrastructure validation script to create ADUser from CSV file
#>
[CmdletBinding(SupportsShouldProcess=$True)]
Param(
[string]
$File = 'C:\scripts\source\csv\users.csv'
)
function ConvertTo-HashTable{
param(
[PSObject]$PSObject
)
$splat = @{}
$PSObject |
Get-Member -MemberType *Property |
ForEach-Object{
$splat.$($_.Name) = $PSObject.$($_.Name)
}
$splat
}
Function Create-TestCases{
param(
$objActual,
$objExpected,
$Properties
)
$Properties |
ForEach-Object{
@{
Actual = $objActual.$_
Expected = $objExpected.$_
Property = $_
}
}
}
$csvFile = Get-childItem $file
#region Arrange
#Define proper dependencies
#1) Verify csv file exist
#2) Can user read AD properties.
$dependencies = @(
@{
Label = 'ActiveDirectory module is available '
Test = {(Get-module -ListAvailable).Name -contains 'ActiveDirectory'}
Action = {Import-Module -Name ActiveDirectory}
}
@{
Label = "CSV File at $($csvFile) exists"
Test = {Test-Path -Path $csvFile}
Action = {
$script:csvUsers = Import-Csv -Path $csvFile -Delimiter ";" -Encoding UTF8
$csvColumns = ($csvUsers | Get-Member | Where-Object {$_.membertype -eq 'noteproperty'}).name
$script:UserProperties = $csvColumns.Where{$_ -ne 'Path'}
}
}
@{
Label = "$(& "$env:windir\system32\whoami.exe") can read AD user object properties"
Test = {[bool](Get-AdUser -Identity $("$((Get-ADDomain).DomainSID)-500"))}
Action = {}
}
)
foreach($dependency in $dependencies){
if(!( & $dependency.Test)){
throw "The check: $($dependency.Label) failed. Halting script"
}
else{
Write-Verbose $dependency.Label
& $dependency.Action
}
}
#endregion
$csvUsers |
Foreach-Object{
$Actual = $_
Describe "Processing User $($Actual.SamAccountName)"{
#Convert to HashTable for splatting
$paramNewADUser = ConvertTo-HashTable -PSObject $Actual
#region Act
#1) Create ADUser from csv file
It "Create an account for $($Actual.SamAccountName)"{
New-ADUser @paramNewADUser
}
#endregion
#region Assert
#1) Verify AD user has been created correctly
#Get AD user properties
$Expected = Get-ADUser -Identity $Actual.SamAccountName -Properties $UserProperties
#Create TestCases for verifying Properties
$TestCases = Create-TestCases -objActual $Actual -objExpected $Expected -Properties $UserProperties
It 'Verify that property <property> expected value <expected> actually is <actual>' -TestCases $TestCases {
param($Actual,$Expected,$Property)
$Actual.$Property | should be $Expected.$Property
}
#endregion
}
}
@irwins
Copy link
Author

irwins commented Mar 17, 2017

Trying my hand at arrange-act-assert with dependencies included...

Got the dependencies tip from the Pester book by Don Jones & Adam Bertram

I'm using Pester 4.0 where Describe can be nested.
image

I was pleasantly surprised by the way Pester handled the error when the user already existed

Feels like I'm overthinking this...

Any tips/ideas on how to make this better?

Rg./Irwin

@bielawb
Copy link

bielawb commented Mar 18, 2017

Line 61 - shouldn't it read Get-Module -List ? Current test will result in $false -> throw unless module is loaded already, so action is not really doing anything....

@irwins
Copy link
Author

irwins commented Mar 18, 2017

Yeah, I just noticed that myself...
This works:

(Get-module -ListAvailable).Name -contains 'ActiveDirectory'

Will update it... 👍

@irwins
Copy link
Author

irwins commented Mar 19, 2017

Reverted back to a single Describe...
image

Had a lively discussion on FB.

So this isn't a good example of AAA principal, but is it useful? It gives you feedback as to what's going on, what went right and what didn't...

Any takers on how it should be done right? 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment