locked
Clearing AD MSRtcsip Attributes , Powershell NEWB RRS feed

  • Question

  • I need to clear all MSRTCsip attributes for a specific set of users for a cutover migration of Skype for Business. I need to filter these users by their email addresses.

    My .csv is formatted as

    Email
    UserA@Domain.com
    UserB@Domain.com

    Here is my current script, I have only added one attribute for the testing process so far but I cannot seem to get it to work.

    $Users = Import-Csv c:\Scripts\Test.csv
     
    Foreach ($User in $Users){
      $AD = Get-ADUser -Filter {Mail -eq $User.email}
      Write-Host “User:” $User.email
     $AD | Set-ADUser -clear msRTCSIP-PrimaryUserAddress
    }

    The errors I am getting are as follows

    Get-ADUser : Property: 'email' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:4 char:9
    +   $AD = Get-ADUser -Filter {Mail -eq $User.email}
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
       ands.GetADUser

    Then it Provides

    User: UserA@domain.com

    Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the
    argument, and then try running the command again.
    At line:6 char:8
    +  $AD | Set-ADUser -clear msRTCSIP-PrimaryUserAddress
    +        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

    Get-ADUser : Property: 'email' not found in object of type: 'System.Management.Automation.PSCustomObject'.
    At line:4 char:9
    +   $AD = Get-ADUser -Filter {Mail -eq $User.email}
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Comm
       ands.GetADUser

    I am trying to run this on a Windows 10 machine with PS Version 5, any help will be greatly appreciated.

    Wednesday, June 14, 2017 1:44 PM

Answers

  • Try it this way

    Import-Csv c:\Scripts\Test.csv | Foreach {
      Write-Host “User:” $_.email
      Get-ADUser -Filter "Mail -eq '$($_.email)'" | Set-ADUser -clear msRTCSIP-PrimaryUserAddress
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, June 14, 2017 1:54 PM typo
    • Marked as answer by BBHadd85 Wednesday, June 14, 2017 2:40 PM
    Wednesday, June 14, 2017 1:52 PM
  • Rather then clearing them out, you can use the -Replace parameter and supply it a hash table of properties/values

    $hash = @{
      property1 = 'value'
      propert2 = 'value'
    }
    
    Set-ADUser -Replace $hash

    Set-ADUser Help


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, June 14, 2017 5:44 PM Added link
    • Marked as answer by BBHadd85 Wednesday, June 14, 2017 6:04 PM
    Wednesday, June 14, 2017 5:44 PM
  • This can all be accomplished with one script, no need to make your life difficult. The error you are getting is because the object being passed over to Set-ADUser is from Get-ADUser, therefore $_.msRTCSIP-PrimaryUserAddress does not exist.

    You will need to save the pipeline object from Impotr-Csv to Foreach in a variable

    Import-Csv Myfile | Foreach {
      $csv = $_
    
      ..... $csv.Property
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:58 PM
    Thursday, June 15, 2017 2:08 PM
  • Like below, but if the property already contains a value then -Add will fail, and you need to use -Replace

    Import-Csv c:\scripts\testsip.csv | Foreach {
    $csv = $_
    Write-Host "User:" $csv.email
    Write-Host "msRTCSIP-PrimaryUserAddress:" $csv.msRTCSIP-PrimaryUserAddress
    Get-ADUser -Filter "Mail-eq '$($csv.email)'" | Set-ADUser -Add $csv.msRTCSIP-PrimaryUserAddress -WhatIf
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:59 PM
    Thursday, June 15, 2017 3:22 PM
  • $csv.'msRTCSIP-PrimaryUserAddress' the - is affecting it, so have to add quotes

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:59 PM
    Thursday, June 15, 2017 4:19 PM
  • Try this for a final script to do it all in one

    Import-Csv c:\scripts\testsip.csv | Foreach {
      $csv = $_
      Write-Host "User:" $csv.email
      Write-Host "msRTCSIP-PrimaryUserAddress:" $csv.'msRTCSIP-PrimaryUserAddress'
      Get-ADUser -Filter "Mail -eq '$($csv.email)'" | 
        Set-ADUser -Add @{ProxyAddresses = $csv.ProxyAddress} -Replace @{'msRTCSIP-PrimaryUserAddress' = $csv.'msRTCSIP-PrimaryUserAddress'} -WhatIf
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:59 PM
    Thursday, June 15, 2017 4:29 PM

All replies

  • Try it this way

    Import-Csv c:\Scripts\Test.csv | Foreach {
      Write-Host “User:” $_.email
      Get-ADUser -Filter "Mail -eq '$($_.email)'" | Set-ADUser -clear msRTCSIP-PrimaryUserAddress
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, June 14, 2017 1:54 PM typo
    • Marked as answer by BBHadd85 Wednesday, June 14, 2017 2:40 PM
    Wednesday, June 14, 2017 1:52 PM
  • That worked perfectly.  Thanks.  Can you explain why I was getting the error/what I was doing wrong?
    Wednesday, June 14, 2017 2:41 PM
  • I believe the main issue was with your filter, so you were not actually returning an object.

    {Mail -eq $User.Email} <-- Incorrect way

    {Mail -eq "$($User.Email)"} or "Mail -eq '$($User.Email)'" <-- Dont think the first method is used anymore, the second method is what you'll see now a days


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''



    • Edited by clayman2 Wednesday, June 14, 2017 2:52 PM typo
    Wednesday, June 14, 2017 2:43 PM
  • Thanks, is there an easy way for me to readd new ones after I have cleared them. 

    Right now they are sip:username@domainA.com and they need to be sip:username@DomainB.com

    On top of that I also need to add a proxy address of sip:username@DomainB.com

    Wednesday, June 14, 2017 5:32 PM
  • Rather then clearing them out, you can use the -Replace parameter and supply it a hash table of properties/values

    $hash = @{
      property1 = 'value'
      propert2 = 'value'
    }
    
    Set-ADUser -Replace $hash

    Set-ADUser Help


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Wednesday, June 14, 2017 5:44 PM Added link
    • Marked as answer by BBHadd85 Wednesday, June 14, 2017 6:04 PM
    Wednesday, June 14, 2017 5:44 PM
  • How would I add that into the entire script.  Sorry I'm extremely raw at powershell as I have only had to start using it in the last 6 weeks.
    Wednesday, June 14, 2017 6:24 PM
  • Will the value from the email field in your CSV file, go in both properties? If so then something like the following

    Import-Csv c:\Scripts\Test.csv | Foreach {
      $hash = @{
        'msRTCSIP-PrimaryUserAddress' = $_.'msRTCSIP-PrimaryUserAddress'
        proxyAddresses = $_.'msRTCSIP-PrimaryUserAddress'
      }
      Get-ADUser -Filter "Mail -eq '$($_.email)'" | Set-ADUser -Replace $hash
    }

    You may want to be careful on the Proxy Address, as when I look at mine it is an array with multiple settings, so unless you just have the one setting or nothing, you should be fine. Otherwise, you may have to approach that in a different way


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''



    • Edited by clayman2 Wednesday, June 14, 2017 6:56 PM edit script
    Wednesday, June 14, 2017 6:37 PM
  • Okay I may have to do some more research on this because our hybrid environment is a little messy after multiple acquisitions and multiple Domains.

    csv = email addresses email addresses = username@domainA.com

    msRTCSIP-PrimaryUserAddress = sip:username@domainA.com but needs to be changed to sip:username@domainB.com

    Mail will remain at username@domainA.com

    Under ProxyAddresses there currently is not an sip proxy address so a proxyaddress of sip:username@domainB.com needs to be added for Skype for Business to work.

    Sorry for being a pain but this has had me beating my head on my desk.

    Wednesday, June 14, 2017 6:49 PM
  • It's all good, I modified the script so that the hash table uses the correct csv field. You can add more properties to the hash table if needed.

    You just need to do a quick check for your environment on the ProxyAddresses field, as when I checked mine there are multiple values, so if I were to replace what was there with just the sip:name@domain.com, I would create a bigger issue.

    Get-ADUser <userName> -Prop ProxyAddresses | Select -ExpandProperty ProxyAddresses

    If the above returns more than one value, you do not want to replace that property.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, June 14, 2017 6:58 PM
  • Yeah, we have tons of proxy addresses in our environment.  I was tasked with Taking over Office365 kind of blindly so I am really trying to learn on the fly.
    Wednesday, June 14, 2017 7:02 PM
  • So we would need to get the user with that property, in order to get the current value and add the new value with the current value, before passing it over to the replace parameter of Set-ADUser.

    I am heading home for the day, so see what you can come up with and I'll check again tomorrow, unless some else chimes in.

    Don't be afraid of PowerShell, embrace it. Play around with the script I provided and look on google for other things. You can always add the -WhatIf switch to Set-ADUser, therefore no changes ever take place but it will show you what will happen if it was not present.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Wednesday, June 14, 2017 7:09 PM
  • Okay, I think what I want to do just because of the complexity of our enviornment is create 2 or 3 scripts.  The original answer you have provided is perfect for the first for clearing the msRTCSIP attributes. 

    The Next script would be to add the new msRTCSIP-PrimaryUserAddress attribute and add a new proxy address attribute if that can be achieved in the same script.  I would be using a different csv file after clearing the original attributes.  Does this csv format look right?

    email,msRTCSIP-PrimaryUserAddress,ProxyAddress
    username@domainA.com,sip:username@domainB.com,sip:username@domainB.com

    This is what I have tried in the script so far but I keep getting an error.  I haven't tried to add the proxy address in there yet because I was still testing.  Error is

    Set-ADUser : Cannot validate argument on parameter 'Add'. The argument is null
    or empty. Provide an argument that is not null or empty, and then try the
    command again.
    At line:4 char:62
    + ... -Filter "Mail-eq '$($_.email)'" | Set-ADUser -Add $_.msRTCSIP-Primary ...

    Here is my script

    Import-Csv c:\scripts\testsip.csv | Foreach {
    Write-Host "User:" $_.email
    Write-Host "msRTCSIP-PrimaryUserAddress:" $_.msRTCSIP-PrimaryUserAddress
    Get-ADUser -Filter "Mail-eq '$($_.email)'" | Set-ADUser -Add $_.msRTCSIP-PrimaryUserAddress -WhatIf
    }

    Thursday, June 15, 2017 2:01 PM
  • This can all be accomplished with one script, no need to make your life difficult. The error you are getting is because the object being passed over to Set-ADUser is from Get-ADUser, therefore $_.msRTCSIP-PrimaryUserAddress does not exist.

    You will need to save the pipeline object from Impotr-Csv to Foreach in a variable

    Import-Csv Myfile | Foreach {
      $csv = $_
    
      ..... $csv.Property
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:58 PM
    Thursday, June 15, 2017 2:08 PM
  • Where would I insert that in the script?

    Thursday, June 15, 2017 2:24 PM
  • Like below, but if the property already contains a value then -Add will fail, and you need to use -Replace

    Import-Csv c:\scripts\testsip.csv | Foreach {
    $csv = $_
    Write-Host "User:" $csv.email
    Write-Host "msRTCSIP-PrimaryUserAddress:" $csv.msRTCSIP-PrimaryUserAddress
    Get-ADUser -Filter "Mail-eq '$($csv.email)'" | Set-ADUser -Add $csv.msRTCSIP-PrimaryUserAddress -WhatIf
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:59 PM
    Thursday, June 15, 2017 3:22 PM
  • Thanks, I was pretty close that time but unfortunately it is not working.

    Set-ADUser : A parameter cannot be found that matches parameter name
    'PrimaryUserAddress'.
    At line:5 char:80
    + ... mail)'" | Set-ADUser -change $csv.msRTCSIP-PrimaryUserAddress -WhatIf
    +                                               ~~~~~~~~~~~~~~~~~~~

    Any idea of why it is thinking that is a parameter?

    Thursday, June 15, 2017 4:05 PM
  • $csv.'msRTCSIP-PrimaryUserAddress' the - is affecting it, so have to add quotes

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:59 PM
    Thursday, June 15, 2017 4:19 PM
  • Try this for a final script to do it all in one

    Import-Csv c:\scripts\testsip.csv | Foreach {
      $csv = $_
      Write-Host "User:" $csv.email
      Write-Host "msRTCSIP-PrimaryUserAddress:" $csv.'msRTCSIP-PrimaryUserAddress'
      Get-ADUser -Filter "Mail -eq '$($csv.email)'" | 
        Set-ADUser -Add @{ProxyAddresses = $csv.ProxyAddress} -Replace @{'msRTCSIP-PrimaryUserAddress' = $csv.'msRTCSIP-PrimaryUserAddress'} -WhatIf
    }


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by BBHadd85 Thursday, June 15, 2017 4:59 PM
    Thursday, June 15, 2017 4:29 PM
  • When I first Tried it, it didn't work.  I added Write-Host "ProxyAddresses:" $csv.ProxyAddress and it worked perfectly. 

    Import-Csv c:\scripts\testsip.csv | Foreach {
      $csv = $_
      Write-Host "User:" $csv.email
      Write-Host "msRTCSIP-PrimaryUserAddress:" $csv.'msRTCSIP-PrimaryUserAddress'
      Write-Host "ProxyAddresses:" $csv.ProxyAddress
      Get-ADUser -Filter "Mail -eq '$($csv.email)'" | 
        Set-ADUser -Add @{ProxyAddresses = $csv.ProxyAddress} -Replace @{'msRTCSIP-PrimaryUserAddress' = $csv.'msRTCSIP-PrimaryUserAddress'} -WhatIf
    }

    Thanks again for all of your Help.  Whatever Company you work for is lucky to have you, as you're an extremely valuable asset.

    Thursday, June 15, 2017 4:58 PM
  • Write-Host should not have an effect on things so that is weird. Glad we got it to work, don't forget to remove the -WhatIf switch for the changes to actually take affect.

    Thanks for the kind words, I am a public servant :-)


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Thursday, June 15, 2017 5:12 PM