0

我的数据看起来类似于这个问题最底部的 xml。这是数千个任务中的 3 个。有些有<DayOfWeek>节点,有些没有。我正在努力获取这些数据并将其放入 csv 中,以便可以将其导入 sql。

我的目标是让每一天成为表格中的一列,每一行代表一个任务。如果计划在给定日期运行,则相应日期为 1,否则为 0。对于此问题底部的以下数据,表格如下所示

| taskID | mon | tue | wed | thur | fri | sat | sun |
|________|_____|_____|_____|______|_____|_____|_____|
|    1   |  1  |  0  |  0  |  1   |  1  |  1  |  0  |
|    2   |  1  |  0  |  0  |  0   |  1  |  0  |  0  |
|    3   |  1  |  1  |  1  |  1   |  1  |  1  |  1  |

我当前的 ps 脚本使这些数据更加垂直而不是水平。代码摘录

foreach($task in $config.Settings.Tasks.Task){
    $taskID = $task.ID
    foreach($DoW in $task.Schedules.Schedule.Days.DayOfWeek){
        $newCSV += @(New-Object -TypeName psobject -Property @{'taskID' = $taskID; 'Day' = $DoW})
    }
}
$newCSV | Export-Csv -Path c:\users\mhopkins\desktop\micSchedule\DoW.csv -notype

你们能帮我把这些数据在表格中更好地工作吗?

XML 片段

<Task ID="1" Name="name1" Active="1" NextEID="11">
    <Schedules>
        <Schedule OnlyUntilFirstSuccess="0" FailIfNoSuccessInSched="0" RunEvenIfNotif="0">
            <Days>
                <DayOfWeek>Monday</DayOfWeek>
                <DayOfWeek>Friday</DayOfWeek>
                <DayOfWeek>Thursday</DayOfWeek>
                <DayOfWeek>Saturday</DayOfWeek>
            </Days>
        </Schedule>
    </Schedules>
</Task>
<Task ID="2" Name="name2" Active="1" NextEID="12">
    <Schedules>
        <Schedule OnlyUntilFirstSuccess="0" FailIfNoSuccessInSched="0" RunEvenIfNotif="0">
            <Days>
                <DayOfWeek>Monday</DayOfWeek>
                <DayOfWeek>Friday</DayOfWeek>
            </Days>
        </Schedule>
    </Schedules>
</Task>
<Task ID="3" Name="name3" Active="0" NextEID="24" TT="COS" AR="0">
    <Schedules>
        <Schedule OnlyUntilFirstSuccess="0" FailIfNoSuccessInSched="0" RunEvenIfNotif="1">
            <Days>
                <DayOfWeek>Monday</DayOfWeek>
                <DayOfWeek>Tuesday</DayOfWeek>
                <DayOfWeek>Wednesday</DayOfWeek>
                <DayOfWeek>Thursday</DayOfWeek>
                <DayOfWeek>Friday</DayOfWeek>
                <DayOfWeek>Saturday</DayOfWeek>
                <DayOfWeek>Sunday</DayOfWeek>
            </Days>
        </Schedule>
    </Schedules>
</Task>
4

2 回答 2

1

我必须在您的 XML 代码片段中添加行以使其适用于 PowerShell。

<xml>
...
</xml>

我正在使用 PowerShell v 3.0 并让它工作。

$config = [xml](get-content 'dat.xml')

foreach($task in $config.xml.Task){

    $props = @{
        'taskID'=$task.ID
        'Sunday'=$NULL
        'Monday'=$NULL
        'Tuesday'=$NULL
        'Wednesday'=$NULL
        'Thursday'=$NULL
        'Friday'=$NULL
        'Saturday'=$NULL
    }

    foreach($DoW in $task.Schedules.Schedule.Days.DayOfWeek){
        foreach($day in $DoW){
            #POWERSHELL!!!
            $props.$day = $true #Or equals 1 whatever  
        }   
    }

    $newCSV += @(New-Object -TypeName psobject -Property $props)
}
$newCSV

这是我得到的输出。

Thursday  : True
Sunday    :
taskID    : 1
Saturday  : True
Friday    : True
Tuesday   :
Monday    : True
Wednesday :

Thursday  :
Sunday    :
taskID    : 2
Saturday  :
Friday    : True
Tuesday   :
Monday    : True
Wednesday :

Thursday  : True
Sunday    : True
taskID    : 3
Saturday  : True
Friday    : True
Tuesday   : True
Monday    : True
Wednesday : True

PowerShell 输出它很奇怪,但它都是有组织的。您始终可以使用格式表$newCSV | Format-Table TaskID,Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday -AutoSize。Export-CSV 将保持所有列正确。

更新

您始终可以使用switch语句来编辑属性。换$props.$day = $true一个 good'ole switch 语句,如下所示:

foreach($DoW in $task.Schedules.Schedule.Days.DayOfWeek){
    foreach($day in $DoW){
        #$props.$day = $true
        switch ($day){
            'Sunday'{$props.Sunday = $true}
            'Monday'{$props.Monday = $true}
            'Tuesday'{$props.Tuesday = $true}
            'Wednesday'{$props.Wednesday = $true}
            'Thursday'{$props.Thursday = $true}
            'Friday'{$props.Friday = $true}
            'Saturday'{$props.Saturday = $true}
            default{"$day is not a Day of the Week"}
        }
    }
}
于 2013-04-02T16:34:02.570 回答
0

传统的智慧似乎是您应该将其转换为 [XML],但如果有数千个,我怀疑这将对 CPU 和内存造成相当沉重的负担。也许其他人会发布该解决方案以进行比较。

同时:

[regex]$regex = @'
(?ms)\s*<Task ID="(\d+)".+?
\s*<Days>(.+?)
\s*</Days>.+?
\s*</Task>
'@

$text = [IO.File]::ReadAllText('C:\tasks.xml')
&{
$regex.Matches($text) |
foreach {
 $Schedule = 
 new-object psobject -Property @{
                                 taskID = $_.groups[1].value
                                 mon    = 0
                                 tue    = 0
                                 wed    = 0
                                 thur   = 0
                                 fri    = 0
                                 sat    = 0
                                 sun    = 0
                                 }
    switch -Regex ($_.groups[2].value)
    {
     'Monday'    {$Schedule.mon  = 1}
     'Tuesday'   {$Schedule.tue  = 1}
     'Wednesday' {$Schedule.wed  = 1}
     'Thursday'  {$Schedule.thur = 1}
     'Friday'    {$Schedule.fri  = 1}
     'Saturday'  {$Schedule.sat  = 1}
     'Sunday'    {$Schedule.sun  = 1}
     }
   $schedule 
  }
}| select taskID,mon,tue,wed,thur,fri,sat,sun | ft -auto

taskID mon tue wed thur fri sat sun
------ --- --- --- ---- --- --- ---
1        1   0   0    1   1   1   0
2        1   0   0    0   1   0   0
3        1   1   1    1   1   1   1
于 2013-04-02T16:12:24.100 回答