Public/Import-MDSSkypeOnprem.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Function Import-MDSSkypeOnPrem {
    <#
    .SYNOPSIS
    Import the Skype On-Premises PowerShell cmdlets using a MDSCredential

    .DESCRIPTION
    Import the Skype On-Premises PowerShell cmdlets using a MDSCredential

    .EXAMPLE
    Import-MDSSkypeOnPrem -MDSCredential MyCred1

    Import the Skype On-Premises cmdlets with the stored 'MyCred1' credentials. The stored credential username should be a UPN.

    .EXAMPLE
    Import-MDSSkypeOnPrem -MDSCredential MyCred1 -Prefix OnPrem

    Import the Skype On-Premises cmdlets with the stored 'MyCred1' credentials and prefix the cmdlets. For example Get-CsUser becomes Get-OnPremCsUser. This allows you to load both the Skype Online cmdlets and Exchange cmdlets in the same session.

    .NOTES

    #>

    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingPlainTextForPassword','')]
    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUsePSCredentialType','')]

    [CmdletBinding(DefaultParameterSetName = 'Credential')]
    Param(
        [parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'MDSCredential'
        )]
        [String]$MDSCredential,

        [parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'Credential'
        )]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.CredentialAttribute()]
        $Credential,

        [parameter(
            Position = 1,
            Mandatory = $false,
            ParameterSetName = 'Credential'
        )]
        [parameter(ParameterSetName = 'Credential')]
        [string]$ServerName,

        [parameter(
            Position = 2,
            Mandatory = $false,
            ParameterSetName = 'Credential'
        )]
        [parameter(ParameterSetName = 'Credential')]
        [string]$Prefix
    )

    Begin {
        $SessionName = 'Microsoft.Skype'
        If (Get-PSSession -Name $SessionName -ErrorAction SilentlyContinue) {
            Try {
                Remove-PSSession -Name $SessionName -ErrorAction Stop
                Write-Verbose "Session $($SessionName) removed"
            }
            Catch {
                $null
            }
        }
    }
    Process {
        Try {
            # MDSCredentials
            If ($PSBoundParameters.MDSCredential) {
                $Credential = Get-MDSCredential -Name $MDSCredential -ErrorAction Stop
            }

            # Use the configuration file if a servername was not specified
            If (-not $PSBoundParameters.ServerName) {
                $Setting = 'SkypeOnPremServer'
                Try {
                    $ServerName = Get-MDSConfiguration -Setting $Setting -ErrorAction Stop
                }
                Catch {
                    Throw "A server name was not specified. Use the -ServerName parameter or configure the $Setting setting with Set-MDSConfiguration."
                }
            }

            # New-PSSession
            $SessionParameters = @{
                Name          = $SessionName
                ConnectionUri = "https://$($ServerName)/ocspowershell"
                Credential    = $Credential
                ErrorAction   = 'Stop'
            }
            $Session = New-PSSession @SessionParameters

            # Import-PSSession
            $PSSessionParameters = @{
                Session             = $Session
                AllowClobber        = $true
                DisableNameChecking = $true
                ErrorAction         = 'Stop'
            }
            If ($PSBoundParameters.Prefix) {
                $PSSessionParameters.Add("Prefix",$Prefix)
            }
            $ModuleInfo = Import-PSSession @PSSessionParameters

            # Import-Module
            $ModuleParameters = @{
                ModuleInfo          = $ModuleInfo
                DisableNameChecking = $true
                Global              = $true
                ErrorAction         = 'Stop'
            }
            If ($PSBoundParameters.Prefix) {
                $ModuleParameters.Add("Prefix",$Prefix)
            }
            Import-Module @ModuleParameters
        }
        Catch {
            Write-Error $PSItem
        }
    }
    End {}
}