来自 CSV 的 Powershell 脚本
Powershell Script from CSV
我正在尝试制作一个脚本来将一堆信息放入 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 ____________________________________________________________________________________
}
您提出了 2 个不同的问题。第一个很可能是错字,第二个我将不得不假设不再存在,因为您当前的代码表明它不会发生。我想指出错误中的行号与您 post 编辑的代码不匹配(是的,即使是第一个 post)。您应该指出您的错误发生在代码中的什么位置。
You cannot call a method on a null-valued expression.
这很可能是由于复制和粘贴拼写错误造成的。正确的调试应该会发现这个问题。错误是指这段代码:get-aduser -identity $i.userid
。该代码在您的函数中出现 几次 次,但是在您的主 for 循环中索引对象中出现 $i
次。它不存在于您函数的 any 范围内。我怀疑您是通过复制和粘贴代码来创建此错误的。在您的函数中使用适当的变量将解决此问题。
get-aduser : Cannot find an object with identity: 'state'
对于 "State"、"city" 等,您还有几种错误类型。我查看了在该上下文中调用 Get-Aduser
的代码,没有发现任何问题。因此,我会得出结论,您得到的错误与您的代码不匹配(可以追溯到较早的一点,我无法获得与错误匹配的行。)。对于上面的错误,说找不到对象 "State" 意味着 $userid
以某种方式变成了字符串 "State",根据您的函数声明,它是第二个参数。这很可能是另一个错字
结论
修复带有 get-aduser -identity $i.userid
的行以使用正确的变量并考虑保存脚本并打开一个新的 PowerShell 会话进行测试。然后看看你的代码会发生什么。
欢迎来到 SO
很高兴看到您完成了帮助之旅。希望您学到了一些东西,欢迎来到 Whosebug 社区!
您指的是不存在的 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 读取查询不会验证您请求的属性对于被 returned 的对象是否有效。 Set-ADUser
,但是,确实这样做是因为它必须这样做。
您可以通过 运行 获取您的 AD 环境支持的实际属性的列表:
$objuserclass = [adsi]"LDAP://schema/user";
$objuserclass.mandatoryproperties;
$objuserclass.optionalproperties;
(Source)
或者您可以查看 online documentation Active Directory 以获取可能的属性列表。
任何强制属性都保证存在并且始终具有非空值,但强制属性很少。任何可选的东西都可能 return 一个空值,因为如果 属性 没有值,它甚至不会出现在对象上。
您也可以 运行 Get-ADUser ... -Properties *
,但同样,这并不全面,因为并非所有用户都拥有所有可能的属性。
没有 "City" 属性。有一个属性 Locality-Name
,它具有 LDAP 显示名称 l
(小写 L)。
没有 "State" 属性。有一个属性 State-Or-Province-Name
,它具有 LDAP 显示名称 st
。
没有 "FAX" 属性。有一个属性 Phone-Fax-Other
,其 LDAP 显示名称为 otherFacsimileTelephoneNumber
。
在我看来 "department" 失败了,而不是 "mobile"。无论如何,这些都是有效的 LDAP 属性。列出的错误是 "InvalidOperation." 我的猜测(按可能性排序):
- 您正在尝试 "replace" 一个未设置的参数。替换操作可能要求 属性 首先存在。
- 你的数据有问题。
- 查询失败的其他一些原因。
总的来说,我会说你的代码方法太复杂了。你有四个函数,表面上做同样的事情,但你必须在不同的情况下调用它们,通过查看代码,这些情况实际上并不明显,函数的命名并不能真正帮助记录它们的作用。我宁愿只传递一个哈希表,其中包含用户的所有属性,让函数在拥有完整数据集时整理出要做什么。该函数本身应该能够根据传入的哈希表确定用户是否是经理,如果我有时需要覆盖,我只需为该函数创建一个 -Force
参数。老实说,您似乎正在尝试在 PowerShell 中编写 VBScript。
我正在尝试制作一个脚本来将一堆信息放入 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 ____________________________________________________________________________________
}
您提出了 2 个不同的问题。第一个很可能是错字,第二个我将不得不假设不再存在,因为您当前的代码表明它不会发生。我想指出错误中的行号与您 post 编辑的代码不匹配(是的,即使是第一个 post)。您应该指出您的错误发生在代码中的什么位置。
You cannot call a method on a null-valued expression.
这很可能是由于复制和粘贴拼写错误造成的。正确的调试应该会发现这个问题。错误是指这段代码:get-aduser -identity $i.userid
。该代码在您的函数中出现 几次 次,但是在您的主 for 循环中索引对象中出现 $i
次。它不存在于您函数的 any 范围内。我怀疑您是通过复制和粘贴代码来创建此错误的。在您的函数中使用适当的变量将解决此问题。
get-aduser : Cannot find an object with identity: 'state'
对于 "State"、"city" 等,您还有几种错误类型。我查看了在该上下文中调用 Get-Aduser
的代码,没有发现任何问题。因此,我会得出结论,您得到的错误与您的代码不匹配(可以追溯到较早的一点,我无法获得与错误匹配的行。)。对于上面的错误,说找不到对象 "State" 意味着 $userid
以某种方式变成了字符串 "State",根据您的函数声明,它是第二个参数。这很可能是另一个错字
结论
修复带有 get-aduser -identity $i.userid
的行以使用正确的变量并考虑保存脚本并打开一个新的 PowerShell 会话进行测试。然后看看你的代码会发生什么。
欢迎来到 SO
很高兴看到您完成了帮助之旅。希望您学到了一些东西,欢迎来到 Whosebug 社区!
您指的是不存在的 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 读取查询不会验证您请求的属性对于被 returned 的对象是否有效。 Set-ADUser
,但是,确实这样做是因为它必须这样做。
您可以通过 运行 获取您的 AD 环境支持的实际属性的列表:
$objuserclass = [adsi]"LDAP://schema/user";
$objuserclass.mandatoryproperties;
$objuserclass.optionalproperties;
(Source)
或者您可以查看 online documentation Active Directory 以获取可能的属性列表。
任何强制属性都保证存在并且始终具有非空值,但强制属性很少。任何可选的东西都可能 return 一个空值,因为如果 属性 没有值,它甚至不会出现在对象上。
您也可以 运行 Get-ADUser ... -Properties *
,但同样,这并不全面,因为并非所有用户都拥有所有可能的属性。
没有 "City" 属性。有一个属性 Locality-Name
,它具有 LDAP 显示名称 l
(小写 L)。
没有 "State" 属性。有一个属性 State-Or-Province-Name
,它具有 LDAP 显示名称 st
。
没有 "FAX" 属性。有一个属性 Phone-Fax-Other
,其 LDAP 显示名称为 otherFacsimileTelephoneNumber
。
在我看来 "department" 失败了,而不是 "mobile"。无论如何,这些都是有效的 LDAP 属性。列出的错误是 "InvalidOperation." 我的猜测(按可能性排序):
- 您正在尝试 "replace" 一个未设置的参数。替换操作可能要求 属性 首先存在。
- 你的数据有问题。
- 查询失败的其他一些原因。
总的来说,我会说你的代码方法太复杂了。你有四个函数,表面上做同样的事情,但你必须在不同的情况下调用它们,通过查看代码,这些情况实际上并不明显,函数的命名并不能真正帮助记录它们的作用。我宁愿只传递一个哈希表,其中包含用户的所有属性,让函数在拥有完整数据集时整理出要做什么。该函数本身应该能够根据传入的哈希表确定用户是否是经理,如果我有时需要覆盖,我只需为该函数创建一个 -Force
参数。老实说,您似乎正在尝试在 PowerShell 中编写 VBScript。