1

I have created a custom object with 3 properties. The data in the properties is pulled from Backup Exec job log files.

$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'

I then created a customer object with custom properties

$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 

The data in the array is what i want so I am happy so far.

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...

When I call the array I get this. The data elements is in there. However it appears to be one long string of text.

$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...

What i want is to have each server on a new line, there is a "space" that can be used to split the ServerName note property, for the StartTime and EndTime there is a "." that can be used to split.

In my final report I use this command to attempt to output how i want but its no good, ive tried lots of different combinations 'r 'n and -split and -join etc.

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

I am really struggling to work it out. The best i can do can be seen below: PICTUREHERE

Sample Data

$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.
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172

1 Answers1

1

I think your main issue is that you are using the -split operator without escaping regular expression commands (like the dot).

Here is some working example based on your data:

$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'
    }
}

Output:

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
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Thank you very much. This has pretty much sorted my issue. I have a few formatting issues (extra spaces on lines) but I should be able to work that out. – Richard Bailey Oct 16 '17 at 09:53
  • Can you explain what this line does for future reference : for ($i = 1; $i -lt $servers.Count; $i++) I understand the concept, but why count? – Richard Bailey Oct 16 '17 at 09:54
  • 1
    Since these are simple splits you could just avoid regex altogether as well. `$backupserver.Split()` and `$BackupStarttime.split('.')` would fit nicely here. – Matt Oct 16 '17 at 10:39
  • This is just a simple for loop. `$servers.Count` returns the number of servers there. @Matt Good point, I tend to always use `-split` but when you don't use regex and even have to escape the special characters, `.split()` is probably the better choice. – Martin Brandl Oct 16 '17 at 11:21