none
Send html mail with result of powershell RRS feed

  • Question

  • Hi,


    I'd like to send myself a simple report of App-V packages deployed to a server. Therefore I'd like to use an existing function Get-AppVPackagesDeployed. However, I can't add the result in an (html) mail.

    I know my error is in

    $body += $AppVpackagesDeployedFormat

    but not sure how to set this correctly.

    Please advise.
    J.


    Function Get-AppVPackagesDeployed
    {
         $AppVpackagesDeployed = Invoke-Command -ScriptBlock { Get-AppvClientPackage * -all } -ComputerName OurServers
         $AppVpackagesDeployedFormat = $AppVpackagesDeployed | Select-Object -Property PSComputerName,Name,InUse | Sort-Object -Property Name     
    }
    
    Get-AppVPackagesDeployed
    
    $body += "Get-AppVPackagesDeployed executed by $env:username on $CurrentDate"
    $body += $AppVpackagesDeployedFormat
    $body = $body | out-string
    Send-MailMessage -From NoReply@ourdomain.com -To myaccount -SmtpServer oursmtpserver -BodyAsHtml -Subject "AppVPackages" -Body $Body



    Jan Hoedt

    Thursday, November 12, 2015 3:33 PM

Answers

  • At first glance, this appears to be a scope problem. Your variable, $AppVpackagesDeployedFormat, doesn't exist when the function has ended. You either need to put your email code inside the function, or change the scope of your variable ($Global:AppVpackagesDeployedFormat). I'd probably avoid using a global variables, if you can, but there's been times, in my experience, where it has been necessary.


    • Edited by tommymaynard Thursday, November 12, 2015 3:40 PM
    • Proposed as answer by SitrucHtims Thursday, November 12, 2015 3:45 PM
    • Marked as answer by janhoedt Friday, November 13, 2015 10:43 AM
    Thursday, November 12, 2015 3:38 PM
  • At first glance, this appears to be a scope problem. Your variable, $AppVpackagesDeployedFormat, doesn't exist when the function has ended. You either need to put your email code inside the function, or change the scope of your variable ($Global:AppVpackagesDeployedFormat). I'd probably avoid using a global variables, if you can, but there's been times, in my experience, where it has been necessary.


    Another option is to return your data from the function in the for of a pscustomobject and then use that object with the rest of your script.
    • Marked as answer by janhoedt Friday, November 13, 2015 10:43 AM
    Thursday, November 12, 2015 3:45 PM
    • Marked as answer by janhoedt Monday, November 16, 2015 12:40 PM
    Friday, November 13, 2015 1:02 PM

All replies

  • At first glance, this appears to be a scope problem. Your variable, $AppVpackagesDeployedFormat, doesn't exist when the function has ended. You either need to put your email code inside the function, or change the scope of your variable ($Global:AppVpackagesDeployedFormat). I'd probably avoid using a global variables, if you can, but there's been times, in my experience, where it has been necessary.


    • Edited by tommymaynard Thursday, November 12, 2015 3:40 PM
    • Proposed as answer by SitrucHtims Thursday, November 12, 2015 3:45 PM
    • Marked as answer by janhoedt Friday, November 13, 2015 10:43 AM
    Thursday, November 12, 2015 3:38 PM
  • At first glance, this appears to be a scope problem. Your variable, $AppVpackagesDeployedFormat, doesn't exist when the function has ended. You either need to put your email code inside the function, or change the scope of your variable ($Global:AppVpackagesDeployedFormat). I'd probably avoid using a global variables, if you can, but there's been times, in my experience, where it has been necessary.


    Another option is to return your data from the function in the for of a pscustomobject and then use that object with the rest of your script.
    • Marked as answer by janhoedt Friday, November 13, 2015 10:43 AM
    Thursday, November 12, 2015 3:45 PM
  • Thanks, that worked indeed but now everything is scrambled. How do I get a neat layout?

    Jan Hoedt

    Thursday, November 12, 2015 3:58 PM
  • We can't actually see your screen, so no one here, but you can say for certain what "scrambled" means. Based on that, all I can suggest is that you try it without piping $body to Out-String and/or using the -BodyAsHtml parameter. Those two thing could account for things not appearing as you would expect.
    Thursday, November 12, 2015 4:30 PM
  • Thanks!
    Only thing now is that a columns name is shortened with ...

    Server  App-V Package       In Use
    ------  -------------       -------
    Server1 Microsoft Off...    Yes
    Server2 7-Zip                  False

    Please advise.
    J.


    Jan Hoedt

    Friday, November 13, 2015 12:20 PM
    • Marked as answer by janhoedt Monday, November 16, 2015 12:40 PM
    Friday, November 13, 2015 1:02 PM
  • Thanks, the -autosize works indeed. Now I need a way to figure out howto get the columns and rows nicely:



    Jan Hoedt

    Monday, November 16, 2015 12:40 PM