Powershell add attribute to xml node

Ive modified a script i found on the net to suit my needs. I want to add samaccountname to employee attribute 'id'.
Like this.

<employees version='1.0'>
      <employee id='Username'>
      <name></name>

How can i do that?

The script.

# Import active directory module
import-module activedirectory

# create template

$template =@'
<employees version='1.0'>
	<employee>
	<name></name>
	<samaccountname></samaccountname>
	<telephonenumber></telephonenumber>
	<title></title>
	<l></l>
	<department></department>
	<buildingName></buildingName>
	<extensionattribute10></extensionattribute10>
	<roomnumber></roomnumber>
	</employee>
</employees>
'@
$template | Out-File c:\inetpub\wwwroot\VingeOfficexml\users.xml -encoding UTF8

# load template into XML object
$xml = New-Object xml

$xml.Load("c:\inetpub\wwwroot\VingeOfficexml\users.xml")
if ($success -ne $true) {
    $($xml.LastErrorText)
    exit
}


# grab template user
$newuser = (@($xml.employees.employee)[0]).Clone()

# Grab the users from active directory
get-aduser user -properties Name,samaccountName,telephonenumber,title,l,department,buildingName,ExtensionAttribute10,roomNumber|

ForEach-Object {
	$newuser = $newuser.clone()
    $newuser.Name = $_.Name
	$newuser.samaccountname = $_.samaccountname
	$newuser.telephonenumber = $_.telephonenumber
	$newuser.title = $_.title
	$newuser.l = $_.l
	$newuser.department = $_.department
	$newuser.buildingName = $_.buildingName[0].toString()
    $newuser.extensionattribute10 = $_.extensionattribute10.toString()
    $newuser.roomnumber = $_.roomnumber[0].tostring()
    
	$xml.employees.AppendChild($newuser) > $null
}

#clean the xml from template
$xml.employees.employee | 
Where-Object { $_.name -eq "" } | ForEach-Object  { [void]$xml.employees.RemoveChild($_) }
# save xml to file
$xml.Save("c:\inetpub\wwwroot\VingeOfficexml\employees.xml")

Open in new window

kihl71System EngineneerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
You can add the (empty) attribute to the template (line 3 in the code below, your line 8):
$template =@'
<employees version='1.0'>
      <employee id=''>
      <name></name>
      <samaccountname></samaccountname>
      <telephonenumber></telephonenumber>
      <title></title>
      <l></l>
      <department></department>
      <buildingName></buildingName>
      <extensionattribute10></extensionattribute10>
      <roomnumber></roomnumber>
      </employee>
</employees>
'@

Open in new window

Then in the loop, fill the "id" (after your current line 40):
      $newuser.id = $_.samaccountname

Open in new window

0
kihl71System EngineneerAuthor Commented:
Thanks for answering, but it Dosent work, get the following error.

Property 'id' cannot be found on this object; make sure it exists and is settable.
At C:\Powerscipts\UserLocations\userx.ps1:37 char:24
+     $newsuser.employee. <<<< id = $_.samaccountname
    + CategoryInfo          : InvalidOperation: (id:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
0
Robert SchuttSoftware EngineerCommented:
Can you post your current script please because line 37 doesn't make sense (or something else might have changed), if you added it to the existing code it should be line 41.
0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Robert SchuttSoftware EngineerCommented:
o wait a second, where did you get "$newuser.employee.id"? I posted "$newuser.id"
0
kihl71System EngineneerAuthor Commented:
sorry i tried adding employee after failure and then posted wrong error message.  
      
This is the original error output after adding.. $newsuser.id = $_.samaccountname

Property 'id' cannot be found on this object; make sure it exists and is settable.
At C:\Powerscipts\UserLocations\userx.ps1:37 char:15
+     $newsuser. <<<< id = $_.samaccountname
    + CategoryInfo          : InvalidOperation: (id:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound
0
Robert SchuttSoftware EngineerCommented:
Still, 2 problems: newuser/newsuser is a different variable name and 37 is a different line number from your original code. Normally shouldn't matter but I can't tell what's wrong without seeing what else you may have changed in the code.
0
Robert SchuttSoftware EngineerCommented:
By the way, an alternative would be:
$newuser.SetAttribute("id", $_.samaccountname)

Open in new window

Again, you can call this right after the clone() call. With this code you don't even need to include the empty attribute in the template.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kihl71System EngineneerAuthor Commented:
Thank you very much the last example worked. Can i ask you another question. Failing with get-aduser -filter?

Cheers
Mattias
0
Robert SchuttSoftware EngineerCommented:
Sure. What's the problem there? Have you seen this page? Under the heading Filter are ample examples.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.