我正在尝试更新我们的活动目录中的manager字段并拥有一个脚本 输入文件看起来像这样
Firstname.LastnameEmployee;firstname.lastnameManager
这是我正在使用的脚本...... 这是我得到的错误
您无法在空值表达式上调用方法。 在线:26 char:5 + $ ObjSearchemployee.Filter =“(&(objectCategory = person)(objectClass = user)(sAM ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull
您无法在空值表达式上调用方法。 在行: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 + ""
}
}
答案 0 :(得分:0)
$.Employee.trim()
是问题所在,因为您没有"员工"的列标题。根据你的源代码示例。见You cannot call a method on a null-valued expression。
假设您的第一列是针对samaccountname的,那么您应该将其更改为$_.Firstname.LastnameEmployee.trim()
,否则您需要重新定义搜索的完成方式,或者更新源文件以包含相关信息并使您的代码匹配。
由于错误表明您在使用$_.Manager.trim()