我正在尝试制作一个脚本来放置一堆信息Active Directory。它从csv文件导入信息。脚本在城市,州,传真,移动设备上出错。该剧本非常深入。我使用了几组不同的代码来实现这一点。它很长但不确定为什么我会收到此错误。
守则:
############################Function for Ad attribute overwrite#########################################
Function Adattribute ($userid,$attribute,$adatt)
{
$userobject = get-aduser -identity $userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,City,State,postalcode,Homephone,fax,title,department,Company,Manager
$givenName = $userobject.givenName
$sn = $userobject.sn
$DisplayName = $userobject.DisplayName
$Description = $userobject.Description
$physicalDeliveryOfficeName = $userobject.physicalDeliveryOfficeName
$telephoneNumber = $userobject.telephoneNumber
$Emailaddress = $userobject.Emailaddress
$homepage = $userobject.HomePage
$StreetAddress = $userobject.StreetAddress
$City = $userobject.City
$state = $userobject.State
$PostalCode = $userobject.PostalCode
$HomePhone = $userobject.HomePhone
$Pager = $userobject.Pager
$mobile = $userobject.mobile
$fax = $userobject.fax
$Title = $userobject.Title
$Department = $userobject.Department
$Company = $userobject.Company
$Manager = $userobject.Manager
# adding log to if attribute is blank
$attrib = "$" + $attribute
if ($attrib -like $null)
{
Write-host $userobject has blank $attribute
$Log1 = ".\logs\" + "Blank" + $attribute + $now + “.log”
Add-content $Log1 “$userobject has blank $attribute”
# If address is Blank than populate the $attribute from the csv file
get-aduser -identity $userid | Set-ADUser @{$attribute = $adatt.Trim()}
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject has $attribute as $attrib
$Log3 = ".\logs\" + "Set" + $attribute + $now + “.log”
Add-content $Log3 “For $userobject $attribute as $attrib has been set”
}
else
{
# adding log to check current $attribute is not blank , than $attribute will be overwritten.
$Log2 = ".\logs\" + "Current" + $attribute + $now + “.log”
get-aduser -identity $i.userid | Set-ADUser -replace @{$attribute = $adatt.Trim()}
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject has been overwritten with $attribute as Current $attrib
Add-content $Log2 “$userobject has been overwritten with $attribute as $attrib”
}
}
###############################################Function Completed####################################
############################Function for Ad attribute No overwrite###################################
Function AdattributeNoOV ($userid,$attribute,$adatt)
{
$userobject = get-aduser -identity $userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$givenName = $userobject.givenName
$sn = $userobject.sn
$DisplayName = $userobject.DisplayName
$Description = $userobject.Description
$physicalDeliveryOfficeName = $userobject.physicalDeliveryOfficeName
$telephoneNumber = $userobject.telephoneNumber
$Emailaddress = $userobject.Emailaddress
$homepage = $userobject.HomePage
$StreetAddress = $userobject.StreetAddress
$City = $userobject.city
$state = $userobject.state
$PostalCode = $userobject.PostalCode
$HomePhone = $userobject.HomePhone
$Pager = $userobject.Pager
$mobile = $userobject.mobile
$fax = $userobject.fax
$Title = $userobject.Title
$Department = $userobject.Department
$Company = $userobject.Company
$Manager = $userobject.Manager
# adding log to if attribute is blank
$attrib = "$" + $attribute
if ($attrib -like $null)
{
Write-host $userobject has blank $attribute
$Log1 = ".\logs\" + "Blank" + $attribute + $now + “.log”
Add-content $Log1 “$userobject has blank $attribute”
# If address is Blank than populate the $attribute from the csv file
get-aduser -identity $userid | Set-ADUser -replace @{$attribute = $adatt.Trim()}
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject has $attribute as $attrib
$Log3 = ".\logs\" + "Set" + $attribute + $now + “.log”
Add-content $Log3 “For $userobject $attribute as $attrib has been set”
}
else
{
# adding log to check current $attribute is not blank , than $attribute will be not be overwritten.
$Log2 = ".\logs\" + "Current" + $attribute + $now + “.log”
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject already has $attribute as $attrib
Add-content $Log2 “$userobject already has $attribute as $attrib”
}
}
###############################################Function Completed####################################
############################Function for Ad attribute Manager overwrite#########################################
Function AdattributeMN ($userid,$attribute,$adatt)
{
$userobject = get-aduser -identity $userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$Manager = $userobject.Manager
# adding log to if attribute is blank
$attrib = "$" + $attribute
if ($attrib -like $null)
{
Write-host $userobject has blank $attribute
$Log1 = ".\logs\" + "Blank" + $attribute + $now + “.log”
Add-content $Log1 “$userobject has blank $attribute”
# If address is Blank than populate the $attribute from the csv file
get-aduser -identity $userid | Set-ADUser -replace @{$attribute = (get-aduser -identity $adatt.Trim()).DN}
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject has $attribute as $attrib
$Log3 = ".\logs\" + "Set" + $attribute + $now + “.log”
Add-content $Log3 “For $userobject $attribute as $attrib has been set”
}
else
{
# adding log to check current $attribute is not blank , than $attribute will be overwritten.
$Log2 = ".\logs\" + "Current" + $attribute + $now + “.log”
get-aduser -identity $userid | Set-ADUser -replace @{$attribute = (get-aduser -identity $adatt.Trim()).DN}
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject has been overwritten with $attribute as Current $attrib
Add-content $Log2 “$userobject has been overwritten with $attribute as $attrib”
}
}
###############################################Function Completed####################################
############################Function for Ad attribute No overwrite Manager###################################
Function AdattributeNoOVMN ($userid,$attribute,$adatt)
{
$userobject = get-aduser -identity $userid --Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$Manager = $userobject.Manager
# adding log to if attribute is blank
$attrib = "$" + $attribute
if ($attrib -like $null)
{
Write-host $userobject has blank $attribute
$Log1 = ".\logs\" + "Blank" + $attribute + $now + “.log”
Add-content $Log1 “$userobject has blank $attribute”
# If address is Blank than populate the $attribute from the csv file
get-aduser -identity $userid | Set-ADUser -replace @{$attribute = (get-aduser -identity $adatt.Trim()).DN}
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject has $attribute as $attrib
$Log3 = ".\logs\" + "Set" + $attribute + $now + “.log”
Add-content $Log3 “For $userobject $attribute as $attrib has been set”
}
else
{
# adding log to check current $attribute is not blank , than $attribute will be not be overwritten.
$Log2 = ".\logs\" + "Current" + $attribute + $now + “.log”
$userobject = get-aduser -identity $i.userid -Properties givenName,sn,displayname,Description,physicalDeliveryOfficeName,Telephonenumber,Emailaddress,HomePage,StreetAddress,city,state,postalcode,Homephone,fax,title,department,Company,Manager
$attrib = $userobject.$attribute
Write-host $userobject already has $attribute as $attrib
Add-content $Log2 “$userobject already has $attribute as $attrib”
}
}
###############################################Function Completed####################################
# Import CSV file that is populated with checked input Attributes
$now=Get-Date -format “dd-MMM-yyyy HH:mm”
# replace : by -
$now = $now.ToString().Replace(“:”, “-”)
$data = import-csv .\Users.csv
$overwrite = new-object -comobject wscript.shell
$Answer = $overwrite.popup("Do you want to Overwrite AD Attributes?",0,"Overwrite Attributes",4)
If ($Answer -eq 6) {
# Loop thru the data from CSV
foreach ($i in $data)
{
$userid = $i.userid
$givenName = $i.givenName
$sn = $i.sn
$DisplayName = $i.DisplayName
$Description = $i.Description
$physicalDeliveryOfficeName = $i.physicalDeliveryOfficeName
$telephoneNumber = $i.telephoneNumber
$Emailaddress = $i.EmailAddress
$homepage = $i.HomePage
$StreetAddress = $i.StreetAddress
$city = $i.city
$state = $i.state
$PostalCode = $i.PostalCode
$HomePhone = $i.HomePhone
$Pager = $i.Pager
$mobile = $i.mobile
$fax = $i.fax
$Title = $i.Title
$Department = $i.Department
$Company = $i.Company
$Manager = $i.Manager
if($result -contains "FirstName"){ Adattribute $userid givenName $givenName }
if($result -contains "LastName"){ Adattribute $userid sn $sn }
if($result -contains "DisplayName"){ Adattribute $userid DisplayName $DisplayName }
if($result -contains "Description"){ Adattribute $userid Description $Description }
if($result -contains "PhysicalDeliveryOfficeName"){ Adattribute $userid physicalDeliveryOfficeName $physicalDeliveryOfficeName }
if($result -contains "TelephoneNumber"){ Adattribute $userid telephoneNumber $telephoneNumber }
if($result -contains "Email"){ Adattribute $userid mail $Emailaddress }
if($result -contains "HomePage"){ Adattribute $userid HomePage $homepage }
if($result -contains "StreetAddress"){ Adattribute $userid StreetAddress $StreetAddress }
if($result -contains "City"){ Adattribute $userid City $City }
if($result -contains "State"){ Adattribute $userid state $state }
if($result -contains "PostalCode"){ Adattribute $userid PostalCode $PostalCode }
if($result -contains "HomePhone"){ Adattribute $userid HomePhone $HomePhone }
if($result -contains "Pager"){ Adattribute $userid Pager $Pager }
if($result -contains "Mobile"){ Adattribute $userid mobile $mobile }
if($result -contains "Fax"){ Adattribute $userid Fax $fax }
if($result -contains "Title"){ Adattribute $userid Title $Title }
if($result -contains "Department"){ Adattribute $userid Department $Department }
if($result -contains "Company"){ Adattribute $userid Company $Company }
if($result -contains "Manager"){ AdattributeMN $userid Manager $Manager }
}
}
else
{
# Loop thru the data from CSV
foreach ($i in $data)
{
$userid = $i.userid
$givenName = $i.givenName
$sn = $i.sn
$DisplayName = $i.DisplayName
$Description = $i.Description
$physicalDeliveryOfficeName = $i.physicalDeliveryOfficeName
$telephoneNumber = $i.telephoneNumber
$Emailaddress = $i.Emailaddress
$homepage = $i.HomePage
$StreetAddress = $i.StreetAddress
$PostOfficeBox = $i.PostOfficeBox
$city = $i.city
$state = $i.state
$PostalCode = $i.PostalCode
$HomePhone = $i.HomePhone
$Pager = $i.Pager
$mobile = $i.mobile
$fax = $i.fax
$Title = $i.Title
$Department = $i.Department
$Company = $i.Company
$Manager = $i.Manager
if($result -contains "FirstName"){ AdattributeNoOV $userid givenName $givenName }
if($result -contains "LastName"){ AdattributeNoOV $userid sn $sn }
if($result -contains "DisplayName"){ AdattributeNoOV $userid DisplayName $DisplayName }
if($result -contains "Description"){ AdattributeNoOV $userid Description $Description }
if($result -contains "physicalDeliveryOfficeName"){ AdattributeNoOV $userid physicalDeliveryOfficeName $physicalDeliveryOfficeName }
if($result -contains "telephoneNumber"){ AdattributeNoOV $userid telephoneNumber $telephoneNumber }
if($result -contains "Email"){ AdattributeNoOV $userid mail $Emailaddress }
if($result -contains "HomePage"){ AdattributeNoOV $userid HomePage $homepage }
if($result -contains "StreetAddress"){ AdattributeNoOV $userid StreetAddress $StreetAddress }
if($result -contains "PostOfficeBox"){ AdattributeNoOV $userid PostOfficeBox $PostOfficeBox }
if($result -contains "City"){ AdattributeNoOV $userid City $City }
if($result -contains "State"){ AdattributeNoOV $userid state $state }
if($result -contains "PostalCode"){ AdattributeNoOV $userid PostalCode $PostalCode }
if($result -contains "HomePhone"){ AdattributeNoOV $userid HomePhone $HomePhone }
if($result -contains "Pager"){ AdattributeNoOV $userid Pager $Pager }
if($result -contains "mobile"){ AdattributeNoOV $userid mobile $mobile }
if($result -contains "fax"){ AdattributeNoOV $userid fax $fax }
if($result -contains "Title"){ AdattributeNoOV $userid Title $Title }
if($result -contains "Department"){ AdattributeNoOV $userid Department $Department }
if($result -contains "Company"){ AdattributeNoOV $userid Company $Company }
if($result -contains "Manager"){ AdattributeNoOVMN $userid Manager $Manager }
}
}
##########################################################################################################
我接受了两个人说的话并重新评估了我的剧本,这就是我提出来的:
$data = import-csv .\Users.csv
foreach ($i in $data)
{
$userid = $i.userid
$givenName = $i.givenName
$sn = $i.sn
$DisplayName = $i.DisplayName
$Description = $i.Description
$Office= $i.Office
$OfficePhone = $i.Officephone
$Emailaddress = $i.EmailAddress
$homepage = $i.HomePage
$StreetAddress = $i.StreetAddress
$city = $i.city
$state = $i.state
$PostalCode = $i.PostalCode
$HomePhone = $i.HomePhone
$mobilephone = $i.mobilephone
$fax = $i.fax
$Title = $i.Title
$StreetAddress = $i.StreetAddress
$Company = $i.Company
$Manager = $i.Manager
$oldfile =(".\logs\" + $userId + "_old.csv")
$NewFile =(".\logs\" + $userId + "_new.txt")
get-aduser $userid -Properties Givenname,Sn,Displayname,Description,Office,OfficePhone,EmailAddress,HomePage,StreetAddress,City,State,PostalCode,HomePhone,Mobilephone,Fax,Title,Department,Company,Manager > $OldFile
Write-host ___________________________________________________________________________________
Write-host
get-aduser $userid -Properties Givenname,Sn,Displayname,Description,Office,OfficePhone,EmailAddress,HomePage,StreetAddress,City,State,PostalCode,HomePhone,Mobilephone,Fax,Title,Department,Company,Manager
Write-host
WriTe-host ____________________________________________________________________________________
######## GivenName No Overwrite ########
$GivenNameTest = get-aduser $userid -Properties GivenName | Select-Object -ExpandProperty GivenName
if ( [string]::IsNullOrWhiteSpace($GivenNametest))
{ set-aduser $userid -GivenName $GivenName}
Else
{ Write-host GivenName has $GivenNameTest alread "in" this field. Field was not update "for" $userId}
######## sn No Overwrite ########
$snTest = get-aduser $userid -Properties sn | Select-Object -ExpandProperty sn
if ( [string]::IsNullOrWhiteSpace($Sntest))
{ set-aduser $userid -sn $sn}
Else
{ Write-host sn has $Sntest alread "in" this field. Field was not update "for" $userId}
######## DisplayName No Overwrite ########
$DisplayNameTest = get-aduser $userid -Properties DisplayName | Select-Object -ExpandProperty DisplayName
if ( [string]::IsNullOrWhiteSpace($DisplayNameTest))
{ set-aduser $userid -DisplayName $DisplayName}
Else
{ Write-host DisplayName has $DisplayNameTest alread "in" this field. Field was not update "for" $userId}
######## Description No Overwrite ########
$DescriptionTest = get-aduser $userid -Properties Description | Select-Object -ExpandProperty Description
if ( [string]::IsNullOrWhiteSpace($Descriptiontest))
{ set-aduser $userid -Description $Description
Write-host The field of Description has been updated to $Description "for" $UserId}
Else
{ Write-host Description has $DescriptionTest alread "in" this field. Field was not update "for" $userId}
######## Office No Overwrite ########
$OfficeTest = get-aduser $userid -Properties Office | Select-Object -ExpandProperty Office
if ( [string]::IsNullOrWhiteSpace($Officetest))
{ set-aduser $userid -Office $Office
Write-host The field of Office has been updated to $Office "for" $UserId}
Else
{ Write-host Office has $Officetest alread "in" this field. Field was not update "for" $userId}
######## OfficePhone No Overwrite ########
$OfficePhoneTest = get-aduser $userid -Properties OfficePhone | Select-Object -ExpandProperty OfficePhone
if ( [string]::IsNullOrWhiteSpace($OfficePhonetest))
{ set-aduser $userid -OfficePhone $OfficePhone
Write-host The field of Office Phone has been updated to $Officephone "for" $UserId}
Else
{ Write-host OfficePhone has $OfficePhoneTest alread "in" this field. Field was not update "for" $userId
}
######## EmailAddress No Overwrite ########
$EmailAddressTest = get-aduser $userid -Properties EmailAddress | Select-Object -ExpandProperty email
if ( [string]::IsNullOrWhiteSpace($EmailAddresstest))
{ set-aduser $userid -EmailAddress $EmailAddress}
Else
{ Write-host EmailAddress has $EmailAddressTest alread "in" this field. Field was not update "for" $userId}
######## Homepage No Overwrite ########
$HomepageTest = get-aduser $userid -Properties Homepage | Select-Object -ExpandProperty Homepage
if ( [string]::IsNullOrWhiteSpace($HomePagetest))
{ set-aduser $userid -Homepage $Homepage
Write-host The field of HomePage has been updated to $HomePage "for" $UserId}
Else
{ Write-host Homepage has $HomePagetest alread "in" this field. Field was not update "for" $userId}
######## StreetAddress No Overwrite ########
$StreetAddressTest = get-aduser $userid -Properties StreetAddress | Select-Object -ExpandProperty StreetAddress
if ( [string]::IsNullOrWhiteSpace($StreetAddressTest))
{ set-aduser $userid -StreetAddress $StreetAddress
Write-host The field of Street Address has been updated to $StreetAddress "for" $UserId}
Else
{ Write-host StreetAddress has $StreetAddressTest alread "in" this field. Field was not update "for" $userId}
######## City No Overwrite ########
$CityTest = get-aduser ahawkins -Properties City | Select-Object -ExpandProperty city
if ( [string]::IsNullOrWhiteSpace($citytest))
{ set-aduser $userid -City $City
Write-host The field of City has been updated to $City "for" $UserId}
Else
{ Write-host City has $CityTest alread "in" this field. Field was not update "for" $userId}
######## State No Overwrite ########
$StateTest = get-aduser $userid -Properties State | Select-Object -ExpandProperty state
if ( [string]::IsNullOrWhiteSpace($statetest))
{ set-aduser $userid -State $State
Write-host The field of State has been updated to $State "for" $UserId}
Else
{ Write-host State has $StateTest alread "in" this field. Field was not update "for" $userId}
######## PostalCode No Overwrite ########
$PostalCodeTest = get-aduser $userid -Properties PostalCode | Select-Object -ExpandProperty PostalCode
if ( [string]::IsNullOrWhiteSpace($PostalCodetest))
{ set-aduser $userid -PostalCode $PostalCode
Write-host The field of Postal Code has been updated to $PostalCode "for" $UserId}
Else
{ Write-host PostalCode has $PostalCodeTest alread "in" this field. Field was not update "for" $userId}
######## homephone No Overwrite ########
$homephoneTest = get-aduser $userid -Properties homephone | Select-Object -ExpandProperty homephone
if ( [string]::IsNullOrWhiteSpace($homephoneTest))
{ set-aduser $userid -homephone $homephone
Write-host The field of Home Phone has been updated to $Homephone "for" $UserId}
Else
{ Write-host homephone has $homephoneTest alread "in" this field. Field was not update "for" $userId}
######## MobilePhone No Overwrite ########
$MobilePhoneTest = get-aduser $userid -Properties MobilePhone | Select-Object -ExpandProperty MobilePhone
if ( [string]::IsNullOrWhiteSpace($MobilePhoneTest))
{ set-aduser $userid -MobilePhone $MobilePhone
Write-host The field of Mobile Phone has been updated to $Mobile "for" $UserId}
Else
{ Write-host MobilePhone has $MobilePhoneTest alread "in" this field. Field was not update "for" $userId}
######## fax No Overwrite ########
$faxTest = get-aduser $userid -Properties fax | Select-Object -ExpandProperty fax
if ( [string]::IsNullOrWhiteSpace($faxtest))
{ set-aduser $userid -fax $fax
Write-host The field of Fax Number has been updated to $fax "for" $UserId}
Else
{ Write-host fax has $faxTest alread "in" this field. Field was not update "for" $userId}
######## Title No Overwrite ########
$TitleTest = get-aduser $userid -Properties Title | Select-Object -ExpandProperty Title
if ( [string]::IsNullOrWhiteSpace($Titletest))
{ set-aduser $userid -Title $Title
Write-host The field of Title has been updated to $Title "for" $UserId}
Else
{ Write-host Title has $TitleTest alread "in" this field. Field was not update "for" $userId}
####### Company NO Overwrite #########
$CompanyTest = get-aduser $userid -Properties Company | Select-Object -ExpandProperty Company
if ( [string]::IsNullOrWhiteSpace($Companytest))
{ set-aduser $userid -Company $Company
Write-host The field of Company has been updated to $Company "for" $UserId}
Else
{ Write-host Company has $CompanyTest alread "in" this field. Field was not update "for" $userId}
######### Manager No Overwrite ########
$Managertest = get-aduser $userid -Properties Manager | Select-Object -ExpandProperty Manager
if ( [string]::IsNullOrWhiteSpace($Mangertest))
{ set-aduser $userid -Manager $Manager
Write-host The field of Manager has been updated to $Manager "for" $UserId}
Else
{ Write-host Manager has $Managertest alread "in" this field. Field was not update "for" $userId}
###### Writing Log of New Info ########
get-aduser $userid -Properties Givenname,Sn,Displayname,Description,Office,OfficePhone,EmailAddress,HomePage,StreetAddress,City,State,PostalCode,HomePhone,Mobilephone,Fax,Title,Department,Company,Manager > $NewFile
Write-host ____________________________________________________________________________________
WriTe-host ____________________________________________________________________________________
Write-host ####################################################################################
get-aduser $userid -Properties Givenname,Sn,Displayname,Description,Office,OfficePhone,EmailAddress,HomePage,StreetAddress,City,State,PostalCode,HomePhone,Mobilephone,Fax,Title,Department,Company,Manager
Write-host ####################################################################################
WriTe-host ____________________________________________________________________________________
WriTe-host ____________________________________________________________________________________
}
答案 0 :(得分:0)
您提出了两个不同的问题。首先是很可能是一个错字,第二个我将不得不假设不再存在,因为你当前的代码表明它不会发生。我想指出错误中的行号与您发布的代码不匹配(是的,即使是第一篇文章)。您应该指出错误发生在代码中的哪个位置。
You cannot call a method on a null-valued expression.
这很可能是由于复制和粘贴错误造成的。正确的调试应该会解决这个问题。错误引用此部分代码:get-aduser -identity $i.userid
。该代码在您的函数中显示几个次,但是在您的main for循环中的索引对象中$i
。它不存在于任何函数的范围内。我怀疑你是在复制和粘贴代码来创建这个错误。在函数中使用适当的变量将解决这个问题。
get-aduser : Cannot find an object with identity: 'state'
对于“州”,“城市”等,您还有几种错误。我查看了在该上下文中调用Get-Aduser
的代码,但没有看到任何问题。因此,我的结论是,你得到的错误与你的代码不符(回到早先的一点,我无法得到与错误匹配的行。)。对于上面的错误,它说找不到对象“State”意味着某种方式$userid
成为字符串“State”,根据你的函数声明是第二个参数。这很可能是另一个错字
<强>结论强>
使用get-aduser -identity $i.userid
修复行以使用正确的变量,并考虑保存脚本并打开新的PowerShell会话进行测试。然后看看您的代码会发生什么。
欢迎使用SO
很高兴看到你参加了帮助之旅。希望您学到一些东西并欢迎来到StackOverflow社区!答案 1 :(得分:0)
您指的是不存在的LDAP属性,而Set-ADUser
正在传递LDAP服务器抱怨它。您正在传递Set-ADUser
LDAP命令,因此您需要在LDAP属性中与LDAP服务器通信,而不是Set-ADUser
允许您使用cmdlet参数的友好名称。这意味着您必须使用LDAP显示名称。
您没有收到Get-ADUser
的错误,因为我记得Get-ADUser
并不关心您是否致电$User = Get-ADUser ... -Properties franksAndBeans
。它只会将$User.franksAndBeans
设置为null,因为LDAP读取查询不会验证您要求的属性对于要返回的对象是否有效。但是,Set-ADUser
确实这样做,因为它必须这样做。
您可以通过运行以下命令获取AD环境支持的实际属性列表:
$objuserclass = [adsi]"LDAP://schema/user";
$objuserclass.mandatoryproperties;
$objuserclass.optionalproperties;
(Source)
或者您可以查看Active Directory的online documentation以获取可能的属性列表。
任何强制性都保证存在并且始终具有非空值,但是强制属性非常少。任何可选的东西都可能很好地返回一个空值,因为如果它没有值,该属性甚至不存在于该对象上。
你也可以运行Get-ADUser ... -Properties *
,但是,这并不全面,因为并非所有用户都拥有所有可能的属性。
没有“城市”属性。有一个属性Locality-Name
,其LDAP显示名称为l
(小写L)。
没有“州”属性。有一个属性State-Or-Province-Name
,其LDAP显示名称为st
。
没有“传真”属性。有一个属性Phone-Fax-Other
,其LDAP显示名称为otherFacsimileTelephoneNumber
。
在我看来,“部门”失败了,而不是“移动”。无论如何,这些都是有效的LDAP属性。列出的错误是“InvalidOperation”。我猜测的可能性是:
总的来说,我会说你的代码方法太复杂了。你有四个表面上做同样事情的函数,但你必须在不同情况下调用它们,并且通过查看代码并且这些函数的命名并不真正有助于记录它们的作用,这些情况并不明显。我宁愿只传递一个哈希表,其中包含用户的所有属性,并让函数在有完整的数据集时理清该做什么。函数本身应该能够从传入的哈希表中确定用户是否是管理员,如果我有时需要覆盖,我只需要为该函数创建一个-Force
参数。老实说,看起来你正试图在PowerShell中编写VBScript。