Powershell格式 - 包含新换行符的表格会在列

时间:2017-02-10 10:02:24

标签: windows powershell scheduled-tasks windows-server-2012-r2 powershell-v5.0

我在Windows中有2个预定任务:StartAppPool和StopAppPool。

在StartAppPool中,我只有一个作业触发器。在StopAppPool中,我有2个工作触发器。

我正在尝试创建一个可以显示我的计划任务和相关属性状态的脚本。

Write-Host "9. Checking Task Scheduler Execution Status...." -BackgroundColor DarkCyan
$taskService = New-Object -ComObject "Schedule.Service"
$taskService.Connect($env:COMPUTERNAME)
$rootTaskFolder = $taskService.GetFolder('\')
$tasks = $rootTaskFolder.GetTasks(1) | Where-Object { $_.Name -in 'StopAppPool','StartAppPool' }

$TaskArray = @()
ForEach ( $task in $tasks ) {
    $object = New-Object -TypeName PSObject
    $object | Add-Member -Name 'Name' -MemberType Noteproperty -Value $task.Name
    $object | Add-Member -Name 'TaskEnabled' -MemberType Noteproperty -Value $task.Enabled
    $object | Add-Member -Name 'LastRunTime' -MemberType Noteproperty -Value $task.LastRunTime
    $object | Add-Member -Name 'NextRunTime' -MemberType Noteproperty -Value $task.NextRunTime

    $TaskXMLObject = [xml]$task.Xml
    $CalendarTriggers = $TaskXMLObject.Task.Triggers.CalendarTrigger


    $TaskTriggerArray = @()
    ForEach ($CalendarTrigger in $CalendarTriggers) {
        $object2 = New-Object -TypeName PSObject
        $object2 | Add-Member -Name 'StartBoundary' -MemberType Noteproperty -Value ( Get-Date $CalendarTrigger.StartBoundary.Replace('T',' ') -Format "dd/MM/yyyy hh:mm:ss tt" )
        $object2 | Add-Member -Name 'Enabled' -MemberType Noteproperty -Value $CalendarTrigger.Enabled
        $DaysOfWeek = ( $CalendarTrigger.ScheduleByWeek.DaysOfWeek | Get-Member -MemberType Property | Select -ExpandProperty Name) | ForEach-Object -Process { [enum]::parse([System.DayOfWeek],$_ ) } | Sort-Object #parsing the values into an enum will allow the objects to be sorted by day instead of alphabetical order           
        $object2 | Add-Member -Name 'DaysOfWeek' -MemberType Noteproperty -Value ( $DaysOfWeek -join ', ' )

        $TaskTriggerArray += $object2
    }

    $object | Add-Member -Name 'StartBoundary' -MemberType NoteProperty -Value ($TaskTriggerArray.StartBoundary | Out-String )
    $object | Add-Member -Name 'TriggerEnabled' -MemberType Noteproperty -Value ($TaskTriggerArray.Enabled | Out-String )
    $object | Add-Member -Name 'DaysOfWeek' -MemberType Noteproperty -Value ($TaskTriggerArray.DaysOfWeek | Out-String )
    $TaskArray += $object

}

$TaskArray | Format-Table Name, TaskEnabled, LastRunTime, NextRunTime, @{Label='StartBoundary';Expression={(($_.StartBoundary )}}, TriggerEnabled, DaysOfWeek -Wrap

不幸的是,“StartBoundary”列似乎附加了额外的空格,我无法弄清楚如何解决这个问题。这是示例输出

Name         TaskEnabled LastRunTime          NextRunTime          StartBoundary                                    TriggerEnabled DaysOfWeek                                                
----         ----------- -----------          -----------          -------------                                    -------------- ----------                                                
StartAppPool        True 10/2/2017 6:00:00 AM 11/2/2017 6:00:00 AM 29/09/2016 06:00:00 AM                           true           Monday, Tuesday, Wednesday, Thursday, Friday, Saturday    

StopAppPool         True 10/2/2017 5:10:01 AM 11/2/2017 5:10:00 AM 29/09/2016 05:10:00 AM                           true           Tuesday, Wednesday, Thursday, Friday, Saturday            
                                                                   05/11/2016 10:00:00 AM                           true           Saturday                                                  

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试使用修剪方法,如下所示:

$_.StartBoundary.ToString().Trim()

您还可以尝试为格式表 cmdlet

添加 -AutoSize 开关