• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Powershell: Dig through a XML and pass two values into command.

SketchMaster

Diamond Member
Trying to help teammate migrate data to a new app by digging through an exported XML file. We need two bits of info:

-Friendly name (UserRole Name)
-Fully Qualified Domain name (User UserName)

Here is an example of the XML:

Code:
<?xml version="1.0" encoding="utf-8"?>
<UserRoles xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UserRole Name="My USER 1" DisplayName="My USER 1" Description="" Profile="Operator">
    <User UserName="DOMAIN\MyUSER 1" />
  </UserRole>
  <UserRole Name="My USER 2" DisplayName="My USER 2" Description="" Profile="ReadOnlyOperator">
    <User UserName="DOMAIN\MyUSER2" />
  </UserRole>
  <UserRole Name="My USER 3" DisplayName="My USER 3" 
  <UserRole Name="My USER 3" DisplayName="My USER 3" Description="" Profile="Operator">
    <User UserName="DOMAIN\My.USER 3" />

(FQDNs are mangled on purpose in this example)

It's easy enough to pull out the list of friendly names:

Code:
[xml]$XML = Get-Content C:\Users\SketchMaster\Desktop\userroles.xml
$names = ($xml.UserRoles.ChildNodes.name)
ForEach  ($name in $names)
     {Write-host "this is $name"}

Or the list of FQDNs:

Code:
[xml]$XML = Get-Content C:\Users\SketchMaster\Desktop\userroles.xml
$Users = $users = ($xml.UserRoles.ChildNodes.user.username)
ForEach  ($user in $users)
     {Write-host "this is $user"}

But say I wanted to do something like this...

Pseudo code:
Code:
#get data
[xml]$XML = Get-Content C:\Users\SketchMaster\Desktop\userroles.xml
$Users = ($xml.UserRoles.ChildNodes.user.username)
$Names = ($xml.UserRoles.ChildNodes.name)

#Start command

Foreach (UserRole)
     {
     #-Name = Friendly name | -User = FQDNs
     Add-UserRole -Name '$Name' -Operator -User '$User'
     }

I've tried every combination of loop commands I know of, but I can't get it to go through the list front top to bottom and pull out the info two by two without mixing up the data. Any ideas on how to do this?
 
Last edited:
You need two loops.

Code:
#get data
[xml]$XML = Get-Content C:\Users\SketchMaster\Desktop\userroles.xml

#Start command

Foreach ($role in $xml.UserRoles.ChildNodes)
     {
        Write-Host $role.Name

        Foreach ($user in $role.ChildNodes)
        {
             $text = "Add-UserRole -Name '" + $role.Name + "' -Operator -User '" + $user.UserName + "'"

             Write-Host $text
            #Add-UserRole -Name $role.Name -Operator -User $user.UserName
        }
     }
 
That got me right on track. Had to tweak it a touch to keep it from glitching on sections that had more childnodes.

Thanks!
 
Back
Top