SOLVED

Powershell - replace an array / hash table with a file

Copper Contributor

Hi All,
This should be straigtforward (I think), but i cannot get it to work.
I have a working script which reads a csv file from an HR app and then creates the user accounts.
Dependant on the users site name it will read the array and assign them to the correct OU.
As the names of the sites has proven to be inconsistant or at least 3 variants in some cases, I created an array (could it be classed as a dictionary file?). to handle the differences, e.g.
$Site2OU = @{
"Miami" = "OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Miami Beach" = "OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Miami Beach Holiday Homes" = "OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Boston" = "OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Boston Bay" = "OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Bston Bay Holiday Homes" = "OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Mississippi" = "OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
"Mississippi Water" = "OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
"Mississippi Water Holiday Homes" = "OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
}
then an extract from the script would handle it like this

(Imported csv Foreach($NewUser in $Newusers)
$Location = $NewUser.Site 
$OU = $Site2OU.$Location

This would then create the $OU variable for a site based on any of the 3 variations.
Everytime a new site is added it means editing the script etc. And this script is compiled into a GUI exe file.
To save the hassle I would like to replace the array with the contents of a file, which would allow the users to add new sites, as and when they are required.
Is there an easy way to do this, I have tried numerous ways and none work. 
Any pointers would be appreciated.

2 Replies
best response confirmed by _MoZZa (Copper Contributor)
Solution

@_MoZZa 

 

Hi, MoZZa.

 

Logically-speaking, you can refer to $Site2OU as a dictionary, but strictly-speaking, PowerShell uses the HashTable class for the @{} statement.

 

 

Given you're working with only two columns in the input file, you can easily achieve what you're seeking.

 

Input file

"name","ou"
"Miami","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Miami Beach","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Miami Beach Holiday Homes","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Boston","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Boston Bay","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Bston Bay Holiday Homes","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Mississippi","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
"Mississippi Water","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
"Mississippi Water Holiday Homes","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"

 

Constructing the HashTable

$h = [ordered] @{};
Import-Csv -Path D:\Data\Temp\Forum\forum.csv | ForEach-Object { $h.Add($_.name, $_.ou); }

 

Example output

LainRobertson_0-1699233068024.png

 

Cheers,

Lain

Hi @LainRobertson,
Perfect! I was actually close but not close enough.
Thank you for your solution!!!
1 best response

Accepted Solutions
best response confirmed by _MoZZa (Copper Contributor)
Solution

@_MoZZa 

 

Hi, MoZZa.

 

Logically-speaking, you can refer to $Site2OU as a dictionary, but strictly-speaking, PowerShell uses the HashTable class for the @{} statement.

 

 

Given you're working with only two columns in the input file, you can easily achieve what you're seeking.

 

Input file

"name","ou"
"Miami","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Miami Beach","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Miami Beach Holiday Homes","OU=New Users,OU=Users,OU=Miami Beach,OU=Contoso,DC=com"
"Boston","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Boston Bay","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Bston Bay Holiday Homes","OU=New Users,OU=Users,OU=Boston Bay,OU=Contoso,DC=com"
"Mississippi","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
"Mississippi Water","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"
"Mississippi Water Holiday Homes","OU=New Users,OU=Users,OU=Mississippi Water,OU=Contoso,DC=com"

 

Constructing the HashTable

$h = [ordered] @{};
Import-Csv -Path D:\Data\Temp\Forum\forum.csv | ForEach-Object { $h.Add($_.name, $_.ou); }

 

Example output

LainRobertson_0-1699233068024.png

 

Cheers,

Lain

View solution in original post