email - PowerShell 邮件编码

标签 email powershell powershell-2.0

我正在尝试使用 cmdlet send-mailmessage 发送带有 powershell 功能的电子邮件。
我需要修改文本的编码。
cmdlet send-mailmessage 有一个参数“encoding”,它使用类 System.Text.Encoding .
所以我必须使用这样的东西:

Send-Mailmessage -Encoding ([System.Text.Encoding]::UTF8)

我想使用 -Encoding UTF8反而。 Out-File cmdlet 的工作方式如下。如何重现 Out-File cmdlet 的行为?

这是我的想法,但我觉得它有点间接:
[parameter()][ValidateSet("UTF8","Unicode","ASCII")][String]$Encoding

有了这个,我会相应地创建编码。
[System.Text.Encoding]::$Encoding

最佳答案

您可以创建一个代理函数,将 Encoding 参数的类型更改为 System.String 并在 Begin block 中对其进行操作。我在 PowerShell Proxy Extensions 中包含了这个示例模块。

function Send-MailMessage
{
    [CmdletBinding()]

    param(
        [Parameter(ValueFromPipeline=$true)]
        [Alias('PsPath')]
        [ValidateNotNullOrEmpty()]
        [System.String[]]
        ${Attachments},

        [ValidateNotNullOrEmpty()]
        [System.String[]]
        ${Bcc},

        [Parameter(Position=2)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        ${Body},

        [Alias('BAH')]
        [Switch]
        ${BodyAsHtml},

        [Parameter()]
        [ValidateSet('ASCII','UTF8','UTF7','UTF32','Unicode','BigEndianUnicode','Default','OEM')]
        [ValidateNotNullOrEmpty()]
        [Alias('BE')]
        [System.String]
        $Encoding,

        [ValidateNotNullOrEmpty()]
        [System.String[]]
        ${Cc},

        [Alias('DNO')]
        [ValidateNotNullOrEmpty()]
        [System.Net.Mail.DeliveryNotificationOptions]
        ${DeliveryNotificationOption},

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [System.String]
        ${From},

        [Parameter(Position=3)]
        [Alias('ComputerName')]
        [ValidateNotNullOrEmpty()]
        [System.String]
        ${SmtpServer},

        [ValidateNotNullOrEmpty()]
        [System.Net.Mail.MailPriority]
        ${Priority},

        [Parameter(Mandatory=$true, Position=1)]
        [Alias('sub')]
        [ValidateNotNullOrEmpty()]
        [System.String]
        ${Subject},

        [Parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [System.String[]]
        ${To},

        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]
        ${Credential},

        [Switch]
        ${UseSsl}
    )


    begin
    {
        try 
        {
            $outBuffer = $null
            if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
            {
                $PSBoundParameters['OutBuffer'] = 1
            }

            if ($PSCmdlet.MyInvocation.BoundParameters.ContainsKey('Encoding')) 
            {
                $null = $PSCmdlet.MyInvocation.BoundParameters.Remove('Encoding') 
                $newValue = & {
                    if ($Encoding -eq 'OEM') 
                    {
                        [System.Text.Encoding]::GetEncoding($Host.CurrentCulture.TextInfo.OEMCodePage)
                    }
                    else
                    {
                        [System.Text.Encoding]::$Encoding
                    }
                }

                $null = $PSCmdlet.MyInvocation.BoundParameters.Add('Encoding',$newValue)
            }


            $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Send-MailMessage', [System.Management.Automation.CommandTypes]::Cmdlet)
            $scriptCmd = {& $wrappedCmd @PSBoundParameters }
            $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
            $steppablePipeline.Begin($PSCmdlet)
        }
        catch 
        {
            throw
        }
    }

    process
    {
        try 
        {
            $steppablePipeline.Process($_)
        } 
        catch 
        {
            throw
        }
    }

    end
    {
        try 
        {
            $steppablePipeline.End()
        } 
        catch 
        {
            throw
        }
    }
}

关于email - PowerShell 邮件编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8150563/

相关文章:

powershell - 从for循环中调用Powershell函数

ios - 如何使用 UIActivityItemProvider 设置邮件主题

php - 回复发件人 - PHP 电子邮件

PowerShell - 格式卷内的 Var

database - 难以通过 Powershell 连接到 Excel 2007 密码电子表格

powershell - 脚本 block Powershell远程MSI安装

linux - 更改 Plesk 12 管理员电子邮件

mysql - 使用电子邮件输入数据库

PowerShell:过滤字符串列表

visual-studio-2010 - 无法通过 msbuild 获取 powershell 脚本来导入模块