SOLVED

send powershell script output by email

Copper Contributor

Hi all,

i'm trying to send the output of a powershell script by mail.

here's the script : collect o365 unused licenses and send the result by mail

 

$licenses = Get-MsolAccountSku
foreach ($license in $licenses){
$UnusedUnits = $license.ActiveUnits - $license.ConsumedUnits
$output = "$($license.SkuPartNumber) has $unusedUnits unused licenses" | Out-String
}
Send-MailMessage -From sender@email.com -To receptient@email.com -Subject licenses_left -Body $output -SmtpServer 'smtp.office365.com' -Port '587' -UseSsl -Credential sender@email.com

 

it's working, i receive the mail except in the body i have only one line, the last result:

O365_BUSINESS  has -2 unused licenses

 

instead of something like :

TREAM has 999999 unused licenses
SPZA_IW has 9999 unused licenses
WINDOWS_STORE has 0 unused licenses
FLOW_FREE has 9829 unused licenses

O365_BUSINESS  has -2 unused licenses

 

anyone has an idea of what i'm doing wrong ?

 

thanks ll for your help

KR

Eli

 

10 Replies

Well you are overwriting your $output variable on each iteration. Instead, you should add to it, or use a list, or whichever other method you prefer.

@Vasil Michev 

 

thank you very much for your answer wich i m sorry is not obsvious for me.

i don't see how to add it instead.

i'm trying to keep this script simple without any file insertion.

 

thanks for your help

best response confirmed by eli_sorow (Copper Contributor)
Solution

You can do something like this:

 

$output = @()
$licenses = Get-MsolAccountSku

foreach ($license in $licenses){
$UnusedUnits = $license.ActiveUnits - $license.ConsumedUnits
$output += "$($license.SkuPartNumber) has $unusedUnits unused licenses"
}

@Vasil Michev 

 

Hi,

the send-mailMessage comand doesn't like the "+" parameter in $output +=

and when i remove it , i have got the same result (only one line)

 

 

 

You dont need to change that cmdlet, just replace the stuff I posted above.

Oh wait, seems I forgot the stringify part... Replace it with

($output | Out-String)

@Vasil Michev 

yes thx was already done but

because of the output syntaxt : $output + email is not sent

 

sendmailmessage.JPG

 

 

Hi

@eli_sorow , as @Vasil Michev  wrote, copy his code and in the Send-Message cmdlet you will need to make this small change

-Body ($output | Out-String) instead of -Body $output 

@farismalaeb @Vasil Michev 

it's working fine.

thank you both for your help and time, much appreciated!

 

KR

Eli

 

@eli_sorow 

This is good 

Please Click on @Vasil Michev answer and Mark it as Best Response.

Thanks

 

1 best response

Accepted Solutions
best response confirmed by eli_sorow (Copper Contributor)
Solution

You can do something like this:

 

$output = @()
$licenses = Get-MsolAccountSku

foreach ($license in $licenses){
$UnusedUnits = $license.ActiveUnits - $license.ConsumedUnits
$output += "$($license.SkuPartNumber) has $unusedUnits unused licenses"
}

View solution in original post