1

我创建了一个具有 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 注释属性,对于 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.
4

1 回答 1

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
于 2017-10-16T09:35:28.077 回答