【问题标题】:Sending email through PowerShell [duplicate]通过 PowerShell 发送电子邮件 [重复]
【发布时间】:2018-05-14 02:24:13
【问题描述】:

变量$DATA10$DATA11 已经有数据了。

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

当我尝试在电子邮件正文中获取上面此函数的输出时,我只得到整数值,而不是 Write-Host 内容,它显示在解释器的输出屏幕中,但未存储在变量中。

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 

邮件正文的输出是

10 2

我所期望的与 ISE 中显示的一样:

10 2 百分比是:10

【问题讨论】:

  • 标题已更改@Ansgar Wiechers
  • 请不要移动目标。邮件正文可能包含一行 b/c 中的文本,当设置 $Mail.Body 时,$email 数组被修改为(空格分隔的)字符串。如需进一步帮助,请使用您当前拥有的代码发布一个新问题,并清楚说明您期望的结果和实际得到的结果。

标签: powershell


【解决方案1】:

Write-Host 就像它的名字所说的那样。它写入主机控制台。它不会在应用程序内部输出。尝试改用Write-Output

【讨论】:

  • 感谢您的输入..是的,使用“write-output”导出我的输出。但它在一行上显示输出我希望我的输出在输出控制台上显示在不同的行中跨度>
  • 使用换行符,即“`nPercentag is:” @VikramJayapaal
猜你喜欢
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
  • 2014-11-09
  • 2016-05-18
  • 2018-10-12
  • 1970-01-01
相关资源
最近更新 更多