none
Capturing specific error messages RRS feed

  • Question

  • Hi All,

    I have a function that adds PC's to a machine group ..  Once added it updates a cell in my datagridview to say YES

    Snip:

    Add-ADPrincipalGroupMembership -identity (Get-ADComputer $hostname) -memberof $pcgp1
    $dataGridView1.Rows[$i - 1].Cells[5].Style.BackColor = "LightGreen"
    $dataGridView1.Rows[$i - 1].Cells[5].Value = "Yes"

    This is working, but the only issue is that if the PC is already in the group, it outputs a warning :

    'The specified account name is already a member of the group'.

    Which is then messing with my error handling and setting the output to "No" ... I want it to say "Yes" if the PC is already in the group.

    So is there a way that I can intercept the error message, and if it's just a warning as above,  ignore it ?

    I've tried various versions of IF's - Else - Try - Catch ... the below code is the latest attempt where I have set the cell value to Yes, and then the Catch section overrides that with NO if there is an actual error - this is where the warning msg is causing problems ...

    function AddPCGroups {
        $pcgp1 = "App.Windows1703Upgrade.Machine"
        #$pcgp1 = "app.testmachinegroup"
        $progressbar1.Visible = $true
        $progressbar1.Value = 0
        $progressbar1.Maximum = $numrows
        $i = 0
        $ErrorOccured = $false
        foreach ($line in $dataGridView1.DataSource) {
            $i -lt $progressbar1.Maximum; $i++
            [System.Windows.Forms.Application]::DoEvents()
            $progressbar1.Value = $i
            $progressbar1.PerformStep()
            $hostname = $line."PC Number"
            try {
                $ErrorActionPreference = 'Stop'
                Add-ADPrincipalGroupMembership -identity (Get-ADComputer $hostname) -memberof $pcgp1
                $dataGridView1.Rows[$i - 1].Cells[5].Style.BackColor = "LightGreen"
                $dataGridView1.Rows[$i - 1].Cells[5].Value = "Yes"
            }
            catch {
                
                $dataGridView1.Rows[$i - 1].Cells[5].Style.BackColor = "Orange"
                $dataGridView1.Rows[$i - 1].Cells[5].Value = "No"
            } 
            
            $dataGridView1.refresh()
        }
        $progressbar1.Visible = $false
    }

    Help or direction appreciated as always

    Damian

    Friday, November 24, 2017 12:26 PM

Answers

  • if(Get-ADGroupMember $pcgp1 |?{$_.Name -eq $hostname}){
         # computer already a member of group
    }else{
         # add to group
    }
    


    \_(ツ)_/

    • Marked as answer by DamianBz Saturday, November 25, 2017 11:36 AM
    Friday, November 24, 2017 1:49 PM

All replies

  • if(Get-ADGroupMember $pcgp1 |?{$_.Name -eq $hostname}){
         # computer already a member of group
    }else{
         # add to group
    }
    


    \_(ツ)_/

    • Marked as answer by DamianBz Saturday, November 25, 2017 11:36 AM
    Friday, November 24, 2017 1:49 PM
  • Ah .. Yes, I was over thinking the capture of the error message !!

    This make much more sense :)

    Thanks again jrv 

    Saturday, November 25, 2017 11:36 AM