AlertMailSMTPSecure.psm1

<#
.SYNOPSIS
Retrieves HPe ILO SMTP Secure Email Connection Setting on ILO 5.
.DESCRIPTION
Retrieves HPe ILO SMTP Secure Email Connection Setting on ILO 5 only.
Not Applicable to ILO 4 and lower; will return Not Attempted.
Returns an object of Hostname and AlertMailSMTPSecureEnabled.
Requires Credential Object generated from Get-Credential.
Requires Module HPERedfishCmdlets: https://www.powershellgallery.com/packages/HPERedfishCmdlets/1.0.0.2
Optionally sends a test email when complete.
.PARAMETER ILO
FQDN of ILO(s).
.PARAMETER Credential
PSCredential generated from Get-Credential.
.PARAMETER DisableCertificateAuthentication
If this switch parameter is present then server certificate authentication is disabled for the execution of this cmdlet. If not present it will execute
according to the global certificate authentication setting. The default is to authenticate server certificates. See Enable-HPERedfishCertificateAuthentication
and Disable-HPERedfishCertificateAuthentication to set the per PowerShell session default.
.PARAMETER TestEmail
Use if a post configuration test email is desired.
.OUTPUTS
PSCUSTOMOBJECT SupSkiFun.SMTPSecureEnabledInfo
.LINK
https://www.powershellgallery.com/packages/HPERedfishCmdlets/1.0.0.2
.EXAMPLE
Get AlertMailSMTPSecureEnabled setting for one host:
$creds = Get-Credential
Get-AlertMailSMTPSecure -ILO MyHost-ilo.example.com -Credential $creds
.EXAMPLE
Get AlertMailSMTPSecureEnabled setting for two hosts, returning object into a variable, sending a test email when complete:
$creds = Get-Credential
$MyVar = Get-AlertMailSMTPSecure -ILO Host01ilo , Host02ilo -Credential $creds -TestEmail
.NOTES
These functions were written as a "stop-gap" measure until HPEiLOCmdlets contains an update
to the Get/Set HPEilLOAlertMailSetting. The current version (2.0.0.1) of the HPEilLOAlertMailSetting Cmdlets
appears to use an ILO4 class, which doesn't contain a property for AlertMailSMTPSecure. This is a property
on ILO5 which can be read / altered via REST/Redish. Ideally this "function"-ality (ugh) will be
incorporated in a future HPe release, obviating the need for the functions in this module.
These functions were written and tested using HPERedfishCmdlets Version 1.0.0.2.
#>

function Get-AlertMailSMTPSecure
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string[]]$ILO,
        [Alias("Name")]

        [Parameter(Mandatory = $true)]
        [pscredential]$Credential,

        [Parameter(Mandatory = $false)]
        [switch]$DisableCertificateAuthentication,

        [Parameter(Mandatory = $false)]
        [switch]$TestEmail

    )

    Begin
    {
        if ($DisableCertificateAuthentication)
        {
            $b = Test-HPERedfishCertificateAuthentication
            if($b)
            {
                Disable-HPERedfishCertificateAuthentication
            }
        }
    }

    Process
    {
        foreach ($i in $ilo)
        {
            #Error Checking Anyone?
              $c = Connect-HPERedfish -Address $i -Credential $credential
            $m = Get-HPERedfishDataRaw -Odataid '/redfish/v1/managers/' -Session $c
            # Loop for more members?
            $md = Get-HPERedfishDataRaw -Odataid $m.Members.'@odata.id' -Session $c
            $iv = $md.FirmwareVersion.Split()[1]

            if($iv -ne "5")
            {
                $loopobj = [pscustomobject]@{
                    HostName = $i
                    AlertMailSMTPSecureEnabled = "Not attempted - ILO $iv detected"
                }
                $loopobj.PSObject.TypeNames.Insert(0,'SupSkiFun.SMTPSecureEnabledInfo')
                $loopobj
              }
            else
            {
                $ns = Get-HPERedfishDataRaw -Odataid $md.NetworkProtocol.'@odata.id' -Session $c
                $g = Get-HPERedfishDataRaw -Odataid $md.NetworkProtocol.'@odata.id' -Session $c
                $loopobj = [pscustomobject]@{
                    HostName = $i
                    AlertMailSMTPSecureEnabled = ($g.oem.hpe.AlertMailSMTPSecureEnabled).ToString().Trim()
                  }
                $loopobj.PSObject.TypeNames.Insert(0,'SupSkiFun.SMTPSecureEnabledInfo')
                $loopobj
            }

            if($TestEmail -and $iv -eq "5")
            {
                Invoke-HPERedfishAction -Odataid $ns.Oem.Hpe.Actions.'#HpeiLOManagerNetworkService.SendTestAlertMail'.target -Session $c |
                    Out-Null
            }

            Disconnect-HPERedfish -Session $c
        }
    }

    End
    {
        if($b)
        {
            Enable-HPERedfishCertificateAuthentication
        }
    }
}

<#
.SYNOPSIS
Enables or Disables HPe ILO SMTP Secure Email Connection Setting on ILO 5.
.DESCRIPTION
Enables or Disables HPe ILO SMTP Secure Email Connection Setting on ILO 5 only.
Not Applicable to ILO 4 and lower; will return Not Attempted. Future: Will need to modify for ILO 6.
Returns an object of Hostname, Result, and AlertMailSMTPSecureEnabled.
Requires Credential Object generated from Get-Credential.
Requires Module HPERedfishCmdlets: https://www.powershellgallery.com/packages/HPERedfishCmdlets/1.0.0.2
Optionally sends a test email when complete. If enabling secure, the mail relay must accept secure connections.
.PARAMETER ILO
FQDN of ILO(s).
.PARAMETER Credential
PSCredential generated from Get-Credential.
.PARAMETER State
State to set AlertMailSMTPSecureEnabled. Enabled or Disabled.
.PARAMETER DisableCertificateAuthentication
If this switch parameter is present then server certificate authentication is disabled for the execution of this cmdlet. If not present it will execute
according to the global certificate authentication setting. The default is to authenticate server certificates. See Enable-HPERedfishCertificateAuthentication
and Disable-HPERedfishCertificateAuthentication to set the per PowerShell session default.
.PARAMETER TestEmail
Use if a post configuration test email is desired.
.OUTPUTS
PSCUSTOMOBJECT SupSkiFun.SMTPSecureEnabledInfo
.LINK
https://www.powershellgallery.com/packages/HPERedfishCmdlets/1.0.0.2
.EXAMPLE
Set AlertMailSMTPSecureEnabled for one host to true (enabled):
$creds = Get-Credential
Set-AlertMailSMTPSecure -ILO MyHost-ilo.example.com -Credential $creds -State Enabled
.EXAMPLE
Set AlertMailSMTPSecureEnabled for two hosts to false (disabled), returning object into a variable, sending a test email when complete:
$creds = Get-Credential
$MyVar = Set-AlertMailSMTPSecure -ILO Host01ilo , Host02ilo -Credential $creds -State Disabled -TestEmail
.NOTES
These functions were written as a "stop-gap" measure until HPEiLOCmdlets contains an update
to the Get/Set HPEilLOAlertMailSetting. The current version (2.0.0.1) of the HPEilLOAlertMailSetting Cmdlets
appears to use an ILO4 class, which doesn't contain a property for AlertMailSMTPSecure. This is a property
on ILO5 which can be read / altered via REST/Redish. Ideally this "function"-ality (ugh) will be
incorporated in a future HPe release, obviating the need for the functions in this module.
These functions were written and tested using HPERedfishCmdlets Version 1.0.0.2.
#>

function Set-AlertMailSMTPSecure
{
    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact='high')]
    param
    (
        [Parameter(Mandatory = $true)]
        [string[]]$ILO,
        [Alias("Name")]

        [Parameter(Mandatory = $true)]
        [pscredential]$Credential,

        [Parameter(Mandatory = $true)]
        [ValidateSet("Enabled" , "Disabled")]
        [string]$State,

        [Parameter(Mandatory = $false)]
        [switch]$DisableCertificateAuthentication,

        [Parameter(Mandatory = $false)]
        [switch]$TestEmail
    )

    Begin
    {
        if($state -eq "Enabled")
        {
            $s = $true
        }
        elseif($state -eq "Disabled")
        {
            $s = $false
        }
        else
        {
            Write-Output "State to set undetermined. Terminating."
        }

        if ($DisableCertificateAuthentication)
        {
            $b = Test-HPERedfishCertificateAuthentication
            if($b)
            {
                Disable-HPERedfishCertificateAuthentication
            }
        }

        $z = @{"AlertMailSMTPSecureEnabled" = $s}
        $y = @{"Hpe" = $z}
        $x = @{"Oem" = $y }
    }

    Process
    {
        foreach ($i in $ilo)
        {
            #Error Checking At Some Point
            if($PSCmdlet.ShouldProcess("$i to $($state)"))
            {
                  $c = Connect-HPERedfish -Address $i -Credential $credential
                $m = Get-HPERedfishDataRaw -Odataid '/redfish/v1/managers/' -Session $c
                $md = Get-HPERedfishDataRaw -Odataid $m.Members.'@odata.id' -Session $c  # Loop for more members?
                $iv = $md.FirmwareVersion.Split()[1]

                if($iv -ne "5")
                {
                    $loopobj = [pscustomobject]@{
                        HostName = $i
                        Result = "Not Attempted; ILO $iv detected"
                        AlertMailSMTPSecureEnabled = "Only Valid for ILO 5"
                    }
                    $loopobj.PSObject.TypeNames.Insert(0,'SupSkiFun.SMTPSecureEnabledInfo')
                    $loopobj
                  }
                else
                {
                    $ns = Get-HPERedfishDataRaw -Odataid $md.NetworkProtocol.'@odata.id' -Session $c
                    $r = Set-HPERedfishData -Odataid $ns.'@odata.id' -Setting $x -Session $c
                    $g = Get-HPERedfishDataRaw -Odataid $md.NetworkProtocol.'@odata.id' -Session $c
                    $loopobj = [pscustomobject]@{
                        HostName = $i
                        Result = ($r.error.'@Message.ExtendedInfo').MessageId.ToString().Trim()
                        AlertMailSMTPSecureEnabled = ($g.oem.hpe.AlertMailSMTPSecureEnabled).ToString().Trim()
                      }
                    $loopobj.PSObject.TypeNames.Insert(0,'SupSkiFun.SMTPSecureEnabledInfo')
                    $loopobj
                }

                if($TestEmail -and $iv -eq "5")
                {
                    Invoke-HPERedfishAction -Odataid $ns.Oem.Hpe.Actions.'#HpeiLOManagerNetworkService.SendTestAlertMail'.target -Session $c |
                        Out-Null
                }

                Disconnect-HPERedfish -Session $c
            }
        }
    }

    End
    {
        if($b)
        {
            Enable-HPERedfishCertificateAuthentication
        }
    }
}