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 + ""
}
}