Powershell - 将多个-note属性输出到自定义对象的多行

时间:2017-10-16 08:43:41

标签: powershell backupexec

我创建了一个包含3个属性的自定义对象。属性中的数据从Backup Exec作业日志文件中提取。

$BackupServer = Get-Content C:\ITO\RB\ERRORS\STARTEND.TXT | Select-String -SimpleMatch  'Backup of "' 

$BackupStartTime =  Get-Content C:\ITO\RB\ERRORS\STARTEND.TXT | Select-String -SimpleMatch 'Backup started on'

 $BackupEndTime = Get-Content C:\ITO\RB\ERRORS\STARTEND.TXT  | Select-String -SimpleMatch  'Backup completed on'

然后我创建了一个具有自定义属性的客户对象

$props= [ordered]@{ ServerName = $backupserver
                    StartTime = $backupstarttime
                    EndTime = $BackupEndTime
                    Success = 'Did Backup Complete'
                    }
$obj = New-Object -TypeName psobject -Property $props 

$objectoutput = $obj | Select @{n='Server Name';e={$_.ServerName -split ','+"`n"}}, @{n='Start Time';e={$_.StartTime -split '.'+"`n"}},  @{n='End Time';e={$_.EndTime -split '.'+"`n"}}
$objectoutput 

数组中的数据是我想要的,所以到目前为止我很高兴。

Custom Object :

$objectoutput | Get-Member


   TypeName: Selected.System.Management.Automation.PSCustomObject

Name        MemberType   Definition                                                                                                                                                                                                                  
----        ----------   ----------                                                                                                                                                                                                                  
Equals      Method       bool Equals(System.Object obj)                                                                                                                                                                                              
GetHashCode Method       int GetHashCode()                                                                                                                                                                                                           
GetType     Method       type GetType()                                                                                                                                                                                                              
ToString    Method       string ToString()                                                                                                                                                                                                           
End Time    NoteProperty System.Object[] End Time=16/10/2017 at 06:04:38. 16/10/2017 at 06:04:57. 16/10/2017 at 06:05:11. 16/10/2017 at 06:05:42. 16/10/2017 at 06:08:31. 16/10/2017 at 06:09:03. 15/10/2017 at 20:07:30. 15/10/2017 at 20:09:49. ...
Server Name NoteProperty System.Object[] Server Name=server1.
SERVERVMWSUS SERVEROP5P01 SERVERLUDIP01 SERVERNEWMPLP04 SERVERURV2...
**Start Time**  NoteProperty System.Object[] Start Time=16/10/2017 at 06:03:08. 16/10/2017 at 06:04:55. 16/10/2017 at 06:05:07. 16/10/2017 at 06:05:36. 16/10/2017 at 06:06:19. 16/10/2017 at 06:09:00. 15/10/2017 at 20:02:36. 15/10/2017 at 20:08:23...

当我调用数组时,我得到了这个。数据元素在那里。然而,它似乎是一长串文字。

$objectoutput 
Server Name  Start Time  End Time                                                              
{ITOTFXSBK01.C, ITOTFXSBK01.EFISystemPartition, ITOTFXSBK01.BKUPEXEC, ITOTFXSB... 
{16/10/2017 at 06:03:08., 16/10/2017 at 06:04:55., 16/10/2017 at 06:05:07., 16... 
{16/10/2017 at 06:04:38., 16/10/2017 at 06:04:57., 16/10/2017 at 06:05:11., 16...

我想要的是将每个服务器放在一个新行上,有一个“空格”可用于拆分ServerName note属性,对于StartTime和EndTime,有一个“。”可以用来拆分。

在我的最终报告中,我使用此命令尝试输出我想要的但是它不好,我尝试了很多不同的组合'r'n和-split和-join等。

$rpt4+= Get-HtmlContentTable ($objectoutput  | Select @{n='Server Name';e={$_.ServerName -split '\s+'}})

我真的在努力解决这个问题。我能做的最好的事情如下: PICTUREHERE

示例数据

$backupserver = 
TELB2BDB01
TELCLUS01
TELADVDB01
TELSPDB01
TELSQL04

$BackupStarttime = 
16/10/2017 at 04:10:43.
16/10/2017 at 04:12:07.
16/10/2017 at 02:02:38.
16/10/2017 at 02:01:02.
16/10/2017 at 02:01:32.

$BackupEndTime = 
16/10/2017 at 04:11:51.
16/10/2017 at 04:13:14.
16/10/2017 at 02:06:17.
16/10/2017 at 02:02:10.
16/10/2017 at 02:02:52.

1 个答案:

答案 0 :(得分:1)

我认为您的主要问题是您使用-split运算符而不使用转义正则表达式命令(如点)。

以下是一些基于您的数据的工作示例:

$backupserver = "TELB2BDB01 TELCLUS01 TELADVDB01 TELSPDB01 TELSQL04"
$BackupStarttime = "16/10/2017 at 04:10:43. 16/10/2017 at 04:12:07. 16/10/2017 at 02:02:38. 16/10/2017 at 02:01:02. 16/10/2017 at 02:01:32."
$BackupEndTime = "16/10/2017 at 04:11:51. 16/10/2017 at 04:13:14. 16/10/2017 at 02:06:17. 16/10/2017 at 02:02:10. 16/10/2017 at 02:02:52."

$servers = $backupserver -split ' '
$startTimes = $BackupStarttime -split '\.'
$endTimes = $BackupEndTime -split '\.'

for ($i = 1; $i -lt $servers.Count; $i++)
{ 
    [PsCustomObject]@{
        ServerName = $servers[$i]
        StartTime = $startTimes[$i]
        EndTime = $endTimes[$i]
        Success = 'Did Backup Complete'
    }
}

<强>输出:

ServerName StartTime               EndTime                 Success            
---------- ---------               -------                 -------            
TELCLUS01   16/10/2017 at 04:12:07  16/10/2017 at 04:13:14 Did Backup Complete
TELADVDB01  16/10/2017 at 02:02:38  16/10/2017 at 02:06:17 Did Backup Complete
TELSPDB01   16/10/2017 at 02:01:02  16/10/2017 at 02:02:10 Did Backup Complete
TELSQL04    16/10/2017 at 02:01:32  16/10/2017 at 02:02:52 Did Backup Complete