Forum Discussion
New PowerShell user question
Good afternoon everyone.
Doing a task for an assignment and I can get the below to answer if the Finance OU is created or not. How could I structure my next if/else statement to delete the OU if it's present with a message stating so and then recreate the OU saying it was created. Could I use a if/else/if else commands? Thank you.
if([bool] (Get-ADOrganizationalUnit -Filter * | ? {$_.Name -eq $OUName} ))
{ Write-Host 'Finance OU exists' }
else { Write-Host 'Finance OU does not exists' }
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) }
2 Replies
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) }- LainRobertsonSilver Contributor
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