-
Notifications
You must be signed in to change notification settings - Fork 29
/
Use-NetMailConfig.ps1
75 lines (61 loc) · 2.24 KB
/
Use-NetMailConfig.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<#
.SYNOPSIS
Use .NET configuration to set defaults for Send-MailMessage.
.DESCRIPTION
The configuration system provides a place to set email defaults:
<system.net>
<mailSettings>
<smtp from="[email protected]" deliveryMethod="network">
<network host="mail.example.org" enableSsl="true" />
</smtp>
</mailSettings>
</system.net>
The values for Send-MailMessage's From, SmtpServer, and UseSsl will be
taken from whatever is set in the machine.config (or more localized config).
.FUNCTIONALITY
Configuration
.COMPONENT
System.Configuration
.LINK
Add-ScopeLevel.ps1
.LINK
Set-ParameterDefault.ps1
.LINK
Send-MailMessage
.EXAMPLE
Use-NetMailConfig.ps1
Sets Send-MailMessage defaults for From, SmtpServer, and UseSsl to
values from the ConfigurationManager.
#>
#Requires -Version 3
[CmdletBinding()][OutputType([void])] Param(
# The scope to create the defaults in.
[string] $Scope = 'Local',
# Indicates the defaults should not be visible to child scopes.
[switch] $Private
)
$Scope = Add-ScopeLevel.ps1 $Scope
$sv = if($Private) {@{Scope=$Scope;Option='Private'}} else {@{Scope=$Scope}}
if($PSVersionTable.PSEdition -eq 'Core')
{ # from .NET (Core), run from .NET Framework, which has a machine.config to parse
$values = Invoke-WindowsPowerShell.ps1 { Use-NetMailConfig.ps1; $PSDefaultParameterValues }
$values.Keys |ForEach-Object {Set-ParameterDefault.ps1 Send-MailMessage `
($_ -replace '\ASend-MailMessage:') ($values[$_]) @sv}
Set-Variable PSEmailServer ($values['Send-MailMessage:SmtpServer']) @sv
}
else
{
try{[void][Configuration.ConfigurationManager]}catch{Add-Type -as System.Configuration}
$smtp = [Configuration.ConfigurationManager]::GetSection('system.net/mailSettings/smtp')
if($smtp.From) { Set-ParameterDefault.ps1 Send-MailMessage From $smtp.From @sv }
if($smtp.DeliveryMethod -eq 'Network' -and $smtp.Network)
{
if($smtp.Network.Host) {Set-ParameterDefault.ps1 Send-MailMessage SmtpServer $smtp.Network.Host @sv}
if($smtp.Network.EnableSsl) {Set-ParameterDefault.ps1 Send-MailMessage UseSsl $smtp.Network.EnableSsl @sv}
}
else
{
Write-Verbose 'Delivery method is not configured as Network'
}
Set-Variable PSEmailServer $smtp.Network.Host @sv
}