0

I am working on a monitoring report using PowerShell. The script pulls out server details, disk status and application status.

I am able to send the server details and disk status over mail in an HTML table. But reading the content of the file (created via batch file extracting the data from Oracle DB) and sending it over same mail in HTML format does not work.

Below is the data in the text file:

**program-id        sta pid        start time           stop time**

Clnt_dp           T   0          04/03/2017 08:00:03  04/03/2017 08:00:04
Clnt_ds           R   5140       04/03/2017 08:00:03  
Clnt_rpc          R   8572       04/03/2017 08:00:03  
bksrvr            R   4692       04/03/2017 07:59:03  
chkw              T   0          04/03/2017 07:59:03  04/03/2017 08:09:04
db                T   0          04/03/2017 07:59:01  04/03/2017 07:59:02
dsaux             R   8444       04/03/2017 07:59:03  
dssched           R   4792       04/03/2017 07:59:03  
dsshares          R   7672       04/03/2017 07:59:03  
ixmonSvc          R   5552       04/03/2017 07:59:03  
jbd               R   7536       04/03/2017 07:59:02  
migration         R   476        04/03/2017 07:59:03  
notifSrvr         R   1220       04/03/2017 07:59:03  
timestamp         R   8928       04/03/2017 07:59:02  

Can anyone please let me know how this could be achieved? Thanks.

1
  • 1
    What have you tried, and how has what you've tried failed? SO is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See How to Ask a Good Question. Commented Apr 4, 2017 at 12:04

2 Answers 2

0

You need to split the string at the fixed width intervals. Using your text file excerpt, to mimic Get-Content:

cls
$content = @'
**program-id        sta pid        start time           stop time**

Clnt_dp           T   0          04/03/2017 08:00:03  04/03/2017 08:00:04
Clnt_ds           R   5140       04/03/2017 08:00:03  
Clnt_rpc          R   8572       04/03/2017 08:00:03  
bksrvr            R   4692       04/03/2017 07:59:03  
chkw              T   0          04/03/2017 07:59:03  04/03/2017 08:09:04
db                T   0          04/03/2017 07:59:01  04/03/2017 07:59:02
dsaux             R   8444       04/03/2017 07:59:03  
dssched           R   4792       04/03/2017 07:59:03  
dsshares          R   7672       04/03/2017 07:59:03  
ixmonSvc          R   5552       04/03/2017 07:59:03  
jbd               R   7536       04/03/2017 07:59:02  
migration         R   476        04/03/2017 07:59:03  
notifSrvr         R   1220       04/03/2017 07:59:03  
timestamp         R   8928       04/03/2017 07:59:02 
'@
$content -split "`r*`n" |
  select -Skip 2 -Property  @{name='program-id';expression={$_.Substring(0 ,17).Trim()}}, #1st
                            @{name='sta';       expression={$_.Substring(18,4).Trim()}},  #2nd 
                            @{name='pid';       expression={$_.Substring(22,11).Trim()}}, #3rd
                            @{name='start time';expression={$_.Substring(33,19).Trim()}}, #4th
                            @{name='stop time'; expression={$_.Substring(54).Trim()}} |   #5th
  ConvertTo-Html

...and using Get-Content...

Get-Content -Path 'c:\path\to\file.txt' |
  select -Skip 2 -Property  @{name='program-id';expression={$_.Substring(0 ,17).Trim()}}, #1st
                            @{name='sta';       expression={$_.Substring(18,4).Trim()}},  #2nd 
                            @{name='pid';       expression={$_.Substring(22,11).Trim()}}, #3rd
                            @{name='start time';expression={$_.Substring(33,19).Trim()}}, #4th
                            @{name='stop time'; expression={$_.Substring(54).Trim()}} |   #5th
  ConvertTo-Html
Sign up to request clarification or add additional context in comments.

2 Comments

Hi TechSpud, Thanks. That worked!!! another thing I would like to check is the sta column I need to check if the value is T and then mark that complete row background color RED. I am looking into it but if you have something handy then would be really helpful. Thanks again!!! Cheers
I do, but it's a little more complex than the straightforward ConvertTo-Html. See my SO answers: stackoverflow.com/a/39486625/1368849 & stackoverflow.com/a/39530701/1368849. You could regex search & replace the html output, or similar, but you'd need to add back-references to the <tr> elements for matching <td>val</td> values.
0

Using this ConvertFrom-SourceTable cmdlet:

$content -split '[\r\n]+' | Foreach-Object {$_.Trim('*')} |
ConvertFrom-SourceTablel | Format-Table # or ... | ConvertTo-Html

program-id sta pid  start time          stop time
---------- --- ---  ----------          ---------
Clnt_dp    T   0    04/03/2017 08:00:03 04/03/2017 08:00:04
Clnt_ds    R   5140 04/03/2017 08:00:03
Clnt_rpc   R   8572 04/03/2017 08:00:03
bksrvr     R   4692 04/03/2017 07:59:03
chkw       T   0    04/03/2017 07:59:03 04/03/2017 08:09:04
db         T   0    04/03/2017 07:59:01 04/03/2017 07:59:02
dsaux      R   8444 04/03/2017 07:59:03
dssched    R   4792 04/03/2017 07:59:03
dsshares   R   7672 04/03/2017 07:59:03
ixmonSvc   R   5552 04/03/2017 07:59:03
jbd        R   7536 04/03/2017 07:59:02
migration  R   476  04/03/2017 07:59:03
notifSrvr  R   1220 04/03/2017 07:59:03
timestamp  R   8928 04/03/2017 07:59:02

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.