I have a few selected items which the user selected in a gui script, those represent GPO now i have the OU he would like to link them to. Code works but i want to result to the user which were link and which couldnt be linked because they are ALREADY linked But this try catch wont do that for some reason, the gui output the succssesful links and write to console errors where the links already exist
$ResultsTextBox.clear()
#$listBox1.selecteditems
$SWITCH = Get-ADOrganizationalUnit -filter * -Property CanonicalName | Where-Object {$_.CanonicalName -eq $listBox2.SelectedItem}
forEach ($line in $listBox1.selecteditems){
#Link each selected item GPO to the OU
try {
New-GPlink -name $line -target $SWITCH
$ResultsTextBox.AppendText("`n GPO: $line HAVE BEEN LINKED Successfully.`n")
}
catch{$ResultsTextBox.AppendText("`n COULDN NOT LINK GPO: $line TO $SWITCH `n")
}
}
Whats wrong here?