简体   繁体   English

Powershell cmdlet删除空格

[英]Powershell cmdlet removing whitespace

I've created a small cmdlet in for Powershell to be able to send an e-mail using exchange services. 我为Powershell创建了一个小cmdlet,使其能够使用交换服务发送电子邮件。 When I pass the body of the message in Powershell seems to be stripping all whitespace out of the string. 当我在Powershell中传递消息正文时,似乎正在从字符串中剥离所有空格。

Here is the cmdlet code: 这是cmdlet代码:

[Cmdlet(VerbsCommunications.Send, "ExchangeEmail")]
public class SendExchageEmailCommand : Cmdlet
{
    [Parameter(Mandatory=true)]
    public string ServerUri { get; set; }

    [Parameter(Mandatory=true)]
    public string Subject { get; set; }

    [ValidatePattern("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$")]
    [Parameter(Mandatory = true)]
    public string To { get; set; }

    [ValidatePattern("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$")]
    [Parameter]
    public string From { get; set; }

    [Parameter]
    public string Body { get; set; }

    protected override void ProcessRecord()
    {
        var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1) {Url = new Uri(ServerUri)};

        var mail = new EmailMessage(service) {Subject = Subject};

        if (!string.IsNullOrEmpty(From))
        {
            mail.From = From;
        }

        if (!string.IsNullOrEmpty(Body))
        {
            mail.Body = Body;
        }

        mail.ToRecipients.Add(To);

        mail.Send();
    }
}

And the PowerShell code looks like: PowerShell代码如下所示:

$subject = "Testing Reports"

$body = ""
$files = Get-ChildItem $ReportsFolder
foreach ($file in $files)
{
    $body += "`r`n"
    $body += $file.FullName
}

Send-ExchangeEmail -ServerUri $SMTPServerURI -Subject $subject -To $MessageTo -Body $body

Is there any way to ensure that whitespace is passed into a cmdlet (and am I even right that it is PS stripping the whitespace?) 有没有什么方法可以确保将空白传递到cmdlet中(我什至是说PS正在剥离空白?)

I don't think that PowerShell is removing whitespace. 我认为PowerShell无法删除空格。 I find it more likely that you're sending the e-mail body as if it was HTML, not plain text. 我发现您发送电子邮件正文的可能性更大,就好像它是HTML而不是纯文本一样。 I don't know what class EmailMessage is (since I don't know the namespace or anything), but in the MailMessage class there is a flag for IsBodyHtml, which you can set. 我不知道什么是EmailMessage类(因为我不知道名称空间或任何东西),但是在MailMessage类中有一个IsBodyHtml的标志,您可以设置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM