Forum Discussion
New PowerShell user question
- Oct 10, 2022
Dre45 Just an example, if checks if the OU exists and if it does, it removes it and recreates it at the same location:
$OUName = 'Finance' #Check if $OUName exists and record location in $oulocation try { $OUlocation = Get-ADOrganizationalUnit -Filter 'Name -like $OUName' -Properties * -ErrorAction Stop Write-Host ("{0} OU does exist, receating..." -f $OUName) -ForegroundColor Green } #Exit script if it doesn't exist catch { Write-Warning ("{0} OU does not exist" -f $OUName) break } #Remove Protected Setting from OU, remove OU, recreate in Original location try { Set-ADOrganizationalUnit -Identity $OUlocation.DistinguishedName -ProtectedFromAccidentalDeletion:$false -Confirm:$false -ErrorAction Stop Remove-ADOrganizationalUnit -Identity $OUlocation.DistinguishedName -Confirm:$false -ErrorAction Stop New-ADOrganizationalUnit -Name $OUlocation.Name -DisplayName $OUlocation.DisplayName -Path ($oulocation.DistinguishedName -replace '^.+?,(CN|OU.+)', '$1') -ErrorAction Stop Write-Host ("Recreated the {0} OU" -f $OUName) -ForegroundColor Green } #Display warning if there was an error recreating OU catch { Write-Warning ("Error creating {0}" -f $OUName) }
Dre45 Just an example, if checks if the OU exists and if it does, it removes it and recreates it at the same location:
$OUName = 'Finance'
#Check if $OUName exists and record location in $oulocation
try {
$OUlocation = Get-ADOrganizationalUnit -Filter 'Name -like $OUName' -Properties * -ErrorAction Stop
Write-Host ("{0} OU does exist, receating..." -f $OUName) -ForegroundColor Green
}
#Exit script if it doesn't exist
catch {
Write-Warning ("{0} OU does not exist" -f $OUName)
break
}
#Remove Protected Setting from OU, remove OU, recreate in Original location
try {
Set-ADOrganizationalUnit -Identity $OUlocation.DistinguishedName -ProtectedFromAccidentalDeletion:$false -Confirm:$false -ErrorAction Stop
Remove-ADOrganizationalUnit -Identity $OUlocation.DistinguishedName -Confirm:$false -ErrorAction Stop
New-ADOrganizationalUnit -Name $OUlocation.Name -DisplayName $OUlocation.DisplayName -Path ($oulocation.DistinguishedName -replace '^.+?,(CN|OU.+)', '$1') -ErrorAction Stop
Write-Host ("Recreated the {0} OU" -f $OUName) -ForegroundColor Green
}
#Display warning if there was an error recreating OU
catch {
Write-Warning ("Error creating {0}" -f $OUName)
}
That first try block will break a little too easily.
An exception isn't thrown if no search results match. Instead, $null is returned, meaning you'd have to test for and handle that (you could, of course, just trigger your own throw). For now though, $OULocation being $null will cause knock-on issues in the second block.
Similarly, if more than one organizationalUnit is returned, you'll end up with errors in the second block.
The easiest way to deal with both -in this particular context - would be to use the "-is" operator with the type returned when it's a single result.
Using the -is operator, in order from top to bottom, here's examples of:
- Multiple matches;
- No matches;
- A single match.
Cheers,
Lain