Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Processing XML

Reply
 
Old 10-21-2008   #1 (permalink)
Jacob
Guest


 
 

Processing XML

I have the following script:

Param(
$prefix,
$volumeTrustees
)


#Load the Trustees files
[xml]$trustees = get-content $volumeTrustees

#Get all the Valid nodes

$validNodes=$trustees.SelectNodes("//Path[starts-with(text(), '$prefix')]/..")

$validNodes | ft


The output is:

subdirectory /apps/Risk/Advisor Switchboard
{User, User, User, User...}
subdirectory /apps/softlogm
{User, User, User, User...}
subdirectory /apps/SuperReport
{User, User, User, User}
subdirectory /apps/Risk/elmapp
{User, User}

And the input looks something like:
<Path>/apps/ABC</Path>
<User
rights="_R____F_">.CN=GJudd.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User>
<User
rights="_RWC__F_">.CN=AUpchurch.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User>
<User
rights="_RWCEMF_">.CN=Everyone.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User>
<User
rights="_R____F_">.CN=RPhillips.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User>
<User
rights="_RWCEMF_">.CN=Maximus.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User>
</Trustee_List>
<Trustee_List type="subdirectory">
<Path>/apps/ABC3</Path>
<User
rights="_RWCEMF_">.CN=Maximus.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User>
<User
rights="_RWCEMF_">.CN=TeamBMH.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User>
</Trustee_List>


My problem is I'd like to select each Path and then list all the 'User'
nodes for that path. I can't help think there is a better way than using
ForEach-object.

--
**********************
Jacob


My System SpecsSystem Spec
Old 10-21-2008   #2 (permalink)
RickB
Guest


 
 

Re: Processing XML

On Oct 21, 2:18*am, Jacob <jacob(AT)hfws.net.nospam> wrote:
Quote:

> I have the following script:
>
> Param(
> * * * $prefix,
> * * * $volumeTrustees
> * * * )
>
> #Load the Trustees files
> [xml]$trustees = get-content $volumeTrustees
>
> #Get all the Valid nodes
>
> $validNodes=$trustees.SelectNodes("//Path[starts-with(text(), '$prefix')]/..")
>
> $validNodes | ft
>
> The output is:
>
> subdirectory * * * * * * * * * * * * * */apps/Risk/Advisor Switchboard * * *
> * *{User, User, User, User...}
> subdirectory * * * * * * * * * * * * * */apps/softlogm * * * * * * * * * * *
> * *{User, User, User, User...}
> subdirectory * * * * * * * * * * * * * */apps/SuperReport * * * * * * * * * *
> * *{User, User, User, User}
> subdirectory * * * * * * * * * * * * * */apps/Risk/elmapp * * * * * * * * * *
> * *{User, User}
>
> And the input looks something like:
> <Path>/apps/ABC</Path>
> <User
> rights="_R____F_">.CN=GJudd.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</User*>
> <User
> rights="_RWC__F_">.CN=AUpchurch.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</*User>
> <User
> rights="_RWCEMF_">.CN=Everyone.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</U*ser>
> <User
> rights="_R____F_">.CN=RPhillips.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</*User>
> <User
> rights="_RWCEMF_">.CN=Maximus.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</Us*er>
> </Trustee_List>
> <Trustee_List type="subdirectory">
> <Path>/apps/ABC3</Path>
> <User
> rights="_RWCEMF_">.CN=Maximus.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</Us*er>
> <User
> rights="_RWCEMF_">.CN=TeamBMH.OU=AllStaff.OU=BRIS.O=BDOK.T=BDOKENDALLS.</Us*er>
> </Trustee_List>
>
> My problem is I'd like to select each Path and then list all the 'User'
> nodes for that path. I can't help think there is a better way than using
> ForEach-object.
>
> --
> **********************
> Jacob

$validNodes|%{$t=$_.Path;$_.User|write-host $t,$_}

The key to nesting foreach pipelines (i.e. pipelines with %{}) is to
store the $_ value (or the part of it you need) in a temp variable
before creating the nested one. It's also possible to access the
parent scope of $_ but I usually consider that just too messy to
bother with.
My System SpecsSystem Spec
Old 10-21-2008   #3 (permalink)
Hal Rottenberg [MVP]
Guest


 
 

Re: Processing XML

RickB wrote:
Quote:

> $validNodes|%{$t=$_.Path;$_.User|write-host $t,$_}
>
> The key to nesting foreach pipelines (i.e. pipelines with %{}) is to
> store the $_ value (or the part of it you need) in a temp variable
> before creating the nested one. It's also possible to access the
> parent scope of $_ but I usually consider that just too messy to
> bother with.
Or you can use Foreach-the-Statement (not foreach-object-the-cmdlet):

foreach ( $node in $validNodes ) { $node.User | % { $node.Path, $_ } }

is one example...

--
$signature = "Hal Rottenberg, MVP - Admin Frameworks"
$projects = @{ title = "Blog Author"; url = "http://halr9000.com" },
@{ title = "Co-host"; url = "http://powerscripting.net" },
@{ title = "Community Director"; url = "http://PowerShellCommunity.org" },
@{ title = "Psi Webmaster"; url = "http://psi-im.org" }
My System SpecsSystem Spec
Old 10-21-2008   #4 (permalink)
Jacob
Guest


 
 

Re: Processing XML

Thanks guys that did the trick.
--
**********************
Jacob



"Hal Rottenberg [MVP]" wrote:
Quote:

> RickB wrote:
Quote:

> > $validNodes|%{$t=$_.Path;$_.User|write-host $t,$_}
> >
> > The key to nesting foreach pipelines (i.e. pipelines with %{}) is to
> > store the $_ value (or the part of it you need) in a temp variable
> > before creating the nested one. It's also possible to access the
> > parent scope of $_ but I usually consider that just too messy to
> > bother with.
>
> Or you can use Foreach-the-Statement (not foreach-object-the-cmdlet):
>
> foreach ( $node in $validNodes ) { $node.User | % { $node.Path, $_ } }
>
> is one example...
>
> --
> $signature = "Hal Rottenberg, MVP - Admin Frameworks"
> $projects = @{ title = "Blog Author"; url = "http://halr9000.com" },
> @{ title = "Co-host"; url = "http://powerscripting.net" },
> @{ title = "Community Director"; url = "http://PowerShellCommunity.org" },
> @{ title = "Psi Webmaster"; url = "http://psi-im.org" }
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
More processing power General Discussion
Log In Out File Processing PowerShell
Asynchronous Processing PowerShell
More text processing. PowerShell
Event Log taking up all my processing Vista performance & maintenance


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46