Help with Placing output into Body of Email

I would like to convert my powershell scripts to place the output into the body of the email currently I am creating txt files and sending as attachments. I coded one script that only had one line and that worked my issue is have multi commands in the script that I need help with.

My working script

$Server = hostname 
$Subject = "Server Uptime Report for $Server"
$From = "no-reply@mynet.com"
$To = "systems-alert@mynet.com"
[string]$messagebody = ""

$logs = net statistics server

foreach ($log in $logs )
{
 $messagebody = $messagebody + $log + "`r`n"
}

$PSEmailServer = "InternalRelay.MYNET.COM"
Send-MailMessage -From $From -To $To -Subject $Subject -Body $messagebody -smtpserver $PSEmailServer

Now a script that has multiple commands

$Server = hostname 
$Subject = "Cluster Status for $Server"
$Body = "Open attachment for Cluster Status"
$From = "no-reply@mynet.com"
$To = "systems-alert@mynet.com"
$LogFolder = "c:\util\logs"
$LogFile = "cluster.txt"

remove-item -path $LogFolder\$LogFile -Force

get-cluster | out-file $LogFolder\$LogFile -append
get-cluster | get-clusternode | out-file $LogFolder\$LogFile -append
get-cluster | get-clusterresource | out-file $LogFolder\$LogFile -append
get-cluster | get-clustergroup | out-file $LogFolder\$LogFile -append
get-cluster | get-clusternetwork | out-file $LogFolder\$LogFile -append
get-cluster | get-clusternetworkinterface | out-file $LogFolder\$LogFile -append
get-cluster | get-clusterquorum | out-file $LogFolder\$LogFile -append


$PSEmailServer = "InternalRelay.MYNET.COM"
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -smtpserver $PSEmailServer -attachment "$LogFolder\$LogFile"

 

Any ideas is my first one good? Any suggestions and examples will be greatly appreciated

Thanks

Tom

 

Hi Tom,

Rather than outputting each of your get-cluster commands into separate files, could you not append output them into a variable $BigBody?

If you need the files, you could also use the tee-object to output to file, but also to the variable?

Or, do a get-content of the file, into your $BigBody variable?

$bigbody = get-content($LogFolder\$LogFile)

Send-MailMessage -From $From -To $To -Subject $Subject -Body $BigBody -smtpserver $PSEmailServer

Some ideas

Tom

If I got you right you dont need to write the output to a file first.

$NetStatistics = net statistics server

$Server = hostname
$Params = @{
Subject = “Server Uptime Report for $Server”
From = ‘no-reply@mynet.com’;
To = ‘systems-alert@mynet.com’
smtpserver = ‘InternalRelay.MYNET.COM
Body = $NetStatistics
}

Send-MailMessage @Params

$ClusterSummary = get-cluster | Out-String -Stream
$ClusterSummary += get-cluster | get-clusternode | Out-String -Stream
$ClusterSummary += get-cluster | get-clusterresource | Out-String -Stream
$ClusterSummary += get-cluster | get-clustergroup | Out-String -Stream
$ClusterSummary += get-cluster | get-clusternetwork | Out-String -Stream
$ClusterSummary += get-cluster | get-clusternetworkinterface | Out-String -Stream
$ClusterSummary += get-cluster | get-clusterquorum | Out-String -Stream

$Server = hostname
$Params = @{
Subject = “Cluster Status for $Server”
From = ‘no-reply@mynet.com’;
To = ‘systems-alert@mynet.com’
smtpserver = ‘InternalRelay.MYNET.COM
Body = $ClusterSummary
}

Send-MailMessage @Params

 

I had to add “” around Body = $NetStatistics

The emails is not formatted correctly looks like this

Server Statistics for \TGCS005-S1 Statistics since 11/16/2018 9:28:59 AM Sessions accepted 1 Sessions timed-out 0 Sessions errored-out 0 Kilobytes sent 0 Kilobytes received 0 Mean response time (msec) 0 System errors 0 Permission violations 17 Password violations 0 Files accessed 35478 Communication devices accessed 0 Print jobs spooled 0 Times buffers exhausted Big buffers 0 Request buffers 0 The command completed successfully.

Should look like this

Server Statistics for \TGCS016

Statistics since 11/14/2018 6:39:46 PM

Sessions accepted 0
Sessions timed-out 0
Sessions errored-out 0

Kilobytes sent 87
Kilobytes received 0

Mean response time (msec) 0

System errors 0
Permission violations 0
Password violations 0

Files accessed 8
Communication devices accessed 0
Print jobs spooled 0

any ideas?

 

 

 

 

 

$NetStatistics = net statistics server

$Server = hostname

$Params = @{
   Subject = "Server Uptime Report for $Server"
   Body = "$NetStatistics"
   From = "no-reply@tgcsnet.com"
   To = "systems-alert@tgcsnet.com"
   smtpserver = "InternalRelay.TGCSNET.COM"
}

Send-MailMessage @Params

Tom

 

the bigbody ran but did not format the output correctly either

 

looks like this

Name ---- TGCSNET-Cluster Name ID State ---- – ----- TGCS005-N1 2 Up TGCS005-N2 1 Up TGCS005-S1 3 Up Name State OwnerGroup ResourceType ---- ----- ---------- ------------ Cluster Disk 3 Online Cluster Group Physical Disk Cluster Disk 4 Online TGCSClusterFS Physical Disk Cluster Disk 6 Online TGCSClusterFS Physical Disk Cluster IP Address Online Cluster Group IP Address Cluster Name Online Cluster Group Network Name File Server (\TGCSClusterFS) Online TGCSClusterFS File Server IP Address 10.2.8.100 Online TGCSClusterFS IP Address TGCSClusterFS Online TGCSClusterFS Network Name Name OwnerNode State ---- --------- ----- Available Storage TGCS005-S1 Online Cluster Group TGCS005-N1 Online TGCSClusterFS TGCS005-N1 Online Name State ---- ----- Cluster Network 2 Up Name Node Network State ---- ---- ------- ----- TGCS005-N1 - Primary TGCS005-N1 Cluster Network 2 Up TGCS005-N2 - Primary TGCS005-N2 Cluster Network 2 Up TGCS005-S1 - Primary TGCS005-S1 Cluster Network 2 Up Cluster QuorumResource ------- -------------- TGCSNET-Cluster Cluster Disk 3

should look like this

Name

TGCS2016

Name ID State


TGCS002-2016 2 Up
TGCS020-2016 1 Up

Name State OwnerGroup ResourceType


Cluster Disk 2 Offline Cluster Group Physical Disk
Cluster IP Address Online Cluster Group IP Address
Cluster Name Online Cluster Group Network Name

Name OwnerNode State


Available Storage TGCS002-2016 Offline
Cluster Group TGCS020-2016 PartialOnline

Name State Metric Role


Cluster Network 1 Up 70384 ClusterAndClient

Name Node Network State


TGCS002-2016 - Ethernet0 TGCS002-2016 Cluster Network 1 Up
TGCS020-2016 - Ethernet0 TGCS020-2016 Cluster Network 1 Up

Cluster QuorumResource


TGCS2016 Cluster Disk 2

 

Thoughts?

 

 

 

I use System.Data.DataTable for this type of thing.

Here’s an example - There’s a lot of HTML formatting code missing when I post this

$Table = $null
$Table = New-Object System.Data.DataTable "Idera Dumps"
$Col1 = New-Object System.Data.DataColumn MachineName,([string])
$Col2 = New-Object System.Data.DataColumn Name,([string])
$Col3 = New-Object System.Data.DataColumn LastWriteTime,([string])
$Col4 = New-Object System.Data.DataColumn FullName,([string])
$Table.Columns.Add($Col1)
$Table.Columns.Add($Col2)
$Table.Columns.Add($Col3)
$Table.Columns.Add($Col4)

foreach($Dump in $Dumps) {
$Row = $Table.NewRow()
$Row.MachineName = $Dump.PSComputerName
$Row.Name = $Dump.Name
$Row.LastWriteTime = $Dump.LastWriteTime
$Row.FullName = $Dump.FullName
$Table.Rows.Add($row)
}

# Format HTML report
$a = "<style>"
$a = $a + "BODY{background-color:#FFFFFF;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px;}"
$style = $style + "TD{border: 1px solid black; padding: 5px;}"
$style = $style + "</style>"

$MsgBody = "<html>
<p>Hello DBA team,</p>
<p></p>
<p>$BodyNote</P>
<p><u></u></p>
<p>The following tickets have been created: $Tickets</p>
<body>$($Table | ConvertTo-Html -Property MachineName,Name,LastWriteTime,FullName -Head $style | Out-String)</body>
<p></p>
<p></p>
<p>Thank You,</p>
<p>The Team</p>
</html>
"

$MailMessage = @{
To = @("others@here.com","theboss@here.com","thebossesboss@here.com")
Cc ="them@here.com"
From = "some@here.com"
Subject = $Subject
Body = $MsgBody
Smtpserver = "email.com"
}

<!–more–>

 

Guys went I type $netstatistics on the console it display properly

 

PS C:\Util> $NetStatistics
Server Statistics for \TGCS005-S1

Statistics since 11/16/2018 9:28:59 AM

Sessions accepted 1
Sessions timed-out 0
Sessions errored-out 0

Kilobytes sent 0
Kilobytes received 0

Mean response time (msec) 0

System errors 0
Permission violations 17
Password violations 0

Files accessed 35478
Communication devices accessed 0
Print jobs spooled 0

Times buffers exhausted

Big buffers 0
Request buffers 0

The command completed successfully.

PS C:\Util>

 

 

If that is the case then why when I get the email I have no returns it is all one big mess

Thanks

 

 

 

Hmmm … ok, I didn’t try the “Send-MailMessage”-Part … you could try if

$NetStatistics = net statistics workstation | Out-String
works.

OLAF

That fixed the first one now we need to get the multi one working.

 

Thanks

Is it now just a case of appending and putting all of the outputs to-string?

$Body  = get-cluster | Out-String
$Body += get-cluster | get-clusternode | Out-String
$Body += get-cluster | get-clusterresource | Out-String
$Body += get-cluster | get-clustergroup | Out-String
$Body += get-cluster | get-clusternetwork | Out-String
$Body += get-cluster | get-clusternetworkinterface | Out-String
$Body += get-cluster | get-clusterquorum | Out-String

I’m interested into how this gets solved! :slight_smile:

 

Here is my current multi command script

$ClusterSummary = get-cluster | out-string -Stream
$ClusterSummary += get-cluster | get-clusternode | out-string -Stream
$ClusterSummary += get-cluster | get-clusterresource | out-string -Stream
$ClusterSummary += get-cluster | get-clustergroup | out-string -Stream
$ClusterSummary += get-cluster | get-clusternetwork | out-string -Stream
$ClusterSummary += get-cluster | get-clusternetworkinterface | out-string -Stream
$ClusterSummary += get-cluster | get-clusterquorum | out-string -Stream

$Server = hostname

$Params = @{
   Subject = "Cluster Status for $Server"
   Body = "$ClusterSummary"
   From = "no-reply@mynet.com"
   To = "systems-alert@mynet.com"
   smtpserver = "InternalRelay.MYNET.COM"
}

Send-MailMessage @Params

The email shows

Name ---- TGCSNET-Cluster Name ID State ---- – ----- TGCS005-N1 2 Up TGCS005-N2 1 Up TGCS005-S1 3 Up Name State OwnerGroup ResourceType ---- ----- ---------- ------------ Cluster Disk 3 Online Cluster Group Physical Disk Cluster Disk 4 Online TGCSClusterFS Physical Disk Cluster Disk 6 Online TGCSClusterFS Physical Disk Cluster IP Address Online Cluster Group IP Address Cluster Name Online Cluster Group Network Name File Server (\TGCSClusterFS) Online TGCSClusterFS File Server IP Address 10.2.8.100 Online TGCSClusterFS IP Address TGCSClusterFS Online TGCSClusterFS Network Name Name OwnerNode State ---- --------- ----- Available Storage TGCS005-S1 Online Cluster Group TGCS005-N1 Online TGCSClusterFS TGCS005-N1 Online Name State ---- ----- Cluster Network 2 Up Name Node Network State ---- ---- ------- ----- TGCS005-N1 - Primary TGCS005-N1 Cluster Network 2 Up TGCS005-N2 - Primary TGCS005-N2 Cluster Network 2 Up TGCS005-S1 - Primary TGCS005-S1 Cluster Network 2 Up Cluster QuorumResource ------- -------------- TGCSNET-Cluster Cluster Disk 3

From the console $ClusterSummary

 

PS C:\Util> $ClusterSummary

Name

TGCSNET-Cluster

Name ID State


TGCS005-N1 2 Up
TGCS005-N2 1 Up
TGCS005-S1 3 Up

Name State OwnerGroup ResourceType


Cluster Disk 3 Online Cluster Group Physical Disk
Cluster Disk 4 Online TGCSClusterFS Physical Disk
Cluster Disk 6 Online TGCSClusterFS Physical Disk
Cluster IP Address Online Cluster Group IP Address
Cluster Name Online Cluster Group Network Name
File Server (\TGCSClusterFS) Online TGCSClusterFS File Server
IP Address 10.2.8.100 Online TGCSClusterFS IP Address
TGCSClusterFS Online TGCSClusterFS Network Name

Name OwnerNode State


Available Storage TGCS005-S1 Online
Cluster Group TGCS005-N1 Online
TGCSClusterFS TGCS005-N1 Online

Name State


Cluster Network 2 Up

Name Node Network State


TGCS005-N1 - Primary TGCS005-N1 Cluster Network 2 Up
TGCS005-N2 - Primary TGCS005-N2 Cluster Network 2 Up
TGCS005-S1 - Primary TGCS005-S1 Cluster Network 2 Up

Cluster QuorumResource


TGCSNET-Cluster Cluster Disk 3

PS C:\Util>

 

 

Still looks formatted correctly except when I get the email

 

Any ideas ?

 

Thanks Tom

 

 

 

Tom

removing the -Stream worked

 

Thanks

 

Now off to my other scripts.