ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    PowerShell - Using Variables to Delete SMTP Proxy Addresses in AD

    IT Discussion
    powershell ad active directory windows get-aduser
    3
    11
    3.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • wrx7mW
      wrx7m
      last edited by

      I am trying to create a script that will allow me to delete an SMTP proxy address on an AD user object, after prompting for some info. I have this, but it doesn't want to create the smtp address using the variables-

      Import-Module ActiveDirectory
      $SamAccountName = Read-Host -Prompt "SamAccountName"
      $Domain = Read-Host -Prompt "Type the domain of the address you wish to DELETE"
      $GivenName = Get-ADUser -Identity "$SamAccountName" -Properties GivenName | Select-Object GivenName
      $Surname = Get-ADUser -Identity "$SamAccountName" -Properties Surname | Select-Object Surname
      
      Set-ADUser -Identity $SamAccountName `
                 -Remove @{proxyAddresses="smtp:$GivenName.$Surname@$Domain"} `
                 -Replace @{telephonenumber="8"}
      

      If I Write-Host on Set-ADUser, I get -

      Set-ADUser -Identity jacks -Remove System.Collections.DictionaryEntry -Replace System.Collections.DictionaryEntry
      

      Obviously, it isn't getting the correct attributes to build out the address.

      If I run the Get-ADUser commands by themselves, it outputs the correct info.

      Get-ADUser -Identity "$SamAccountName" -Properties GivenName | Select-Object GivenName
      
      GivenName
      ---------
      Jack  
      

      Where am I screwing up?

      1 Reply Last reply Reply Quote 1
      • F
        flaxking
        last edited by flaxking

        Probably either

        "smtp:$($GivenName).$($Surname)@$($Domain)"
        

        Or

        ('smtp:' + $GivenName + '.' + $Surname + '@' + $Domain)
        
        wrx7mW 1 Reply Last reply Reply Quote 1
        • F
          flaxking
          last edited by

          Did you test your string creation on its own?

          wrx7mW 1 Reply Last reply Reply Quote 0
          • wrx7mW
            wrx7m @flaxking
            last edited by

            @flaxking said in PowerShell - Using Variables to Delete SMTP Proxy Addresses in AD:

            Probably either

            "smtp:$($GivenName).$($Surname)@$($Domain)"
            

            Or

            ('smtp:' + $GivenName + '.' + $Surname '@' + $Domain)
            

            The first option yields the same results. The second, errors out.

            F 1 Reply Last reply Reply Quote 0
            • wrx7mW
              wrx7m @flaxking
              last edited by

              @flaxking said in PowerShell - Using Variables to Delete SMTP Proxy Addresses in AD:

              Did you test your string creation on its own?

              I have not and am not exactly sure how to.

              1 Reply Last reply Reply Quote 0
              • F
                flaxking @wrx7m
                last edited by

                @wrx7m said in PowerShell - Using Variables to Delete SMTP Proxy Addresses in AD:

                @flaxking said in PowerShell - Using Variables to Delete SMTP Proxy Addresses in AD:

                Probably either

                "smtp:$($GivenName).$($Surname)@$($Domain)"
                

                Or

                ('smtp:' + $GivenName + '.' + $Surname '@' + $Domain)
                

                The first option yields the same results. The second, errors out.

                Yeah, the second one was missing a +

                1 Reply Last reply Reply Quote 0
                • F
                  flaxking
                  last edited by

                  You're also trying to put an object into a string, which will only work if that object it set to print out the value you're looking for by default

                  1 Reply Last reply Reply Quote 0
                  • F
                    flaxking
                    last edited by flaxking

                    So in order to access the GivenName property on the object you have you would do $GivenName.GivenName

                    you could just do:

                    $User = Get-ADUser -Identity $SamAccountName
                    $User.GivenName
                    $User.Surname
                    

                    in which case your smtp string would be "smtp:$($User.GivenName).$($User.Surname)@$Domain"

                    1 Reply Last reply Reply Quote 2
                    • F
                      flaxking
                      last edited by

                      Powershell is object oriented, which is super important to realize when working with it, and that is what usually gives people the problem with it, if they do not have previous experience with objects. It makes a big learning curve increase.

                      On you 'Write-Host' test on your Set-ADUser command, you see "System.Collections.DictionaryEntry" because that is telling you what object is there. You're creating dictionary objects, so it's not going to automatically write out the contents of the dictionary.

                      wrx7mW 1 Reply Last reply Reply Quote 1
                      • ObsolesceO
                        Obsolesce
                        last edited by

                        So what you're looking at now is something like this:

                        Import-Module ActiveDirectory
                        $SamAccountName = Read-Host -Prompt "SamAccountName"
                        $Domain = Read-Host -Prompt "Type the domain of the address you wish to DELETE"
                        $user = Get-ADUser -Identity $SamAccountName
                        Set-ADUser $user -Remove @{proxyAddresses="smtp:$($user.GivenName).$($user.Surname)@$Domain"}
                        
                        1 Reply Last reply Reply Quote 2
                        • wrx7mW
                          wrx7m @flaxking
                          last edited by

                          @flaxking said in PowerShell - Using Variables to Delete SMTP Proxy Addresses in AD:

                          if they do not have previous experience with objects

                          Describes me. lol

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post