0

我正在解析一个配置任务,我需要获取所有没有“失败电子邮件”部分的任务。下面是来自任务 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"AND的任务报告的最佳方法是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
4

2 回答 2

1

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

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

应该是这样的:

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

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

于 2013-05-13T15:39:57.660 回答
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
于 2013-05-13T15:42:28.267 回答