Solved

PowerShell Sc;ript

Posted on 2014-07-24
12
472 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
ID: 40218168
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
ID: 40218173
0
 
LVL 39

Expert Comment

by:footech
ID: 40218316
Would need to see the script to troubleshoot further.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 14

Assisted Solution

by:Justin Yeung
Justin Yeung earned 60 total points
ID: 40218559
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
ID: 40222964
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
ID: 40223185
How do you enable multiple extended properties?
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 180 total points
ID: 40223254
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
ID: 40223373
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
ID: 40223837
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
ID: 40224670
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
ID: 40224741
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
ID: 40224760
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

774 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