I was asked to create a Windows PowerShell script [easy enough for me, the scriptingnoob]. This script needed to create a blue-print of Active Directory sub-OU’s within several OU’s.
|- Organization1
|- Accounts
|- Groups
|- Resources
|- Organization2
|- Accounts
|- Groups
|- Resources
First I wanted the script to accept variables from the cmdlet;
c:\script.ps1 Organisation1 “Organisation1_description”
By starting the script with param, Organisation1 will be set as variable $org_name, and “Organisation1_description” will be set as $org_descr.
param($org_name,$org_descr)
This can be used later in the script..
New-ADOrganizationalUnit -Name $org_name -Path “DC=domain,DC=local” -Description “$org_descr”
New-ADOrganizationalUnit -Name “Accounts” -Path “OU=$org_name,OU=Organizations,DC=domain,DC=lan”
Secondly I wanted to build an “ask-to-proceed” function within the script, because this is a script that should not be used lightly. Also because “it was possible” 🙂 [nl- omdat het kan]
#The Question
$message = “Do you want to continue?”
#Option1 with explanation
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”, `
“If you choose yes, we will continue”
#Option2 with explanation
$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”, `
“If you choose no, we will stop”
#Show the options
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
#What happens after the chosen
switch ($result)
{
0 {“You selected Yes.”}
1 {“You selected No.”}
}
If you paste the following in a ps1-file then you will be asked to continue, with the options for help.