2

Variables $DATA10 and $DATA11 havd data already.

function email {
    $Dis = $DATA11|Where-Object As -EQ 'Gr'|Select-Object In
    $Dis.Count
    $Dis1 = $DATA10 |
            Where-Object As -EQ 'Gr'|
            Select-Object In
    $Dis1.Count
    if ($Dis.Count -gt 0) {
        $dis = (($Dis1.Count/$Dis.Count)*100)
        Write-Host "Percentag is:" + $dis
        Write-Host ""
    } else {
        Write-Host "No value"
    }
}

email
$email = email

When I try to get this output of this function above in the email body I get only the integer value and not the Write-Host content which shows in the output screen of the interpreter but not stored in the variable.

function global:Send-Email {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$False, Position=0)]
        [String]$Address = "guido@compperf.com",
        [Parameter(Mandatory=$False,Position=1)]
        [String]$Subject = "Swimming",
        [Parameter(Mandatory=$False)]
        $Body = $email
    )
    Begin {
        Clear-Host
        #Add-Type -assembly "Microsoft.Office.Interop.Outlook"
    }
    Process {
        # Create an instance Microsoft Outlook
        $Outlook = New-Object -ComObject Outlook.Application
        $Mail = $Outlook.CreateItem(0)
        $Mail.To = "$Address"
        $Mail.Subject = $Subject
        $Mail.Body = $Body
        $Mail.HTMLBody = $email
        #$File = "D:\CP\timetable.pdf"
        #$Mail.Attachments.Add($File)
        $Mail.Send()
    }
    End {
        # Section to prevent error message in Outlook
        $Outlook.Quit()
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
        $Outlook = $null
    }
}

Send-Email -Address vikram@hotmail.com -Body $email 

Output of the email body is

10 2

What I expect is like the one that displays in the ISE:

10

2

Percentag is: 10
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Rmm
  • 85
  • 7
  • Title has been changed @Ansgar Wiechers – Rmm May 13 '18 at 01:28
  • 1
    Please do not move the target. The mail body probably contains the text in a single line b/c the `$email` array is mangled into a (space-separated) string when setting `$Mail.Body`. For further help please post a new question with the code you currently have and a clear description of what result you expect and what result you actually get. – Ansgar Wiechers May 13 '18 at 18:23

1 Answers1

2

Write-Host does what its name says. It writes to the host console. It will not be output inside the application. Try using Write-Output instead.

Jacob Colvin
  • 2,625
  • 1
  • 17
  • 36
  • Thanks for your input..Yes using "write-output" exports my output.But it displays the output on a single line I want my output in different lines as it displays on the output console – Rmm May 13 '18 at 01:31
  • Use newline characters, i.e. "`nPercentag is:" @VikramJayapaal – Jacob Colvin May 13 '18 at 18:25