Last active
October 21, 2018 18:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
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 | |
} | |
} |
Yeah, I just noticed that myself...
This works:
(Get-module -ListAvailable).Name -contains 'ActiveDirectory'
Will update it... 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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....