SOLVED

send powershell script output by email

%3CLINGO-SUB%20id%3D%22lingo-sub-1931276%22%20slang%3D%22en-US%22%3Esend%20powershell%20script%20output%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1931276%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3Ei'm%20trying%20to%20send%20the%20output%20of%20a%20powershell%20script%20by%20mail.%3C%2FP%3E%3CP%3Ehere's%20the%20script%20%3A%20collect%20o365%20unused%20licenses%20and%20send%20the%20result%20by%20mail%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CEM%3E%24licenses%20%3D%20Get-MsolAccountSku%20%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eforeach%20(%24license%20in%20%24licenses)%7B%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%24UnusedUnits%20%3D%20%24license.ActiveUnits%20-%20%24license.ConsumedUnits%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%24output%20%3D%20%22%24(%24license.SkuPartNumber)%20has%20%24unusedUnits%20unused%20licenses%22%20%7C%20Out-String%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%7D%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3ESend-MailMessage%20-From%20sender%40email.com%20-To%20receptient%40email.com%20-Subject%20licenses_left%20-Body%20%24output%20-SmtpServer%20'smtp.office365.com'%20-Port%20'587'%20-UseSsl%20-Credential%20%3C%2FEM%3E%3CA%20href%3D%22mailto%3Asender%40email.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%3CEM%3Esender%40email.com%3C%2FEM%3E%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eit's%20working%2C%20i%20receive%20the%20mail%20except%20in%20the%20body%20i%20have%20only%20one%20line%2C%20the%20last%20result%3A%3C%2FP%3E%3CP%3EO365_BUSINESS%26nbsp%3B%20has%20-2%20unused%20licenses%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Einstead%20of%20something%20like%20%3A%3C%2FP%3E%3CP%3ETREAM%20has%20999999%20unused%20licenses%3CBR%20%2F%3ESPZA_IW%20has%209999%20unused%20licenses%3CBR%20%2F%3EWINDOWS_STORE%20has%200%20unused%20licenses%3CBR%20%2F%3EFLOW_FREE%20has%209829%20unused%20licenses%3C%2FP%3E%3CP%3EO365_BUSINESS%26nbsp%3B%20has%20-2%20unused%20licenses%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eanyone%20has%20an%20idea%20of%20what%20i'm%20doing%20wrong%20%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20ll%20for%20your%20help%3C%2FP%3E%3CP%3EKR%3C%2FP%3E%3CP%3EEli%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1931276%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1931630%22%20slang%3D%22en-US%22%3ERe%3A%20send%20powershell%20script%20output%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1931630%22%20slang%3D%22en-US%22%3E%3CP%3EWell%20you%20are%20overwriting%20your%20%3CEM%3E%24output%26nbsp%3B%3C%2FEM%3Evariable%20on%20each%20iteration.%20Instead%2C%20you%20should%20add%20to%20it%2C%20or%20use%20a%20list%2C%20or%20whichever%20other%20method%20you%20prefer.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1931675%22%20slang%3D%22en-US%22%3ERe%3A%20send%20powershell%20script%20output%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1931675%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F58%22%20target%3D%22_blank%22%3E%40Vasil%20Michev%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethank%20you%20very%20much%20for%20your%20answer%20wich%20i%20m%20sorry%20is%20not%20obsvious%20for%20me.%3C%2FP%3E%3CP%3Ei%20don't%20see%20how%20to%20add%20it%20instead.%3C%2FP%3E%3CP%3Ei'm%20trying%20to%20keep%20this%20script%20simple%20without%20any%20file%20insertion.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20for%20your%20help%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1933036%22%20slang%3D%22en-US%22%3ERe%3A%20send%20powershell%20script%20output%20by%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1933036%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20do%20something%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CEM%3E%24output%20%3D%26nbsp%3B%40()%3CBR%20%2F%3E%24licenses%20%3D%20Get-MsolAccountSku%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3Eforeach%20(%24license%20in%20%24licenses)%7B%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%24UnusedUnits%20%3D%20%24license.ActiveUnits%20-%20%24license.ConsumedUnits%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%24output%20%2B%3D%20%22%24(%24license.SkuPartNumber)%20has%20%24unusedUnits%20unused%20licenses%22%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional 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 (Occasional 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