none
Determine if Get-CsUser exists RRS feed

  • Question

  • All,

    Hopefully this is an easy one for Friday.  I am working with a script that either enables a brand new user if they do not exist for Lync or re-enable the user if they were disabled.  I am having a problem with "$CS=Get-CsUser –Identity $username  -DomainController $targetdc".  If the user does not exist, it always throws "Management object not found for identity acme\jsmith".

    I just want to see if the user exists, if so re-enable via Set-CsUser, otherwise Enable-CsUser.

    Code:

    #This line always throws error if no user found

    $CSUser= Get-CsUser –Identity $username  -DomainController $targetdc | Measure

    #This line reached if user found     

          If ($CSUser -eq 0 )
          {
           Enable-CsUser -Identity $username -RegistrarPool $boaPool -SipAddress $sipID -DomainController $targetdc -ErrorAction Stop            
             echo 'Do enable'
           }
           else
           {
           Set-CsUser –Identity $username –Enabled $True  -DomainController $targetdc  -ErrorAction Stop
           Get-CsUser –Identity $username  -DomainController $targetdc 
          
           }

    Cheers!

    Friday, August 10, 2012 2:36 PM

Answers

  • You inspired me to go make a version that would act on one or more specific error messages.  http://mikestacy.typepad.com/mike-stacys-blog/2012/08/a-basic-method-for-catching-specific-lync-and-other-powershell-script-errors.html

    Hopefully either mine or Alanmad's suggestion was helpful.


    Mike Stacy | http://mikestacy.typepad.com

    • Marked as answer by FIM_Admin Wednesday, August 15, 2012 1:57 PM
    Monday, August 13, 2012 8:32 PM
    Moderator
  • Hi,

    You can use the Get-CsADUser to get the user then check if it is a enabled lync user or disable lync user:

    $User= Get-CsAdUser –Identity $username  -DomainController $targetdc
    If($User.Enabled -ep $null){
    Enable-CsUser -Identity $username -RegistrarPool $boaPool -SipAddress $sipID -DomainController $targetdc
    Write-Host "Do Enable!"
    }

    elseif($User.Enabled -eq $false)
    {
    Set-CsUser –Identity $username –Enabled $True  -DomainController $targetdc
    Get-CsUser –Identity $username  -DomainController $targetdc 
    }
    else
    {
    Write-Host "The user has been enabled!"
    }


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Marked as answer by FIM_Admin Wednesday, August 15, 2012 1:57 PM
    Tuesday, August 14, 2012 4:51 AM
    Moderator

All replies

  • You could use the get-csaduser command. You can use a filter enabled -ne $true.

    Friday, August 10, 2012 3:44 PM
  • You can grab the error as a variable and check it to see how to proceed.  Try this:


    $CSUser= Get-CsUser –Identity $username  -DomainController $targetdc -errorvariable +err  | Measure
    if ($err -eq "$null")
          {
           Enable-CsUser -Identity $username -RegistrarPool $boaPool -SipAddress $sipID -DomainController $targetdc -ErrorAction Stop             
             echo 'Do enable'
           }
           else
           {
           Set-CsUser –Identity $username –Enabled $True  -DomainController $targetdc  -ErrorAction Stop 
           Get-CsUser –Identity $username  -DomainController $targetdc  
           }

    You will still see the error in the console but you can process against it.  Of course it would be better to actually validate that the error contains the text you are looking for but this should give you the basic structure you need to create that.


    Mike Stacy | http://mikestacy.typepad.com


    Friday, August 10, 2012 4:18 PM
    Moderator
  • You inspired me to go make a version that would act on one or more specific error messages.  http://mikestacy.typepad.com/mike-stacys-blog/2012/08/a-basic-method-for-catching-specific-lync-and-other-powershell-script-errors.html

    Hopefully either mine or Alanmad's suggestion was helpful.


    Mike Stacy | http://mikestacy.typepad.com

    • Marked as answer by FIM_Admin Wednesday, August 15, 2012 1:57 PM
    Monday, August 13, 2012 8:32 PM
    Moderator
  • Hi,

    You can use the Get-CsADUser to get the user then check if it is a enabled lync user or disable lync user:

    $User= Get-CsAdUser –Identity $username  -DomainController $targetdc
    If($User.Enabled -ep $null){
    Enable-CsUser -Identity $username -RegistrarPool $boaPool -SipAddress $sipID -DomainController $targetdc
    Write-Host "Do Enable!"
    }

    elseif($User.Enabled -eq $false)
    {
    Set-CsUser –Identity $username –Enabled $True  -DomainController $targetdc
    Get-CsUser –Identity $username  -DomainController $targetdc 
    }
    else
    {
    Write-Host "The user has been enabled!"
    }


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    • Marked as answer by FIM_Admin Wednesday, August 15, 2012 1:57 PM
    Tuesday, August 14, 2012 4:51 AM
    Moderator
  • All, thanks for the suggestions I will take a look at the solutions and let you know.
    Tuesday, August 14, 2012 2:21 PM
  • Mike and Sean, thanks for your help.  Both solutions worked great and I am glad I was the inspiration for the blog post.  Thanks again.
    Wednesday, August 15, 2012 1:58 PM