send-mailmessage - Body missing when script run as a scheduled task

Odpovědět send-mailmessage - Body missing when script run as a scheduled task

  • 14. června 2012 16:42
     
      Obsahuje kód

    Hi I've encountered an issue that I have yet to find a resolution to. Firstly I'm a complete amateur when it comes to scripting so please dont criticise me too much.

    I have a script that runs against a SCVMM server that checks for virtual machines that have been added to the system in the last 7 days and 30 days. Outputs detailed info to a file and then sends an e-mail with the file as an attachment and a summary as the message body.

    When run interactivley the script works like a charm. However when run as a scheduled task everything works except parts of the body are missing.

    The script is below with the output directly below that. Firstly the output as it should look and secondly as it comes through.

    get-vmmserver localhost
    $body = @()
    $attachment = "C:\scripts\newguests\NewServers_$(get-date -f yyyy-MM-d).txt"
    
    $full = get-vm | where{$_.Addedtime -gt (get-date).AddDays(-30)} | ft Name, Cloud, Addedtime, Hostname, Location -auto  | out-file $attachment
      
    $brief7 = get-vm | where{$_.Addedtime -gt (get-date).AddDays(-7)} | ft Name, Cloud, Addedtime | Out-string
    $brief30 = get-vm | where{$_.Addedtime -gt (get-date).AddDays(-30)} | ft Name, Cloud, Addedtime | out-string
    
    $emailFrom = "e-mail"
    $emailTo = "e-mail"
    $subject = "New Servers Added - YOA-VMSS2012-01"
    $smtpServer = "172.16.4.86"
    $body += "Attached is the report for $(get-date -f d-MM-yyyy). Please ensure new servers are included in the backup schedule. New additions are summarised below."
    $body += " "
    $body += "Servers for the last 7 Days"
    $body += "--------------------------------------"
    $body += "$brief7"
    $body += ""
    $body += "Servers in Last 30 days"
    $body += "--------------------------------------"
    $body += "$Brief30"
    $body = $body | out-string
    
    send-mailmessage -from $emailfrom -to $emailto -smtpserver $smtpserver -subject $subject -body $body -attachments $attachment 

    Interactive Output:

    Attached is the report for 14-06-2012. Please ensure new servers are included in the backup schedule. New additions are summarised below.

    Servers for the last 7 Days

    --------------------------------------

    Servers in Last 30 days

    --------------------------------------

    Name                                    Cloud                                   AddedTime                             

    ----                                    -----                                   ---------                             

    HIVEDOME-PC05                           Hivedome                                31/05/2012 14:38:49                   

    HIVEDOME-PC03                           Hivedome                                31/05/2012 13:28:22                   

    HIVEDOME-PC06                           Hivedome                                31/05/2012 15:25:05                   

    HIVEDOME-PC10                           Hivedome                                31/05/2012 21:48:05                   

    HIVEDOME-PC12                           Hivedome                                31/05/2012 22:01:05                   

    HIVEDOME-PC02                           Hivedome                                31/05/2012 13:03:18                   

    HIVEDOME-PC04                           Hivedome                                31/05/2012 14:10:12                   

    HIVEDOME-PC01                           Hivedome                                30/05/2012 17:08:25                   

    HIVEDOME-PC09                           Hivedome                                31/05/2012 19:44:35                   

    HIVEDOME-PC07                           Hivedome                                31/05/2012 16:28:25                   

    HIVEDOME-PC08                           Hivedome                                31/05/2012 19:12:06                   

    HIVEDOME-PC13                           Hivedome                                31/05/2012 22:02:19                   

    HIVEDOME-PC11                           Hivedome                                31/05/2012 21:58:25  

    Scheduled Task Output:

    Attached is the report for 14-06-2012. Please ensure new servers are included in the backup schedule. New additions are summarised below.

    Servers for the last 7 Days

    --------------------------------------

    Servers in Last 30 days

    --------------------------------------

                     

     Thanks in advance.

    Paul             


    • Upravený paulmo 14. června 2012 16:47
    •  

Všechny reakce

  • 14. června 2012 16:55
     
     Navržená odpověď
    Is your script loading the module that get-vm is in?

    G. Samuel Hays

  • 14. června 2012 17:26
     
     

    Yep. Becuase the file attachement is populated correctly and it works fine standalone. The issue comes as soon as its run through task scheduler.

    Cheers

    Paul

  • 14. června 2012 17:48
     
     Odpovědět

    Right... so your interactive environment might be loading a module that is not loaded when PowerShell is kicked off in a script.

    In your interactive, run "get-module" and see what's loaded.

    In your script, add:

    import-module <whatever module is loaded>

    See if that fixes your problem.


    G. Samuel Hays

    • Označen jako odpověď paulmo 15. června 2012 8:13
    •  
  • 15. června 2012 5:12
    Moderátor
     
     

    Hi,

    I feel agree with above suggestions, it seems like that you should have the specific module loaded which contains the command get-vm.

    Please also make sure that the schedule task are running under an admin account that have permissions to read those information from SCVMM server.

    Regards,

    Yan Li


    Yan Li

    TechNet Community Support

  • 15. června 2012 8:15
     
     

    Hi guys,

    Thanks for the replies. I wasnt convinced that loading the modules in the script was necessary but this is what was required. I had to put the following as the first line:

         ipmo 'C:\Program Files\Microsoft System Center 2012\Virtual Machine Manager\bin\psmodules\virtualmachinemanager'

    Thanks

    Paul