PowerShell Mail Encoding

I am trying to send an email using the powershell function using the send-mailmessage command. I need to change the text encoding. The send-mailmessage cmdlet has an encoding parameter that uses the System.Text.Encoding class. Therefore, I should use something like this:

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

I would like to use -Encoding UTF8 . The Out-File cmdlet works as follows. How can I reproduce the behavior from the Out-File cmdlet?

This is my idea, but I find it a little indirect:

 [parameter()][ValidateSet("UTF8","Unicode","ASCII")][String]$Encoding 

With this, I would create an encoding accordingly.

 [System.Text.Encoding]::$Encoding 
+4
source share
1 answer

You can create a proxy function, change the Encoding parameter type to System.String and manipulate it in the Begin block. I have included this example in the PowerShell Proxy Extensions module.

 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 } } } 
+2
source

Source: https://habr.com/ru/post/1381465/


All Articles