VMware Cloud Community
dalo
Hot Shot
Hot Shot
Jump to solution

Format logfiles

I would send me the vmkwarning logs per email:

foreach ($vmhost in get-cluster cluster04 | get-vmhost){
 $log = get-log -key vmkwarning $vmhost
 $mailBody = $mailBody`
 + $log.entries
}
$mailBody

But then the log entries are all on one line. How could I format this nicer?

Thanks, Daniel

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try this

$mailbody = @()
foreach ($vmhost in get-cluster | get-vmhost){
	$log = get-log -key vmkernel $vmhost
	$log.Entries | %{
		$mailbody += ("`r`n" + $_)
	}
}

# Send report via email
$SmtpClient = New-Object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.host = <smtp-server>
$MailMessage.from = <from-address>
$MailMessage.To.add(<to-address>)
$MailMessage.Subject = "logs"
$MailMessage.body = $mailBody
$SmtpClient.Send($MailMessage)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
Harkamals
Contributor
Contributor
Jump to solution

Get-Log for all VMHosts

$mailbody = Get-VMHost | Get-Log -Key vmkwarning | % { $_.Entries }

Get-Log for all VMHosts in Cluster

$mailbody = Get-Cluster &lt;clustername&gt; | Get-VMHost | Get-Log -Key vmkwarning | % { $_.Entries }

dalo
Hot Shot
Hot Shot
Jump to solution

Thanks, this is a lot easier way. But the result is the same. If I generate a mail with the variable $mailbody as mailtext I loose the newlines.

Is there a way to remain them?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try this

$mailbody = @()
foreach ($vmhost in get-cluster | get-vmhost){
	$log = get-log -key vmkernel $vmhost
	$log.Entries | %{
		$mailbody += ("`r`n" + $_)
	}
}

# Send report via email
$SmtpClient = New-Object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.host = <smtp-server>
$MailMessage.from = <from-address>
$MailMessage.To.add(<to-address>)
$MailMessage.Subject = "logs"
$MailMessage.body = $mailBody
$SmtpClient.Send($MailMessage)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dalo
Hot Shot
Hot Shot
Jump to solution

Thank you, this one works as expected.

I modified it a bit:

$mailbody = @()
foreach ($vmhost in get-cluster | get-vmhost | sort-object){
	$log = get-log -key vmkwarning $vmhost
	if ($log.Entries -ne "") {
		$mailbody += ("`r`n" + $vmhost + ":")
	}
	$log.Entries | %{
		$mailbody += ("`r`n" + $_)
	}
}
...

0 Kudos
harkamal
Expert
Expert
Jump to solution

Hit Correct Answer for LucD --so others know its been answered

0 Kudos
dalo
Hot Shot
Hot Shot
Jump to solution

I did this already!

0 Kudos