来自CSV的Powershell脚本

来自CSV的Powershell脚本,powershell,csv,active-directory,account,ps1,Powershell,Csv,Active Directory,Account,Ps1,我正试图制作一个脚本,将一堆信息放到Active Directory中。它从csv文件导入信息。脚本在城市、州、传真、手机上出错。剧本很有深度。我使用了几组不同的代码来实现这一点。它很长,但不确定我为什么会出现这个错误 守则: ############################Function for Ad attribute overwrite######################################### Function Adattribute (

我正试图制作一个脚本,将一堆信息放到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 ____________________________________________________________________________________



} 

您有两个不同的问题,您正在介绍。第一个很可能是打字错误,第二个我必须假设它不再存在,因为您当前的代码表明它不会发生。我想指出错误中的行号与您发布的代码不匹配(是的,即使是第一篇文章)。您应该指出代码中发生错误的位置

  • 不能对空值表达式调用方法。
  • 这很可能是由于复制和粘贴错误造成的。正确的调试应该会暴露出这个问题。此错误涉及此部分代码:
    get aduser-identity$i.userid
    。该代码在函数中多次出现,但在主for循环的索引对象中,
    $i
    。它不存在于您的任何功能范围内。我怀疑您是在复制和粘贴代码来创建此错误。在函数中使用适当的变量可以解决这个问题

  • get aduser:找不到标识为“state”的对象
  • 对于“州”、“市”等等,你也有几种错误的味道。我查看了在该上下文中调用
    Get Aduser
    的代码,没有发现任何问题。因此,我认为您得到的错误与您的代码不匹配(返回到前面的一个点,在那里我无法获得与错误匹配的行)。对于上面的错误,如果说找不到对象“State”,则意味着
    $userid
    以某种方式变成了字符串“State”,根据您的函数声明,它是第二个参数。这很可能是另一个打字错误

    结论

    将代码与<代码>获取Advisher -标识$IUSERID < /代码>以使用适当的变量,并考虑保存脚本并打开一个新的PuthSek会话进行测试。然后看看你的代码会发生什么

    欢迎来到SO


    很高兴看到你参加了帮助之旅。希望你学到了一些东西,欢迎来到StackOverflow社区

    您指的是不存在的LDAP属性,
    Set ADUser
    传递的信息是LDAP服务器正在对此进行投诉。您正在传递
    Set-ADUser
    LDAP命令,因此需要在LDAP属性中与LDAP服务器对话,而不是
    Set-ADUser
    允许您与cmdlet参数一起使用的友好名称。这意味着您必须使用LDAP显示名称

    您没有从
    Get ADUser
    中得到错误,因为据我记忆所及
    Get ADUser
    不在乎您是否调用
    $User=Get ADUser-属性franksAndBeans
    。它只会将
    $User.franksAndBeans
    设置为null,因为LDAP读取查询不会验证您请求的属性对于返回的对象是否有效<但是,代码>设置ADUser会这样做,因为它必须这样做

    通过运行以下命令,可以获得广告环境支持的实际属性列表:

    $objuserclass = [adsi]"LDAP://schema/user";
    $objuserclass.mandatoryproperties;
    $objuserclass.optionalproperties;
    
    ()

    或者,您可以查看for Active Directory以获取可能属性的列表

    任何强制的内容都保证存在,并且始终具有非null值,但强制属性很少。任何可选内容都可能返回空值,因为如果对象没有值,则该属性甚至不存在于对象上

    您还可以运行
    Get ADUser-属性*
    ,但同样,这并不全面,因为并非所有用户都拥有所有可能的属性

    没有“城市”属性。有一个属性
    Locality Name
    ,它具有LDAP显示名称
    l
    (小写l)

    没有“状态”属性。有一个属性
    State或Province Name
    ,它具有LDAP显示名称
    st

    没有“传真”属性。有一个属性
    Phone Fax Other
    ,它具有LDAP显示名称
    otherFacsimileTelephoneNumber

    在我看来,“部门”正在失败,而不是“移动”。在任何情况下,这些都是有效的LDAP属性。列出的错误是“无效操作”。我按可能性顺序猜测:

  • 您试图“替换”未设置的参数。替换操作可能要求首先存在属性
  • 你的数据不好
  • 查询失败的其他原因

  • 总的来说,我认为您的代码方法太复杂了。你有四个函数表面上做同样的事情,但是你必须在不同的情况下调用它们,这些情况通过查看代码是不明显的,函数的命名法并不能真正帮助记录它们所做的事情。我宁愿只传递一个包含用户所有属性的哈希表,让函数在拥有完整数据集时进行排序。函数本身应该能够从传入的哈希表中确定用户是否是经理,如果有时需要覆盖,我只需要为函数创建一个
    -Force
    参数。老实说,看起来您正试图在PowerShell中编写VBScript。

    您真的应该阅读:。这里有很多不必要的代码。这会让人们很沮丧想要帮助。我想说,你不需要任何gui组件来表达你的观点。谢谢Matt,我删除了gui部分。我还仔细检查了Csv,它在所有字段中都有数据。哪一行是236和284。你有一些密码