I want to validate user inputs in a script which sets the attributes Company, Department, and Office in Active Directory.
This is what I've got so far:
$ApprovedLocations = #A table with known Company,Department,Office values
Class Company : System.Management.Automation.IValidateSetValuesGenerator {
[String[]] GetValidValues(){
return $global:ApprovedLocations.Company
}
}
Class Department : System.Management.Automation.IValidateSetValuesGenerator {
[String[]] GetValidValues(){
return $global:ApprovedLocations.Department
}
}
Class Office : System.Management.Automation.IValidateSetValuesGenerator {
[String[]] GetValidValues(){
return $global:ApprovedLocations.Office
}
}
function Set-AttributeTest{
param (
[Parameter(Mandatory)][string]$Name,
[ValidateSet([Company])][string]$Company,
[ValidateSet([Department])][string]$Department,
[ValidateSet([Office])][string]$Office
)
#Do stuff
}
This works to prevent typos. But since not all companies have the same departments and offices I'd like the ValidateSet to allow different values depending on previous parameter. E.g. only allow office and department combinations which actually exist at the different companies.
Example:
Company A
|-- HR
| |-- Office A
| |-- Office B
|
|-- IT
|-- Office C
|-- Office D
Company B
|-- Design
| |-- Office E
| |-- Office F
|
|-- Finance
// ...etc
Is there a way to do this with parameters in Powershell or should I rethink the entire thing?