[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

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
0
SydNal2009
Asked:
SydNal2009
  • 4
  • 4
  • 3
  • +1
6 Solutions
 
sirbountyCommented:
Do you have an alias for surname at home?  Try AdUser.surname instead...
0
 
footechCommented:
Would need to see the script to troubleshoot further.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Justin YeungSenior Systems EngineerCommented:
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
 
SydNal2009Author Commented:
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
 
SydNal2009Author Commented:
How do you enable multiple extended properties?
0
 
sirbountyCommented:
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
 
SydNal2009Author Commented:
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
 
footechCommented:
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
 
SydNal2009Author Commented:
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
 
footechCommented:
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
 
footechCommented:
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now