读取几个相同类型的特定XML节点

时间:2013-05-13 15:16:32

标签: xml parsing powershell

我正在解析配置任务,我需要获得所有没有“失败时发送电子邮件”部分的任务。下面是xml任务的部分。

<Settings>
    <Tasks>
        <Task ID="916360623" Name="name1" Active="0" NextEID="21" CacheNames="random">
            <Schedules/>
            <NextActions>
                <NextAction DoIfSuccess="1" DoIfFailure="0" DoIfNoAction="0" DoAfter="Task" Type="Task" TaskID="545310265"/>
                <NextAction DoIfSuccess="0" DoIfFailure="1" DoIfNoAction="0" DoAfter="Task" Type="Email" HostID="385322183" AddressTo="email1@email.com, email2@email.com" Subject="[TaskName] - [TaskStatus]" Message="emailMessageFail"/>
                <NextAction DoIfSuccess="1" DoIfFailure="0" DoIfNoAction="0" DoAfter="Task" Type="Email" HostID="385322183" AddressTo="email1@email.com, email2@email.com" Subject="[TaskName] - [TaskStatus]" Message="emailMessageSuccess"/>
            </NextActions>
        </Task>
        <Task ID="701754236" Name="Task2" Active="0" NextEID="21" CacheNames="random">
            <NextActions>
                <NextAction DoIfSuccess="1" DoIfFailure="0" DoIfNoAction="0" DoAfter="Task" Type="Task" TaskID="840519929"/>
                <NextAction DoIfSuccess="0" DoIfFailure="1" DoIfNoAction="0" DoAfter="Task" Type="Email" HostID="385322183" AddressTo="email1@email.com, email2@email.com" Subject="[TaskName] - [TaskStatus]" Message="emailMessage"/>
            </NextActions>
        </Task>             
    </Tasks>
</Settings>

我有一个PowerShell脚本循环遍历每个任务,而每个<NextAction>但是,它似乎不能成功地只执行我想要的任务。获得没有DoIfFailue="1"Type="Email"的任务报告的最佳方法是什么?

set-executionpolicy unrestricted
[xml]$config = Get-Content c:\path\config.xml
$allTasks = @()
foreach($task in $config.Settings.Tasks.task){
    $taskID = $task.name
    foreach($nextAction in $task.NextActions.NextAction){
        if(($nextAction.DoIfFailure = "1") -AND ($nextAction.type = "Email")){
            $address = $nextAction.addressto
            $subject = $nextAction.subject
            $message = $nextAction.message
            $allTasks += @(New-Object -TypeName psobject -Property @{'ID' = $taskID; 'emailAddress' = $address; 'emailSubject' = $subject; 'emailMessage' = $message})
        }
    }
}
$allTasks | Export-Csv -Path c:\path\output.csv -notype

2 个答案:

答案 0 :(得分:1)

Powershell中的等于测试是-eq,而不是=。所以这个:

if(($nextAction.DoIfFailure = "1") -AND ($nextAction.type = "Email")){

应该是这样的:

if(($nextAction.DoIfFailure -eq "1") -AND ($nextAction.type -eq "Email")){

此外,您每次都不需要set-executionpolicy unrestricted,每台服务器只运行一次。

答案 1 :(得分:0)

你也可以试试这个:

$allTasks = @()
$doc=New-Object -typename System.Xml.XmlDocument
$doc.Load("c:\path\config.xml")
$nextActions = $doc.SelectNodes("//NextAction[@DoIfFailure='1' and @Type='Email']")

foreach($nextAction in $nextActions ){
            $address = $nextAction.addressto
            $subject = $nextAction.subject
            $message = $nextAction.message
            $allTasks += @(New-Object -TypeName psobject -Property @{'ID' = $taskID; 'emailAddress' = $address; 'emailSubject' = $subject; 'emailMessage' = $message})
    }

$allTasks | Export-Csv -Path c:\path\output.csv -notype