0

I'm trying to update our managers field in our active directory and have a script the input file looks like this

Firstname.LastnameEmployee;firstname.lastnameManager

Here is the script I'm using... This is the error I'm getting

You cannot call a method on a null-valued expression. At line:26 char:5 + $ObjSearchemployee.Filter = "(&(objectCategory=person)(objectClass=user)(sAM ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression. At line:31 char:9 + $ObjSearchmanager.Filter = "(&(objectCategory=person)(objectClass=user)( ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

############################################################### 
# Update_Manager_v1.0.ps1 
# input : n/a 
# output : none (logs) 
# Version 1. 
# Changelog : n/a 
# MALEK Ahmed - 02 / 06 / 2013 
################### 

################## 
#--------Config 
################## 
$adPath="LDAP://DC=local,DC=com" 


################## 
#--------Main   
################## 
#LDAP connection 
$objDomain=New-Object System.DirectoryServices.DirectoryEntry($adPath) 
#Doing an LDAP search 
$ObjSearchemployee=New-Object System.DirectoryServices.DirectorySearcher($ObjDomain) 
$ObjSearchmanager=New-Object System.DirectoryServices.DirectorySearcher($ObjDomain) 
#Operations on user accounts 
Import-Csv .\input.csv -Delimiter ';' | Foreach-Object { 
    $ObjSearchemployee.Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName="+ $_.employee.trim() +"))" 
    $allSearchResultemployee = $ObjSearchemployee.FindAll() 
    foreach ($objSearchResultemployee in $allSearchResultemployee) 
    { 
        $objUseremployee=New-Object System.DirectoryServices.DirectoryEntry($objSearchResultemployee.Path)

        $ObjSearchmanager.Filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName="+ $_.manager.trim() +"))" 
        $allSearchResultmanager = $ObjSearchmanager.FindAll() 
        foreach ($objSearchResultmanager in $allSearchResultmanager) 
        { 
            $objUsermanager=New-Object System.DirectoryServices.DirectoryEntry($objSearchResultmanager.Path) 
            $objUseremployee.manager = $objUsermanager.distinguishedname 
        } 
        $objUseremployee.CommitChanges() 
        "" + $objUsermanager.displayName + " is now the manager of " + $objUseremployee.displayName + "" 
    } 
}
BenH
  • 9,766
  • 1
  • 22
  • 35
  • You dont have a header called employee in your source file. That is why you are getting the error. – Matt May 04 '17 at 19:34

1 Answers1

0

$.Employee.trim() is the problem since you do not have a column header of "Employee" as per your source example. See You cannot call a method on a null-valued expression.

Assuming your first column is for the samaccountname then you should change that to $_.Firstname.LastnameEmployee.trim() else you need to redefine how your search is being done or update your source file to contain the relevant information and get your code to match.

As the error suggests you will have the same problem with $_.Manager.trim()

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119