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.ps 1:20 char:10
+ $ADUser. <<<< sn = $user.'sn'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'sn' cannot be found on this object; make sure it exists and is settable.
At C:\Scripts\MergeScript3.ps
+ $ADUser. <<<< sn = $user.'sn'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Would need to see the script to troubleshoot further.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.p s1:28 char:11
+ $ADUser. <<<< Title = $user.Title
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'Department' cannot be found on this object; make sure it exists and is
settable.
At C:\Scripts\Merge_Script4.p s1:34 char:11
+ $ADUser. <<<< Department = $user.Department
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'streetAddress' cannot be found on this object; make sure it exists and
is settable.
At C:\Scripts\Merge_Script4.p s1:40 char:11
+ $ADUser. <<<< streetAddress = $user.'streetAddress'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'postOfficeBox' cannot be found on this object; make sure it exists and
is settable.
At C:\Scripts\Merge_Script4.p s1:46 char:11
+ $ADUser. <<<< postOfficeBox = $user.'postOfficeBox'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'l' cannot be found on this object; make sure it exists and is settable.
At C:\Scripts\Merge_Script4.p s1:52 char:11
+ $ADUser. <<<< l = $user.l
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'st' cannot be found on this object; make sure it exists and is settable
.
At C:\Scripts\Merge_Script4.p s1:58 char:11
+ $ADUser. <<<< st = $user.State
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'postalCode' cannot be found on this object; make sure it exists and is
settable.
At C:\Scripts\Merge_Script4.p s1:64 char:11
+ $ADUser. <<<< postalCode = $user.postalCode
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
Property 'telephoneNumber' cannot be found on this object; make sure it exists an
d is settable.
At C:\Scripts\Merge_Script4.p s1:70 char:11
+ $ADUser. <<<< telephoneNumber = $user.telephoneNumber
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio n
-------------------------- ---------- ---------- -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
}
Property 'Title' cannot be found on this object; make sure it exists and is setta
ble.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< Title = $user.Title
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
Property 'Department' cannot be found on this object; make sure it exists and is
settable.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< Department = $user.Department
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
Property 'streetAddress' cannot be found on this object; make sure it exists and
is settable.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< streetAddress = $user.'streetAddress'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
Property 'postOfficeBox' cannot be found on this object; make sure it exists and
is settable.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< postOfficeBox = $user.'postOfficeBox'
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
Property 'l' cannot be found on this object; make sure it exists and is settable.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< l = $user.l
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
Property 'st' cannot be found on this object; make sure it exists and is settable
.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< st = $user.State
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
Property 'postalCode' cannot be found on this object; make sure it exists and is
settable.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< postalCode = $user.postalCode
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
Property 'telephoneNumber' cannot be found on this object; make sure it exists an
d is settable.
At C:\Scripts\Merge_Script4.p
+ $ADUser. <<<< telephoneNumber = $user.telephoneNumber
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentExceptio
--------------------------
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
}
ASKER
How do you enable multiple extended properties?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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=$Fals e
$WorkBook = $Excel.Workbooks.Open($Fil e)
$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,ti tle,depart ment,stree tAddress,p ostOfficeB ox,l,st,po stalCode,t elephoneNu mber
}
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
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=$Fals
$WorkBook = $Excel.Workbooks.Open($Fil
$Workbook.SaveAs($FileCSV,
$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,ti
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
HR-Merge-list.xlsx
NewScript.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.