【问题标题】:Powershell Script from CSV来自 CSV 的 Powershell 脚本
【发布时间】:2015-01-16 21:24:32
【问题描述】:

我正在尝试制作一个脚本来放置一堆信息 Active Directory。它从 csv 文件中导入信息。脚本在 City、State、Fax、mobile 上出错。剧本相当有深度。我一起使用了几组不同的代码来完成这个。很长,但不确定我为什么会收到此错误。

代码:

    ############################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 ____________________________________________________________________________________



} 

【问题讨论】:

  • 你真的应该阅读:How to create a Minimal, Complete, and Verifiable example。这里有很多不必要的代码。会让想要帮助的人非常沮丧。我想说你不需要任何 gui 组件来表达你的观点。
  • 感谢 Matt 我删除了 gui 部分。我还仔细检查了 Csv,它在所有字段中都有数据。
  • 哪一行是 236 和 284。你有一些代码重复,所以很难说。我建议将 cmets 放入代码中,以便人们可以找到它们。
  • 这是一个错字get-aduser -identity $i.userid ,因为在那个函数Adattribute中你没有变量$i

标签: powershell csv active-directory


【解决方案1】:

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

  1. You cannot call a method on a null-valued expression.

这很可能是由于复制和粘贴拼写错误。适当的调试应该会发现这个问题。错误是指这段代码:get-aduser -identity $i.userid。该代码在您的函数中出现多次 次,但 $i 在您的主 for 循环中的索引对象中。它不存在于您的任何函数的范围内。我怀疑您是在复制和粘贴代码来创建此错误。在你的函数中使用适当的变量将解决这个问题。

  1. get-aduser : Cannot find an object with identity: 'state'

对于“州”、“城市”等,您也有几种类似的错误。我查看了在该上下文中调用 Get-Aduser 的代码,没有发现任何问题。因此,我会得出结论,您遇到的错误与您的代码不匹配(回到之前的点,我无法让行与错误匹配。)。对于上述错误来说,它找不到对象“State”意味着$userid 以某种方式变成了字符串“State”,根据您的函数声明,它是第二个参数。这很可能是另一个错字

结论

修复带有get-aduser -identity $i.userid 的行以使用正确的变量,并考虑保存脚本并打开一个新的 PowerShell 会话进行测试。然后看看你的代码会发生什么。

欢迎来到 SO

很高兴看到您完成了帮助之旅。希望您学到了一些东西,欢迎来到 StackOverflow 社区!

【讨论】:

  • 感谢马特的帮助。修复了这些行,它仍然在这些行上崩溃。我最终去掉了很多额外的逻辑。这似乎解决了这个问题。这是更多的代码,但它不会出错。
【解决方案2】:

您指的是不存在的 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”。我的猜测按可能性排序:

  1. 您正在尝试“替换”未设置的参数。 Replace 操作可能要求属性首先存在。
  2. 您的数据不正确。
  3. 查询失败的其他一些原因。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 2021-10-23
    • 2018-08-18
    • 2018-11-15
    • 1970-01-01
    • 2014-08-09
    • 2021-01-04
    相关资源
    最近更新 更多