Solved

PowerShell Sc;ript

Posted on 2014-07-24
12
454 Views
Last Modified: 2014-08-05
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
0
Comment
Question by:SydNal2009
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 180 total points
Comment Utility
Do you have an alias for surname at home?  Try AdUser.surname instead...
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 180 total points
Comment Utility
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Would need to see the script to troubleshoot further.
0
 
LVL 14

Assisted Solution

by:Justin Yeung
Justin Yeung earned 60 total points
Comment Utility
sn is a property of Get-Aduser, however it is not the default properties,
you will need to enable it by adding -properties sn or -properties *

example
get-aduser someone -properties sn

Default - DistinguishedName
Default - Enabled
Default - GivenName
Default - Name
Default - ObjectClass
Default - ObjectGUID
Default - SamAccountName
Default - SID
Addon Property - sn
Default - Surname
Default - UserPrincipalName
0
 

Author Comment

by:SydNal2009
Comment Utility
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
      }
0
 

Author Comment

by:SydNal2009
Comment Utility
How do you enable multiple extended properties?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 180 total points
Comment Utility
That's not the full script, right?
Can you include these associative lines (28, 34, etc) where the exception is thrown?
Also, can you post how you're getting $user filled - what method?  Mask any sensitive data as needed.

Lastly, lines where you're concatenating, like this:
  $Current = $Current + "," + $ADUser.Title
can be modified like this:
  $Current = "$Current,$($ADUser.Title)"
Hope that helps.
0
 

Author Comment

by:SydNal2009
Comment Utility
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
0
 
LVL 39

Assisted Solution

by:footech
footech earned 260 total points
Comment Utility
You do not "enable" those properties, they are either populated or not set.  Justin's comment was about the properties that are returned by default with the Get-ADUser cmdlet, and when additional properties/attributes beyond the default set are needed then you specify them with the -properties parameter.  It appears that you are already doing that with your Get-ADUser command above.

You will see that error when the object doesn't exist that you're trying to set the property for.  In this case, $ADUser must not exist.

I'm not really sure of the purpose of your script.  Rather than using a command like
$ADUser.givenName = $user.'givenName'
if you're trying to set the attribute for the user in AD you should use the Set-ADUser cmdlet.  If you're trying to do something else, please explain what that is and your reasoning.

And by the way, why do you have a filter of "(ObjectClass -eq "user")" for the Get-ADUser command?  It only returns user objects.
0
 

Author Comment

by:SydNal2009
Comment Utility
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
0
 
LVL 39

Accepted Solution

by:
footech earned 260 total points
Comment Utility
Try this.  I modified your foreach statement so that the $ADUser check uses an If/Else.  I put the Change-Attribute call inside the try block, but it could be moved out (just leave it in the else block).
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
	}
    Else
    {
	   	try{
	        Change-Attribute
		    Set-ADUser -Instance $ADUser
	    }
	    catch
	    {
		    $StrSetError = "There was a problem saving the user information"
		    Out-File $log -Append -InputObject $StrSetError
		    Out-File $log -Append -InputObject $_
	    }
    }
}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now