Link to home
Start Free TrialLog in
Avatar of SydNal2009
SydNal2009

asked on

PowerShell Sc;ript

I am getting the error below everytime I run the this powershell script I have created. It works fine at my house and my friend's house but not at my job it always gives me the same error below.

Property 'sn' cannot be found on this object; make sure it exists and is settable.
At C:\Scripts\MergeScript3.ps1:20 char:10
+  $ADUser. <<<< sn = $user.'sn'
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Would need to see the script to troubleshoot further.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SydNal2009
SydNal2009

ASKER

Thanks, sirbounty the AdUser.surname worked but I have the following properties which are giving me the same error:

Property 'Title' cannot be found on this object; make sure it exists and is setta
ble.
At C:\Scripts\Merge_Script4.ps1:28 char:11
+         $ADUser. <<<< Title = $user.Title
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Property 'Department' cannot be found on this object; make sure it exists and is
settable.
At C:\Scripts\Merge_Script4.ps1:34 char:11
+         $ADUser. <<<< Department = $user.Department
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Property 'streetAddress' cannot be found on this object; make sure it exists and
is settable.
At C:\Scripts\Merge_Script4.ps1:40 char:11
+         $ADUser. <<<< streetAddress = $user.'streetAddress'
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Property 'postOfficeBox' cannot be found on this object; make sure it exists and
is settable.
At C:\Scripts\Merge_Script4.ps1:46 char:11
+         $ADUser. <<<< postOfficeBox = $user.'postOfficeBox'
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Property 'l' cannot be found on this object; make sure it exists and is settable.
At C:\Scripts\Merge_Script4.ps1:52 char:11
+         $ADUser. <<<< l = $user.l
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Property 'st' cannot be found on this object; make sure it exists and is settable
.
At C:\Scripts\Merge_Script4.ps1:58 char:11
+         $ADUser. <<<< st = $user.State
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Property 'postalCode' cannot be found on this object; make sure it exists and is
settable.
At C:\Scripts\Merge_Script4.ps1:64 char:11
+         $ADUser. <<<< postalCode = $user.postalCode
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
Property 'telephoneNumber' cannot be found on this object; make sure it exists an
d is settable.
At C:\Scripts\Merge_Script4.ps1:70 char:11
+         $ADUser. <<<< telephoneNumber = $user.telephoneNumber
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException
 
-----------------------------------------------below is the part of the script that is causing the error---------------------------------------------

if ($user.Title -ne '')
      {
            $Current = $Current + "," + $ADUser.Title
            $ADUser.Title = $user.Title
            $New = $New + "," + $ADUser.Title
      }
      if ($user.Department -ne '')
      {
            $Current = $Current + "," + $ADUser.Department
            $ADUser.Department = $user.Department
            $New = $New + "," + $ADUser.Department
      }
      if ($user.'Street Address' -ne '')
      {
            $Current = $Current + "," + $ADUser.streetAddress
            $ADUser.streetAddress = $user.'streetAddress'
            $New = $New + "," + $ADUser.streetAddress
      }
      if ($user.'P.O. Box' -ne '')
      {
            $Current = $Current + "," + $ADUser.postOfficeBox
            $ADUser.postOfficeBox = $user.'postOfficeBox'
            $New = $New + "," + $ADUser.postOfficeBox
      }
      if ($user.City -ne '')
      {
            $Current = $Current + "," + $ADUser.l
            $ADUser.l = $user.l
            $New = $New + "," + $ADUser.l
      }
      if ($user.State -ne '')
      {
            $Current = $Current + "," + $ADUser.st
            $ADUser.st = $user.State
            $New = $New + "," + $ADUser.st
      }
      if ($user.Zip -ne '')
      {
            $Current = $Current + "," + $ADUser.postalCode
            $ADUser.postalCode = $user.postalCode
            $New = $New + "," + $ADUser.postalCode
      }
      if ($user.Phone -ne '')
      {
            $Current = $Current + "," + $ADUser.telephoneNumber
            $ADUser.telephoneNumber = $user.telephoneNumber
            $New = $New + "," + $ADUser.telephoneNumber
      }
      if ($user.Email -ne '')
      {
            $Current = $Current + "," + $ADUser.mail
            $ADUser.mail = $user.mail
            $New = $New + "," + $ADUser.mail
      }
How do you enable multiple extended properties?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is not the whole script, but I hope this will help.  I think the problem is those extended properties for $ADUser need to be enabled. let me know if you agree with Justin and me.

Param(
         [Parameter(
        Mandatory = $True,
        Position = 0
    )]
    [string]$logfiledir
)

function Change-Attribute
{
      if ($user.'First Name' -ne '')
      {
            $Current = $EmpID + "," + $ADUser.GivenName
            $ADUser.givenName = $user.'givenName'
            $New = $EmpID + "," + $ADUser.GivenName
      }
      if ($user.'Last Name' -ne '')
      {
            $Current = $Current + "," + $ADUser.surname
            $ADUser.surname = $user.'surname'
            $New = $New + "," + $ADUser.surname
      }
      if ($user.Title -ne '')
      {
            $Current = $Current + "," + $ADUser.Title
            $ADUser.Title = $user.Title
            $New = $New + "," + $ADUser.Title
      }
      if ($user.Department -ne '')
      {
            $Current = $Current + "," + $ADUser.Department
            $ADUser.Department = $user.Department
            $New = $New + "," + $ADUser.Department
      }
      if ($user.'Street Address' -ne '')
      {
            $Current = $Current + "," + $ADUser.streetAddress
            $ADUser.streetAddress = $user.'streetAddress'
            $New = $New + "," + $ADUser.streetAddress
      }
      if ($user.'P.O. Box' -ne '')
      {
            $Current = $Current + "," + $ADUser.postOfficeBox
            $ADUser.postOfficeBox = $user.'postOfficeBox'
            $New = $New + "," + $ADUser.postOfficeBox
      }
      if ($user.City -ne '')
      {
            $Current = $Current + "," + $ADUser.l
            $ADUser.l = $user.l
            $New = $New + "," + $ADUser.l
      }
      if ($user.State -ne '')
      {
            $Current = $Current + "," + $ADUser.st
            $ADUser.st = $user.State
            $New = $New + "," + $ADUser.st
      }
      if ($user.Zip -ne '')
      {
            $Current = $Current + "," + $ADUser.postalCode
            $ADUser.postalCode = $user.postalCode
            $New = $New + "," + $ADUser.postalCode
      }
      if ($user.Phone -ne '')
      {
            $Current = $Current + "," + $ADUser.telephoneNumber
            $ADUser.telephoneNumber = $user.telephoneNumber
            $New = $New + "," + $ADUser.telephoneNumber
      }
      if ($user.Email -ne '')
      {
            $Current = $Current + "," + $ADUser.mail
            $ADUser.mail = $user.mail
            $New = $New + "," + $ADUser.mail
      }
      
      Out-File $log -append -InputObject $Current
      Out-File $log -append -InputObject $New
}

#Prod File Names
$File = "C:\Scripts\HR Merge list.xlsx"
$FileCSV = "C:\Scripts\HR Merge list.csv"
#$logfiledir = "C:\Scripts"

# Install the Active Directory modules for this session
Import-Module ActiveDirectory

# Get today's date and set logfile location
$date = Get-Date -Format "MMM d yyyy hh mm"
$log = $logfiledir + "\HR-Modifications " + $date + ".txt"

$xlCSV = 6
$Excel = New-Object -Com Excel.Application
$Excel.visible = $False
$Excel.displayalerts=$False
$WorkBook = $Excel.Workbooks.Open($File)
$Workbook.SaveAs($FileCSV,$xlCSV)
$Excel.quit()

$hrwareusers = import-csv $FileCSV

Foreach ($user in $hrwareusers)
{
      $EmpID = $user.employeeID
      try
      {
            $ADUser = Get-ADUser -Filter {(employeeID -eq $EmpID) -and (ObjectClass -eq "user") } -Properties employeeID,givenName,sn,title,department,streetAddress,postOfficeBox,l,st,postalCode,telephoneNumber
      }
      catch
      {
            $StrUserError = "There was a problem retrieving user $EmpID"
            Out-File $log -Append -InputObject $StrUserError
            Out-File $log -Append -InputObject $_
      }
      
      if ($ADUser -eq $null)
      {
            $StrUserNullError = "There was no user found with Employee ID of $EmpID"
            Out-File $log -Append -InputObject $StrUserNullError
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the whole script. Also, what I'm trying to accomplish is to take a spreadsheet sent to me by HR convert it to a .csv file and check each user on that list based on employeeID with what is in AD, and if there are any discrepencies the information on the  spreadsheet provided by HR takes precedence, therefore, that info is used to modify the propertie's attribute value. I have attached my test spreadsheet and the full script.
HR-Merge-list.xlsx
NewScript.txt
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, a lot of your If condition checks in the Change-Attribute function reference property names that are not equal to the headers in your .CSV (e.g. "Last name" instead of "surname").  These need to be made consistent with the properties of $user which are present and used in the block following the If condition.