Public/Import/Remove-PublicFolderSMTP.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function Remove-PublicFolderSMTP {

    [CmdletBinding(SupportsShouldProcess = $true)]
    param (

        [Parameter(Mandatory = $true)]
        [string] $ReportPath,

        [Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
        $PF

    )
    Begin {

        $LogFileName = $(get-date -Format yyyy-MM-dd_HH-mm-ss)
        $ErrorLog = Join-Path $ReportPath ($LogFileName + "-PublicFolderSMTP-Error_Log.csv")

    }
    Process {
        ForEach ($CurPF in $PF) {
            try {

                $SetPFSplat = @{
                    Identity       = $CurPF.DisplayName
                    EmailAddresses = @{Remove = $CurPF.AddressOrMember}
                    ErrorAction    = 'Stop'
                }

                Set-MailPublicFolder @SetPFSplat
                Write-Host "Success: $($CurPF.Identity)`t$($CurPF.AddressOrMember)"
            }
            catch {
                Write-Host "Failed: $($CurPF.Identity)`t$($CurPF.AddressOrMember)"

                $Failure = $_.CategoryInfo.Reason

                [PSCustomObject]@{
                    Time               = (Get-Date).ToString("yyyy/MM/dd HH:mm:ss")
                    Result             = 'FAILURE'
                    Action             = 'REMOVING'
                    Object             = 'PFSMTP'
                    DisplayName        = $CurPF.DisplayName
                    PrimarySMTPAddress = $CurPF.PrimarySMTPAddress
                    Address            = $CurPF.AddressOrMember
                    FullNameError      = $_.Exception.GetType().fullname
                    Message            = $Failure
                    ExtendedMessage    = $_.Exception.Message

                } | Export-Csv -Path $ErrorLog -NoTypeInformation -Append
            }

            Start-Sleep -Seconds 2
        }
    }
    End {

    }
}

<#
 
 
$Pf = 'foo@apple.com'
 
$Domain = @(
    'pear.com', 'banana.com', 'parsley.com', 'beer.com', 'strawberry.com', 'luke.com'
    'orange.com', 'star.com', 'contoso.mail.onmicrosoft.com'
)
 
$DomainNoMS = @(
    'pear.com', 'banana.com', 'parsley.com', 'beer.com', 'strawberry.com', 'luke.com'
    'orange.com', 'star.com'
)
 
$Smtp = (Get-MailPublicFolder -Identity $Pf | Select -ExpandProperty EmailAddresses |
        Where-Object {
        ($_ -split "@")[1] -in $Domain
    })
 
$Primary = $Smtp | Where-Object {
    ($_ -clike "SMTP:*") -and
    ($_ -split "@")[1] -in $DomainNoMS
}
 
$OnMicrosoft = ($Smtp | Where-Object {
        -not ($_ -clike "SMTP:*") -and
        ($_ -split "@")[1] -match 'contoso.mail.onmicrosoft.com'
    }) -replace 'SMTP:', ''
 
$Remove = $Smtp.tolower() | Where-Object {
    -not ($_ -match 'contoso.mail.onmicrosoft.com')
}
 
if ($Primary -and $OnMicrosoft) {
    Set-MailPublicFolder -Identity $Pf -PrimarySmtpAddress $OnMicrosoft
}
 
if ($Remove) {
    Set-MailPublicFolder -Identity $Pf -EmailAddresses @{ Remove = $Remove }
}
 
 
 
#>