电子邮件 – PowerShell邮件编码

我正在尝试使用cmdlet send-mailmessage发送带有Power
Shell函数的电子邮件.

我需要修改文本的编码.

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块中对其进行操作.我在
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
        }
    }
}
点赞