none
How to break a loop if user doesn't exist RRS feed

  • Question

  • For provision new remote mailboxes in a E2010SP3 \ EXO hybrid I want to take a few actions on members of a DL.  If the cmdlet finds the mailbox doesn't exist I want it to break from the loop, and move on to the next user.  When the cmdlet fails because the mailbox isn't found it errors with, "The operation couldn't be performed because object 'UserA' couldn't be found on 'server1'"

    Here's some of the script:

    $DLMember = Get-DistributionGroupMember -Identity ProvisionGroup

    foreach ($member in $DLMember | ?{$_.recipienttypedetails -eq "RemoteUserMailbox"})

        {

            # Set-CASMailbox $member.PrimarySmtpAddress -ActiveSyncEnabled $False

            # Set-Mailbox $member.alias -AuditDelegate SendOnBehalf,Move,MovetoDeletdItems

            # Set-MsolUser -UserPrincipalName $member.userprincipalname -UsageLocation US

            # Set-MsolUserLicense $member.alias -AddLicenses "<domain:License_Name>"     

        }


    Wednesday, August 9, 2017 9:13 PM

Answers

  • This will work for me.  I must have done something wrong on my initial test with this, or didn't realize it was working  Try\Catch isn't working, and neither is parameter errorvariable apparently because of something with O365 error handling.

    --------------$DLMember = Get-DistributionGroupMember -Identity ProvisionGroup

    foreach ($member in $DLMember)

        {

            $MB = Get-Mailbox $member

            if(!$MB){BREAK}

        }
    --------------

    Wednesday, August 9, 2017 9:51 PM

All replies

  • All of the code is commented out. How can it produce any errors?

    Without correct information and a complete error it is not possible to understand your question.


    \_(ツ)_/

    Wednesday, August 9, 2017 9:30 PM
  • This will work for me.  I must have done something wrong on my initial test with this, or didn't realize it was working  Try\Catch isn't working, and neither is parameter errorvariable apparently because of something with O365 error handling.

    --------------$DLMember = Get-DistributionGroupMember -Identity ProvisionGroup

    foreach ($member in $DLMember)

        {

            $MB = Get-Mailbox $member

            if(!$MB){BREAK}

        }
    --------------

    Wednesday, August 9, 2017 9:51 PM
  • Use Try / Catch  https://www.vexasoft.com/blogs/powershell/7255220-powershell-tutorial-try-catch-finally-and-error-handling-in-powershell
    Thursday, August 10, 2017 8:17 AM
  • "ErrorAction" is what you want and not "ErrorVariable".

    foreach ($member in $DLMember) {
    	if($MB = Get-Mailbox $member -ErrorAction 0){
    		break
        }
    }
    
    Learn PowerShell now before it is too late.


    \_(ツ)_/

    Thursday, August 10, 2017 2:44 PM
  • Learn how to communicate with people effectively and respectfully before giving advise or commands.  I think that's about equivalent to your comment.  I encourage you to never comment on my posts, and I will look for other forums. 
    Thursday, August 10, 2017 5:27 PM
  • Learn how to communicate with people effectively and respectfully before giving advise or commands.  I think that's about equivalent to your comment.  I encourage you to never comment on my posts, and I will look for other forums. 

    It would still help you if you took the time to learn the bare basics of the technology.  It is a helpful suggestion.  Of course you can conti nue on guessing if you choose.


    \_(ツ)_/

    Thursday, August 10, 2017 5:29 PM
  • suffice it to say I have time for certain things at certain times just like yourself. Your passive aggressive, disrespectful and insulting comments are of no positive use to me, and so I don't want your help. check yourself... you either fully understand how you come off, making you the jerk I think you are, or you don't, in which case I suggest you take some critical feedback and do some self-eval. I don't want help from someone that responds like you do.
    Thursday, August 10, 2017 5:37 PM
  • suffice it to say I have time for certain things at certain times just like yourself. Your passive aggressive, disrespectful and insulting comments are of no positive use to me, and so I don't want your help. check yourself... you either fully understand how you come off, making you the jerk I think you are, or you don't, in which case I suggest you take some critical feedback and do some self-eval. I don't want help from someone that responds like you do.

    When does a suggestion to learn something count as an insult? 

    I gave that advise because so many people who want to become techs are not capable of studying anything.  They can only learn by asking questions which is not a possible way to learn a technology if you do not know the basics.  Of course if you already know all of the answers then why are you asking a this question?  Why did you not use help to find the correct parameters?  Why was your original question technically ambiguous.

    Stop defending your ego and take the advise .... or just be an like that.


    \_(ツ)_/

    Thursday, August 10, 2017 5:58 PM
  • I'm going to take your inquiry seriously for some reason.  A suggestion counts as an insult when it's framed in an insulting way, or it characterizes the person, as if you know enough about me to say anything, or it's a command rather than a suggestion. Do you think saying, "Learn PowerShell now before it is too late." is somehow helpful? It's really, really not. I ask questions when I've spent time on something and need help from people I hope are knowledgeable, have empathy, and care to help.  Getting insulted, or talked down to while getting that help doesn't make sense.  You responded to most if not all of questions with a similar tone.  I can accept my question and knowledge not being up to par, or needing adjustment, but there are helpful ways to request more info, to express confusion, or to just not comment.  Can you accept criticism for how you do things?  It doesn't seem like it to me. 

    Stop defending your need to feel superior.  Neither of us have to worry about bumping into each other again.  I'm done with this forum.

    Thursday, August 10, 2017 8:05 PM
  • Here is a good article outlining how to become a PowerShell scripter.  It covers many of the most important dos and don'ts.

    https://blogs.msdn.microsoft.com/daviddasneves/2017/08/06/how-to-become-a-powershell-pro-v2/


    \_(ツ)_/

    Thursday, August 10, 2017 8:24 PM