[Webinar] Learn how to a build a cloud-first strategyRegister Now

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

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

0
kihl71
Asked:
kihl71
  • 6
  • 3
1 Solution
 
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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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
 
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

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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