RogerDingo
RogerDingo

Reputation: 13

how to email output from measure-command

I'm looking for a way to email the output from the measure-command command.

I have a script that runs the vmfs.unmap command and I would like to use measure-command to tell us how long it took and then email us the results once completed.

So far, I have:

Measure-Command {.\VMware_vmfsunmap.ps1}

which outputs:

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 11
Milliseconds      : 913
Ticks             : 119139896
TotalDays         : 0.000137893398148148
TotalHours        : 0.00330944155555556
TotalMinutes      : 0.198566493333333
TotalSeconds      : 11.9139896
TotalMilliseconds : 11913.9896

I can't figure out how to get the output into a email. I have tried piping this into the send-mailmessage but it didn't work.

I can output it into an txt file using Out-File but then I'm not sure how to add that text to the body of an email.

I tried adding in a variable that says

$body = c:\scripts\output.txt

then

Send-MailMessage -From "vmfsunmap" -To "ME" -Subject "test vmfs unmap email" -Body **$Body** -SmtpServer smtpserver.com

but this just opened the txt file and didn't attach it into the body.

Does anybody have any suggestions?

Upvotes: 0

Views: 620

Answers (1)

notjustme
notjustme

Reputation: 2494

I believe the Body-parameter has to be a string so try piping the output to Out-String and see if that works for ya.

Measure-Command {.\VMware_vmfsunmap.ps1} | Out-String

The difference could be verified with something like this;

(Measure-Command {.\VMware_vmfsunmap.ps1} | Out-String).GetType().FullName

Should output System.String

(Measure-Command {.\VMware_vmfsunmap.ps1}).GetType().FullName

Should output System.TimeSpan

Upvotes: 0

Related Questions