Link to home
Start Free TrialLog in
Avatar of kihl71
kihl71Flag for Sweden

asked on

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

Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

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

Avatar of kihl71

ASKER

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
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.
o wait a second, where did you get "$newuser.employee.id"? I posted "$newuser.id"
Avatar of kihl71

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kihl71

ASKER

Thank you very much the last example worked. Can i ask you another question. Failing with get-aduser -filter?

Cheers
Mattias
Sure. What's the problem there? Have you seen this page? Under the heading Filter are ample examples.