0

I am trying to make a script to put a bunch of information Active Directory. It imports the information from a csv file. The script errors out on City,State,Fax,mobile. The script is pretty in depth. I used several different sets of code together to make this. It is long but not sure why I am getting this error.

The Code :

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


    }

    }

    ##########################################################################################################

I took what both people said and re-evaluated my script and this is what I came Up with:

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



} 
Thomas Dickey
  • 43,185
  • 7
  • 51
  • 88
Andrew
  • 103
  • 4
  • 2
    You should really read: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). There is _a lot_ of unnecessary code here. Would make it very discouraging for people to want to help. I would say none of the gui components are required for you to make your point. – Matt Jan 16 '15 at 21:32
  • Thanks Matt I removed the gui portion. Also I double checked the Csv and it has data in all fields. – Andrew Jan 16 '15 at 21:48
  • Which lines are 236 and 284. You have some code repetition so it is hard to tell. I suggest putting comments in the code so people can find them. – Matt Jan 16 '15 at 21:51
  • Is this a typo `get-aduser -identity $i.userid ` since in that function Adattribute you do not have a variable `$i`? – Matt Jan 16 '15 at 21:52

2 Answers2

0

You have 2 different issues that you are presenting. First is most likely a typo and the second I will have to assume no longer exists since your current code suggests that it would not happen. I would like to point out that the line numbers in the errors don't match up with the code you posted (Yes, even the first post). You should point out where in the code your errors are occurring.

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

This is most likely due to a copy and paste typo. Proper debugging should have brought this issue to light. The error refers to this section of code: get-aduser -identity $i.userid. That code appears several times in your functions however $i in you index object in your main for loop. It does not exist in the scope of any of your functions. I would suspect you were copying and pasting code to create this error. Using the proper variables in your functions will fix this.

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

You also have several flavours of that error for "State", "city" and so forth. I have looked at the code that calls Get-Aduser in that context and do not see any issues. I would therefore conclude that the errors you are getting do not match your code (The goes back to an earlier point where I could not get the lines to match the errors.). For the above error to say it could not find the object "State" would mean that somehow $userid became the string "State" which as per your function declarations is the second argument. This is most likely another typo

Conclusion

Fix the lines with get-aduser -identity $i.userid to use the proper variables and consider saving the script and opening a new PowerShell session to test. Then see what happens with your code.

Welcome to SO

Nice to see that you went through the help tour. Hope you learned something and welcome to the StackOverflow Community!

Matt
  • 40,384
  • 7
  • 62
  • 97
  • Thanks Matt for your help. Fixed those lines and it still crashed on those lines. I ended up taking out a lot of the extra logic. Which seemed to solve the problem. It's more code but it doesn't error out. – Andrew Jan 19 '15 at 23:05
0

You're referring to LDAP properties that don't exist, and Set-ADUser is passing on that the LDAP server is complaining about it. You're passing Set-ADUser LDAP commands, so you need to speak to the LDAP server in LDAP properties and not the friendly names that Set-ADUser lets you use with cmdlet arguments. That means you have to use the LDAP display name.

You're not getting errors from Get-ADUser because as far as I recall Get-ADUser doesn't care if you call $User = Get-ADUser ... -Properties franksAndBeans. It will just set $User.franksAndBeans as null because LDAP read queries don't verify that the properties you're asking for are valid for the object being returned. Set-ADUser, however, does do that because it has to.

You can get a list of the actual properties your AD environment supports by running this:

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

(Source)

Or you can look at the online documentation for Active Directory to get a list of possible properties.

Anything mandatory is guaranteed to be present and will have a non-null value at all times, but there are very few mandatory properties. Anything optional may very well return a null value, because the property is not even present on the object if it has no value.

You can also run Get-ADUser ... -Properties *, but, again, that's not comprehensive since not all users possess all possible attributes.

There is no "City" attribute. There is an attribute Locality-Name, which has the LDAP display name l (lower case L).

There is no "State" attribute. There is an attribute State-Or-Province-Name, which has the LDAP display name st.

There is no "FAX" attribute. There is an attribute Phone-Fax-Other which has the LDAP display name otherFacsimileTelephoneNumber.

It looks to me like "department" is failing, not "mobile". In any case, those are both valid LDAP attributes. The error listed is "InvalidOperation." My guesses in order of likelihood:

  1. You're trying to "replace" a parameter that isn't set. The Replace operation may require that the property exist first.
  2. Your data is bad.
  3. Some other reason for the query failing.

Overall, I'd say your code approach is too complex. You've got four functions that ostensibly do the same thing, but you have to call them in different circumstances and those circumstances are really not obvious by looking at the code and the nomenclature of the functions doesn't really help document what they do. I'd rather just pass a hashtable with all the properties for the user in it and let the function sort out what to do when it has a complete set of data. The function itself should be able to determine from the incoming hashtable if the user is a manager or not, and if I needed to sometimes overwrite, I'd just create a -Force parameter for the function. Honestly, it looks like you're trying to write VBScript in PowerShell.

Bacon Bits
  • 26,878
  • 5
  • 51
  • 60