locked
Save Powershell Function to variable then email using Send-MailMessage. RRS feed

  • Question

  • Hi there all.

    Thank you for taking the time to look at this question.

    In simple terms, I have a function that gathers license information from office 365. I am trying to pass this to a variable ($body) so I can use Send-MailMessage to send by SMTP mail. All the parts work individually but I am not managing to pass the contents of my function to the variable. 

    I am using $Body = Write-Output @(MyCode) and my variable is null.

    Function MyCode
        {
    
        $licenses = get-msolaccountsku
    
            foreach ($license in $licenses)
                {
                    $line = "{0,-35} {1,18} {2,18}" -f $license.accountskuID, "($($license.activeunits) active)", "($($license.consumedunits) used)" 
                    write-host $line -foregroundcolor green
      
                    foreach ($sublicense in $license.servicestatus)
    	                {
        	                write-host  "   $($sublicense.serviceplan.servicename)" -foregroundcolor yellow
                        }
                }
    
        }
    
    $Body = Write-Output @(MyCode)
    
    $userid = "admin@contoso.com"
    $From = "admin@contoso.com"
    $To = "manager@contoso.com"
    $Subject = "SMTP Email to manager"
    $SMTPServer = "smtp.office365.com"
    $SMTPPort = "587"
    Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
    -Body ($Body | Out-String) -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
    -Credential (Get-Credential $userid)

    Would someone mind pointing me in the right direction so I can output the function as a string, save it to my variable called $Body so it can be sent off using Send-MailMessage.

    Thanks

    Saturday, February 10, 2018 9:50 PM

Answers

  • This will give you output but it will not be colored.

    Function MyCode{
        $licenses = get-msolaccountsku
        foreach ($license in $licenses){
            "{0,-35} {1,18} {2,18}" -f $license.accountskuID, "($($license.activeunits) active)", "($($license.consumedunits) used)" 
            foreach ($sublicense in $license.servicestatus){
                "   $($sublicense.serviceplan.servicename)"
            }
        }
    }
    
    $mailprops = @{
        From        = 'admin@contoso.com'
        To          = 'manager@contoso.com'
        Subject     = 'SMTP Email to manager'
        SMTPServer  = 'smtp.office365.com'
        SMTPPort    = 587
        UseSSL      = $true
        Credential  = Get-Credential 'admin@contoso.com'
    }
    
    $body = MyCode | Out-String
    Send-MailMessage -Body $body @mailprops
    


    \_(ツ)_/


    • Edited by jrv Saturday, February 10, 2018 10:31 PM
    • Marked as answer by AlanPs1 Saturday, February 10, 2018 10:39 PM
    Saturday, February 10, 2018 10:31 PM

All replies

  • Why would you want to mail code?  What is the purpose of this.  Are you trying to share your code with someone.  Just save the code to a text fiel and mail the text file.

    Here is the best way to use "Send-MailMessage"

    $mailprops = @{
        From        = 'admin@contoso.com'
        To          = 'manager@contoso.com'
        Subject     = 'SMTP Email to manager'
        SMTPServer  = 'smtp.office365.com'
        SMTPPort    = 587
        UseSSL      = $true
        Credential  = Get-Credential 'admin@contoso.com'
    }
    
    $body = MyCode | Out-String
    Send-MailMessage -Body $body @mailprops

    You cannot use "Write-Host" to output to a variable"

    This works"

    $a = 'hello'

    This won't work:

    $a = Write-Host hello


    \_(ツ)_/


    • Edited by jrv Saturday, February 10, 2018 10:31 PM
    Saturday, February 10, 2018 10:26 PM
  • This will give you output but it will not be colored.

    Function MyCode{
        $licenses = get-msolaccountsku
        foreach ($license in $licenses){
            "{0,-35} {1,18} {2,18}" -f $license.accountskuID, "($($license.activeunits) active)", "($($license.consumedunits) used)" 
            foreach ($sublicense in $license.servicestatus){
                "   $($sublicense.serviceplan.servicename)"
            }
        }
    }
    
    $mailprops = @{
        From        = 'admin@contoso.com'
        To          = 'manager@contoso.com'
        Subject     = 'SMTP Email to manager'
        SMTPServer  = 'smtp.office365.com'
        SMTPPort    = 587
        UseSSL      = $true
        Credential  = Get-Credential 'admin@contoso.com'
    }
    
    $body = MyCode | Out-String
    Send-MailMessage -Body $body @mailprops
    


    \_(ツ)_/


    • Edited by jrv Saturday, February 10, 2018 10:31 PM
    • Marked as answer by AlanPs1 Saturday, February 10, 2018 10:39 PM
    Saturday, February 10, 2018 10:31 PM
  • Hi JRV.

    Function MyCode
        {
    
        $licenses = get-msolaccountsku
    
            foreach ($license in $licenses)
                {
                    $line = "{0,-35} {1,18} {2,18}" -f $license.accountskuID, "($($license.activeunits) active)", "($($license.consumedunits) used)" 
                    write-host $line -foregroundcolor green
      
                    foreach ($sublicense in $license.servicestatus)
    	                {
        	                write-host  "   $($sublicense.serviceplan.servicename)" -foregroundcolor yellow
                        }
                }
    
        }

    The above outputs the following in my powershell:

    contoso.com:STANDARDPACK                 (3 active)           (3 used)
       BPOS_S_TODO_1
       FORMS_PLAN_E1
       STREAM_O365_E1
       Deskless
       FLOW_O365_P1
       POWERAPPS_O365_P1
       TEAMS1
       SHAREPOINTWAC
       PROJECTWORKMANAGEMENT
       SWAY
       INTUNE_O365
       YAMMER_ENTERPRISE
       MCOSTANDARD
       SHAREPOINTSTANDARD
       EXCHANGE_S_STANDARD
    contoso.com:POWER_BI_STANDARD            (1 active)           (1 used)
       EXCHANGE_S_FOUNDATION
       BI_AZURE_P0


    It is this output that I am trying to pass to a variable so I can send by email. I have the email using Send-MailMessage working but not with the desired output. 

    Thank you for the best way to send to email also I will revise my code to that but any way of getting the above outputting to the body of my email would be helpful.

    I am doing this just in a way to communicate the license quotas of a 365 tenant to senior members.

    Thanks.


    Saturday, February 10, 2018 10:35 PM
  • Thank you JRV

    I had to make one small adjustment below from your code but it has now sent the code.

    The colors weren't really needed I was just using them for on screen demo purposes.

    Marked as answer - Thanks so much for your assistance. 

    $mailprops = @{
        From        = 'admin@contoso.com'
        To          = 'manager@contoso.com'
        Subject     = 'SMTP Email to manager'
        SMTPServer  = 'smtp.office365.com'
        Port        = '587'
        UseSSL      = $true
        Credential  = Get-Credential 'alan@awtechservices.co.uk'
    }


    Saturday, February 10, 2018 10:43 PM
  • You are not trying to send "code".  You are sending text generated by your function.

    "Port" is an integer and does not need to be quoted.

    Now that you see how guessing can get you confused try the following link to help you avoid guessing.

    Learn PowerShell  


    \_(ツ)_/

    Saturday, February 10, 2018 10:51 PM