Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot catch ADException in Powershell script
    primarykey
    data
    text
    <p>I'm using the ActiveDirectory Powershell module to add some groups to our domain. During this process, the script might attempt to add a user to a group they're already a member of. This is an exception that I should be able to handle, but, I cannot seem to catch it. No matter what I do, I see red text in the powershell console.</p> <p>I simplified this all down to a few testable steps.</p> <pre><code>New-ADGroup -Path "ou=temp,dc=domain,dc=tld" -GroupScope Global -Identity testGroup Get-ADGroup -Identity testGroup | Add-ADGroupMember -Members testUser try { Get-ADGroup -Identity testGroup | Add-ADGroupMember -Members testUser } catch { Write-Warning "Caught exception!" } </code></pre> <p>I expect to see "Caught exception!" in yellow text, however, what I actually get is:</p> <pre><code>Add-ADGroupMember : The specified account name is already a member of the group At line:1 char:40 + try {Get-ADGroup -Identity testGroup | Add-ADGroupMember -Members testUser} ca ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (CN=testGroup,OU=Temp,DC=domain,DC=tld:ADGroup) [Add-ADGroupMember], ADException + FullyQualifiedErrorId : The specified account name is already a member of the group,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember </code></pre> <p>All in red text. </p> <p>I expect that I'm overlooking something simple. I'd appreciate any input.</p> <p>Edit: PS version information as requested:</p> <pre><code>C:\&gt; $PSVersionTable Name Value ---- ----- PSVersion 3.0 WSManStackVersion 3.0 SerializationVersion 1.1.0.1 CLRVersion 4.0.30319.18331 BuildVersion 6.2.9200.16481 PSCompatibleVersions {1.0, 2.0, 3.0} PSRemotingProtocolVersion 2.2 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload