none
Distribution Group Script help RRS feed

  • Question

  • Hi guys,

    I'm currently in the process of writing a PowerShell script for my workplace with the intention of clearing all of our Distribution groups, then it will auto add our AD users depending on the state, city or custom attributes, however everytime we run just one part of the script - we get the following error:

    You cannot call a method on a null-valued expression.
    At line:19 char:9
    +         $UserCity = $user.City.ToString()
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    However, the funny thing is it is still adding the user to the correct AD Distribution group...

    This is the script part that it returns the error:

    #Variables
    $users = Get-ADUser -Filter * -Properties * | Where-Object {$_.DistinguishedName -like "*OU=Users,OU=Australia,OU=XXX,DC=lsc,DC=local"}

    foreach($user in $users)
        {
        #Variable
        $UserName = $user.Name.toString()

        #XXX (All Staff)
        #-----------------------------------------------------------------------------
        if (-NOT $user.Company)
        {
           #Do nothing
        }
        Else
        {
            $UserName = $user.name.tostring()
            $UserCompany = $user.Company.ToString()
            $UserCity = $user.City.ToString()

            if($UserCompany -eq "XXX")
            {
                if($UserCity -eq "Belmont")
                {
                    Write-Host "Adding user: $UserName to `"LSC Belmont (WA)`" distribution list"
                    Add-ADGroupMember -Identity "XXX Belmont (WA)" -Members $user -Confirm:$false
                }    
                
            }
         }
         }

    (Please note: I have XXX out the company name for privacy.)

    If I could have someone's help - that would be greatly appreciated. We have checked and every single user has their respective state & city etc. 

    • Moved by Bill_Stewart Monday, March 12, 2018 7:19 PM Abandoned
    Wednesday, January 10, 2018 3:53 AM

All replies

  • Hi guys,

    I'm currently in the process of writing a PowerShell script for my workplace with the intention of clearing all of our Distribution groups, then it will auto add our AD users depending on the state, city or custom attributes, however everytime we run just one part of the script - we get the following error:

    You cannot call a method on a null-valued expression.
    At line:19 char:9
    +         $UserCity = $user.City.ToString()
    +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    However, the funny thing is it is still adding the user to the correct AD Distribution group...

    This is the script part that it returns the error:

                

    #Variables
    $users = Get-ADUser -Filter * -Properties * | Where-Object {$_.DistinguishedName -like "*OU=Users,OU=Australia,OU=XXX,DC=lsc,DC=local"}

    foreach($user in $users)
        {
        #Variable
        $UserName = $user.Name.toString()

        #XXX (All Staff)
        #-----------------------------------------------------------------------------
        if (-NOT $user.Company)
        {
           #Do nothing
        }
        Else
        {
            $UserName = $user.name.tostring()
            $UserCompany = $user.Company.ToString()
            $UserCity = $user.City.ToString()

            if($UserCompany -eq "XXX")
            {
                if($UserCity -eq "Belmont")
                {
                    Write-Host "Adding user: $UserName to `"XXX Belmont (WA)`" distribution list"
                    Add-ADGroupMember -Identity "XXX Belmont (WA)" -Members $user -Confirm:$false
                }    

            }
         }
         }

    (Please note: I have XXX out the company name for privacy.)

    If I could have someone's help - that would be greatly appreciated. We have checked and every single user has their respective state & city etc. 

    Thanks - Jon

    • Merged by Bill_Stewart Wednesday, January 10, 2018 4:34 PM Duplicate
    Wednesday, January 10, 2018 3:56 AM
  • First, in PowerShell we do not need ToString().

    \_(ツ)_/

    Wednesday, January 10, 2018 4:00 AM
  • Start with:

    help Get-AdUser -online

    THIs is hwo to filter users:

    $users = Get-ADUser -Filter * -Properties * -SearchBase 'OU=Users,OU=Australia,OU=XXX,DC=lsc,DC=local'

    If City is null using "ToString" will cause that error.


    \_(ツ)_/


    • Edited by jrv Wednesday, January 10, 2018 4:02 AM
    Wednesday, January 10, 2018 4:02 AM
  • Thanks for you reply - funny thing is that "City" for all of our AD users isnt blank - hence why its causing confusion around why its erroring on that particular line :-/ 
    Thursday, January 11, 2018 2:08 AM
  • Do NOT use ToString() in PowerShell.  It will almost always lead to bad outcomes.


    \_(ツ)_/

    Thursday, January 11, 2018 2:33 AM
  • This is how we would code this:

    $users = Get-ADUser -Filter * -Properties * -SearchBase 'OU=Users,OU=Australia,OU=XXX,DC=lsc,DC=local'
    foreach ($user in $users) {
        if($user.Company -eq 'XXX' -and $User.City -eq 'Belmont') {
            Write-Host "Adding user: $($user.Name) to 'XXX Belmont (WA)' distribution list"
            Add-ADGroupMember -Identity 'XXX Belmont (WA)' -Members $user -Confirm:$false
        }
    }
    As you can see it is much simpler and much less prone to coding errors.


    \_(ツ)_/




    • Edited by jrv Thursday, January 11, 2018 2:39 AM
    Thursday, January 11, 2018 2:38 AM
  • This has actually made things heap easier! Thank you - im still fairly new to powershell scripting - so i have only had work collegue to try and teach me! 

    Now i just need to get it to work with mulitple of this code in the one script and ill be sweet!

    Thanks guys so much!!!! 

    Thursday, January 11, 2018 4:34 AM
  • Great.  Before you work with your colleague do this video tutorial.  It will help you time with your colleague so that his/her time will be most valuable.

    Learn PowerShell  

    I also recommend getting any one f the many excellent books on PowerShell as an ongoing study.


    \_(ツ)_/

    Thursday, January 11, 2018 4:40 AM