Hi all,<\/p>\n

Advertisement

I need to run a powershell script against nearly 4000 user accounts which will meet the following conditions:<\/p>\n

    \n
  1. \n
    Advertisement

    Search user proxyAddress for SMTP attribute<\/p>\n<\/li>\n

  2. \n

    IF SMTP proxyAddress = “*@domain.org<\/span>” then add an alias smtp proxyAddress =“smtp:”+$user.sAMAccountName+\"@domain.net<\/a>\"<\/p>\n<\/li>\n<\/ol>\n

    This is what I’m working off so far… it doesn’t generate any errors for me, but does not complete the task as required.<\/p>\n

    Any assistance would be greatly appreciated!<\/p>\n

    $userou = “OU=Ontario,DC=domain,DC=local”
    \n$users = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName,ProxyAddresses<\/p>\n

    Foreach ($user in $users)<\/p>\n

    {<\/p>\n

    If ($PrimarySMTP -like “*@domain.org<\/span>”)
    \n{Set-ADUser -Identity $user.samaccountname -Add @{proxyAddresses=“
    smtp:“+$user.sAMAccountName+”@domain.net<\/a>”}}<\/p>\n

    }<\/p>\n

      \n
    • I’m sure \" If ($PrimarySMTP -like “*@domain.org<\/span>”)\" should have the notation $True in there somewhere, but I’m not sure how to apply it.<\/li>\n<\/ul>","upvoteCount":6,"answerCount":10,"datePublished":"2019-05-24T15:18:17.000Z","author":{"@type":"Person","name":"simonfanning3565","url":"https://community.spiceworks.com/u/simonfanning3565"},"acceptedAnswer":{"@type":"Answer","text":"

      If you get the desired output with<\/p>\n

       Get-ADUser -Filter \"ProxyAddresses -like '*@domain.org'\" -SearchBase \"OU=Ontario,DC=domain,DC=local\" -Properties ProxyAddresses|select Name,proxyaddress\n<\/code><\/pre>\n

      than this should work<\/p>\n

       Get-ADUser -Filter \"ProxyAddresses -like '*@domain.org'\" -SearchBase \"OU=Ontario,DC=domain,DC=local\" -Properties ProxyAddresses |Foreach{\n$smtp=\"smtp:\"+$_.samaccountname + \"@domain.net\"\nSet-ADUser -Identity $_.samaccountname -Add @{proxyAddresses=$SMTP}\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-05-25T01:44:52.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/7","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},"suggestedAnswer":[{"@type":"Answer","text":"

      Hi all,<\/p>\n

      I need to run a powershell script against nearly 4000 user accounts which will meet the following conditions:<\/p>\n

        \n
      1. \n

        Search user proxyAddress for SMTP attribute<\/p>\n<\/li>\n

      2. \n

        IF SMTP proxyAddress = “*@domain.org<\/span>” then add an alias smtp proxyAddress =“smtp:”+$user.sAMAccountName+\"@domain.net<\/a>\"<\/p>\n<\/li>\n<\/ol>\n

        This is what I’m working off so far… it doesn’t generate any errors for me, but does not complete the task as required.<\/p>\n

        Any assistance would be greatly appreciated!<\/p>\n

        $userou = “OU=Ontario,DC=domain,DC=local”
        \n$users = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName,ProxyAddresses<\/p>\n

        Foreach ($user in $users)<\/p>\n

        {<\/p>\n

        If ($PrimarySMTP -like “*@domain.org<\/span>”)
        \n{Set-ADUser -Identity $user.samaccountname -Add @{proxyAddresses=“
        smtp:“+$user.sAMAccountName+”@domain.net<\/a>”}}<\/p>\n

        }<\/p>\n

          \n
        • I’m sure \" If ($PrimarySMTP -like “*@domain.org<\/span>”)\" should have the notation $True in there somewhere, but I’m not sure how to apply it.<\/li>\n<\/ul>","upvoteCount":6,"datePublished":"2019-05-24T15:18:17.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/1","author":{"@type":"Person","name":"simonfanning3565","url":"https://community.spiceworks.com/u/simonfanning3565"}},{"@type":"Answer","text":"

          Your script never defines a value $PrimarySMTP, so Powershell will see it as null. You can do something like this:<\/p>\n

          $userou = \"OU=Ontario,DC=domain,DC=local\"\n$users = Get-ADUser -Filter * -SearchBase $userou -Properties SamAccountName,ProxyAddresses \n\nForeach ($user in $users)\n\n{\n\nIf ($User.ProxyAddresses -match \"*@domain.org\") \n{Set-ADUser -Identity $user.samaccountname -Add @{proxyAddresses=\"smtp:\"+$user.sAMAccountName+\"@domain.net\"}}\n\n}\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-05-24T16:27:52.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/2","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"
          \n
          \n
          <\/div>\n\"\" simon fanning:<\/div>\n
          \n
            \n
          • I’m sure \" If ($PrimarySMTP -like “*@domain.org<\/span>”)\" should have the notation $True in there somewhere, but I’m not sure how to apply it.<\/li>\n<\/ul>\n<\/blockquote>\n<\/aside>\n

            Not quite. The conditions in If statements evaluate to $True or $False. If the condition equals $True, then the command block within the If statement will run. Your condition does not need to contain $True. You need it to be a true statement.<\/p>\n

            For example, you can check if a variable exists (has non-null value):<\/p>\n

            PS > $test = \"test\"\n\nPS > If ($test) {\"True\"} else {\"False\"}\nTrue\n\nPS > $test = $null\n\nPS > If ($test) {\"True\"} else {\"False\"}\nFalse\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2019-05-24T16:33:03.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/3","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":"

            proxyaddresses will return an array of addresses. You’ll have to do something like if ($user.proxyaddresses -contains “SMTP:”){add the new smtp value}<\/p>","upvoteCount":1,"datePublished":"2019-05-24T16:43:15.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/4","author":{"@type":"Person","name":"matthart5","url":"https://community.spiceworks.com/u/matthart5"}},{"@type":"Answer","text":"

            Here's some code that I wrote that checks to see if a user has the correct SMTP address\n<\/code><\/pre>\n
            $details = get-aduser $domainuser -properties proxyaddresses\n### Initialize a blank array\n$array = @()\n### Step through the returned proxyaddresses and put them in a new array that we can mess with\nforeach ($proxy in $details.proxyaddresses) {\n    $array += $proxy\n}\n### Step through our new array and check if the user already has a primary SMTP (designated by being all CAPS)\nforeach ($proxy in $array) {\n    $first, $last = $proxy.split(\"@\")\n    $first, $second = $first.split(\":\")\n    if ($first -ceq \"SMTP:\" -And $last -eq \"mydomain.org\") {\n        write-host $domainuser \"already has the correct primary proxy address AD attribute. No changes necessary\"\n        $emailbody += \"<br>\" + $username + \" already has the correct SMTP address\"\n    }\n    if ($first -ceq \"SMTP\") {\n        write-host \"Found invalid primary proxyaddress. Converting the wrong proxyaddress and adding the correct one\"\n        $emailbody += \"<br>Found invalid primary proxyaddress \" + $first +  \"Converting the wrong proxyaddress and adding the correct one\"\n        Set-Aduser -identity $domainuser -Remove @{ProxyAddresses = $proxy.ToString()}\n        $correctaddress = \"SMTP:\" + $domainuser + \"@mydomain.org\"\n        $replaceaddress = \"smtp:\" + $proxy.split(\":\")[1]\n        Set-Aduser -identity $domainuser -Add @{ProxyAddresses = $correctaddress}\n        Set-Aduser -identity $domainuser -Add @{ProxyAddresses = $replaceaddress}\n    }\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2019-05-24T16:46:33.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/5","author":{"@type":"Person","name":"matthart5","url":"https://community.spiceworks.com/u/matthart5"}},{"@type":"Answer","text":"

            Welcome OP,<\/p>\n

            If you post code, please use the ‘Insert Code’ button. Please and thank you!
            \nPLEASE READ BEFORE POSTING! Read if you're new to the PowerShell forum!<\/a> .<\/p>\n

            \"codebutton_small.png\"<\/p>\n

            @simonfanning3565<\/a> @stevenpeterson1053<\/a><\/p>","upvoteCount":1,"datePublished":"2019-05-24T16:54:38.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"

            Not only short and sweet, but the perfect solution to my issue!!<\/p>\n

            Thank you so very much for your help!<\/p>\n

            @jitensh<\/a><\/p>","upvoteCount":0,"datePublished":"2019-05-27T11:55:23.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/8","author":{"@type":"Person","name":"simonfanning3565","url":"https://community.spiceworks.com/u/simonfanning3565"}},{"@type":"Answer","text":"

            Please mark the answer as best answer and close the topic<\/p>","upvoteCount":0,"datePublished":"2019-05-27T12:34:56.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/9","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"

            #close<\/span><\/p>","upvoteCount":0,"datePublished":"2019-05-27T14:49:45.000Z","url":"https://community.spiceworks.com/t/need-to-add-alias-proxyaddress-where-smtp-is-true/713579/10","author":{"@type":"Person","name":"simonfanning3565","url":"https://community.spiceworks.com/u/simonfanning3565"}}]}}