Newb question - Spaces in output

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Dan Potter Dan Potter 3 months, 3 weeks ago.

  • Author
    Posts
  • #50610
    Profile photo of Tony DiLeone
    Tony DiLeone
    Participant

    Hello everyone,

    I haven't worked with PS in over a year and even then I wasn't that experienced with it. I'm trying to see a simple script to remove all ProxyAddresses for a deprecated domain from my Active Directory accounts. However, the output is coming back with spaces after SMTP: and after the mailnickname field. Can someone tell me how to get rid of these spaces so the command works? Here is the script:

    $ADobjects = @(Get-ADObject -Filter 'objectClass -eq "User"' -Properties mailNickname, ProxyAddresses) | Where-Object {$_.ProxyAddresses -Match "@domain.xyz"}
    ForEach ($userObject in $ADobjects)
    {
    ForEach ($proxyAddress in $userObject.ProxyAddresses)

    Set-ADUser $userobject.mailnickname -Remove '@{ProxyAddresses="SMTP:'$userObject.mailnickname'@domain.xyz}'

    }

  • #50621
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Why are you performing set foreach proxy address?

    We don't have to specify the .mailnickname property after set-aduser, the cmdlet is capable of binding the object to set-aduser. We wouldn't use this property for identity anyway.

    I suspect you have issues in your qoutes. Singles are literal and I don't see the end double qoute. Use write-host to troubleshoot.

    Get the value of what proxy you are looking for and remove it. No sense building a string of data that is already present.

    Lastly improve your filter. get-aduser -Filter 'anr -like "smtp:*@mydomain.com"'

  • #50623
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Always work with one object first until you know for sure what it's doing.

    
    $users = get-aduser -Filter 'anr -eq "smtp:myemail@somewhere.com"' -properties proxyaddresses
    
    $users | %{
    
    $proxytoremove = $_.proxyaddresses -match "@somedomain.somewhere.com"
    
    Set-ADUser $_ -Remove @{ProxyAddresses=$proxytoremove} -whatif
    
    }
    
    
  • #50653
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    And don't ever, ever, ever use % as an alias for foreach-object in a production script

  • #50657
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Is there a technical reason for that or just opinion?

    This would fit into the one off typed into shell never saved category:D

  • #50663
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    Dan,

    It best practice to use the cmdlet instead of alias. I can use ? in a statement like | ? {$_.something -like something} or I can do the same thing using the cmdlet like so: | where {$_.something -like something}. It's harder to read when using the alias instead of the cmdlet.

    Thanks,
    Freddy

  • #50665
    Profile photo of Dan Potter
    Dan Potter
    Participant

    That's fine but still no documentation that says never ever ever. Just like the write-host opinions.

    I do understand what they are for:D

    • This reply was modified 3 months, 3 weeks ago by Profile photo of Dan Potter Dan Potter.

You must be logged in to reply to this topic.