简体   繁体   中英

read xml file with powershell in windows server 2003

can you please help me with this code below?

[System.Xml.XmlDocument]$xd = New-Object System.Xml.XmlDocument
$file = "C:\PS\SetUP.xml";
$xd.LoadXml($file);
#$root = $xd.DocumentElement 
Write-Host $xd.SelectSingleNode("/Setup").Attributes.GetNamedItem("WebAppUrl").Value

This code works fine when I run it in windows server 2008, but it does not work in windows server 2003.

I tested it in C# in the same environment (win server 2003) and it works, but not in Powershell.

I think there could be some Powershell version issue.

Could you please help me?

Thank you


Xml file

<?xml version="1.0" encoding="utf-8"?>
<Setup WebAppUrl="http://sp2007">
    <SiteCollection Name="Report" Url="Report Requests">
    <Lists>
        <List Name="Report Requests" Url="ReportRequests" />    
        <List Name="Report Request Tasks" Url="ReportRequestTasks" />   
    </Lists>
    <Features>
    <Feature>ContentTypeSyndication</Feature>
    </Features>
    <Site Name="Child 1" Url="child1" Template="STS#0"/>
    <Site Name="Child 2" Url="child2" Template="STS#0"/>
    <Site Name="Child 3" Url="child3" Template="STS#0"/>
    </SiteCollection>
</Setup>

The Powershell version on Windows 2003 is 1.

The version on Windows 2008 is 2.

Link: http://en.wikipedia.org/wiki/Windows_PowerShell#Versions

Simple as that. You can try to upgrade it AFAIK.

Edit: Update procedure -> http://maxt2posh.wordpress.com/2009/12/23/update-powershell-v1-to-v2-windows-management-framework/

I don't have v1 but you can read xml files and navigate them the same way regardless of OS version. Can you share the xml file?

[xml]$xd = Get-Content C:\PS\SetUP.xml
$xd.SelectSingleNode("/Setup").WebAppUrl

http://sp2007

If there's only one Setup node then you can get the value with:

$xd.Setup.WebAppUrl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM