script schedule from task scheduler not correctly sending the html formatted email RRS feed

  • Question

  • HI All,

    I am running a script using task scheduler. I fetch out some info and in html format and sending it in email body.

    When i run script using ISE, it went out and show the formatted email nicely. 

    But when using task scheduler, i am receiving the email but not with html data.

    Also the csv file that is generating via task scheduler is blank.

    $date_test = get-date
    $date = get-date -f "MM-dd-yyyy"
    $path = "D:\Asharma5\Scripts\vmware\backup_task"
    $data_path = "$path\data"
    $user = "ADS\VCReader"
    " $date_test - Script started successfully" | out-file $path\script_test.txt -Append
    #getting credentials
    if ( !(test-path -Path  "$path\VCRe@der.txt") )
         {    read-host -AsSecureString | Convertfrom-SecureString | out-file "$path\VCRe@der.txt" }
    $pass = gc "$path\VCRe@der.txt" | ConvertTo-SecureString
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
    $to = "XXXX"
    $from = "xxxx"
    $smtp = "xxxx"
    $subject = "Runing backup task report `(`>100 hours`) on $date `"NO action Needed`"  "
    $Header = @"
    TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
    TH {border-width: 1px; padding: 10px; border-style: solid; border-color: black; background-color: #6495ED;}
    TD {border-width: 1px; padding: 10px; border-style: solid; border-color: black;}
    TH { text-align: middle;}
    TD { text-align: middle;}
    $data = @()
    get-module -ListAvailable vmware* | import-module
    "Imported module successfully" | out-file $path\script_test.txt -Append
    $vcs = "amdc-npn-vcenter01"
    foreach ( $vc in $vcs )
                connect-viserver -Server $vc -Credential $cred -ErrorAction Stop
                " $date_test - connected $vc successfully" | out-file $path\script_test.txt -Append
                $tasks = get-task | where { $ -like "backup_vm*" -and $_.StartTime -lt $((get-date).addHours(-100)) } 
                foreach ( $task in $tasks ) { $duration = [math]::Round($($(get-date) - $task.StartTime).totalhours,2)
                                              $vm_data = get-vm $($
                                              $properties = [ordered]@{ VM = $
                                                                        TaskName = $
                                                                        'Duration(hours)' = $duration
                                                                        'VM provisioned Space(GB)' = [math]::Round($vm_data.ProvisionedSpaceGB,2)
                                                                        'VM Used Space(GB)' = [math]::Round($vm_data.UsedSpaceGB,2)
                                                                        State = $task.state                                                            
                                                                        VCenter = $vc                            
                                              $list = New-Object -TypeName psobject -Property $properties
                                              $data += $list                                          
                disconnect-viserver -server $vc -Confirm:$false
                " $date_test - disconnected $vc successfully" | out-file $path\script_test.txt -Append
    $data | export-csv "$($data_path)\task_report_$($date).csv" -NoTypeInformation
    $data = $data | ConvertTo-Html -Head $Header  | Out-String
    Send-MailMessage -BodyAsHtml -Body $data -to $to -From $from -SmtpServer $smtp -Subject $subject
    " $date_test - sent email successfully" | out-file $path\script_test.txt -Append

    Monday, December 2, 2019 5:11 AM

All replies

  • In a task you must catch and handle all errors. You are likely getting one or more errors and are not catching and reporting them.


    Monday, December 2, 2019 5:25 AM
  • same script i am running in ISE.

    Still i add try catch and see the results.

    Monday, December 2, 2019 5:31 AM
  • I got the error when i applied try catch.

    As per the error the way i am using the credentials is not supported when running in task scheduler.

    #getting credentials
    if ( !(test-path -Path  "$path\VCRe@der.txt") )
         {    read-host -AsSecureString | Convertfrom-SecureString | out-file "$path\VCRe@der.txt" }
    $pass = gc "$path\VCRe@der.txt" | ConvertTo-SecureString
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass
    connect-viserver -Server $vc -Credential $cred -ErrorAction Stop

    And it is working when i use this, but providing direct password is a security concern.

    connect-viserver -Server $vc -user ADS\xxxx -password "xxxxx" -ErrorAction Stop

    Monday, December 2, 2019 5:42 AM
  • Without the full error there is no way to understand what you are seeing.

    You also have numerous syntax, logic and programming errors.

    Try/Cat5ch requires you to use stop on CmdLets to get the correct errors.


    • Edited by jrv Monday, December 2, 2019 6:07 AM
    Monday, December 2, 2019 6:05 AM
  • Hi @jrv

    I got the issue actually, but still looking for answers.

    While encrypting password in a file, i am using my account. But while running from task scheduler i am using local system account. Due to this reason the decryption of the password file was not happening.

    $pass = gc "$path\VCRe@der.txt" | ConvertTo-SecureString
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user,$pass

    Any work around for this.

    Monday, December 2, 2019 8:46 AM
  • Simple - run the task under your account.


    Monday, December 2, 2019 8:48 AM
  • As my account password keep changing in every 10 hours, i cannot use my account for running task scheduler jobs.

    Tuesday, December 3, 2019 1:44 AM
  • Sorry but this is not a task scheduler forum. You will have to talk to your admins to find a solution.


    Tuesday, December 3, 2019 1:51 AM
  • You should also know that you do not need a password to define a task under your current account. Just specify to run only locally and the password will not be required and the credentials will work correctly.

    To learn how to use tasks post in the Windows Sever utilities forum.


    Tuesday, December 3, 2019 1:58 AM
  • please try the same and see the result. encryption and decryption won't work, because while decrypting local system will decrypt the file which was encrypted by me.
    Tuesday, December 3, 2019 3:26 AM
  • Please try to understand that the task should be run under your account.  The file can then be decrypted.


    Tuesday, December 3, 2019 3:31 AM