Public/Start-MDSSitesAndServices.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
Function Start-MDSSitesAndServices {
    <#
        .SYNOPSIS
        Open Active Directory Sites and Services console

        .DESCRIPTION
        Open ctive Directory Sites and Services console as the current user, with a stored MDSCredential, or prompt for a credential

        .EXAMPLE
        Start-MDSSitesAndServices

        Open the ctive Directory Sites and Services console console as the current user

        .EXAMPLE
        Start-MDSSitesAndServices -MDSCredential MyCred1

        Open the ctive Directory Sites and Services console console with a stored MDSCredential

        .EXAMPLE
        Start-MDSSitesAndServices -Credential MyUserName

        Open the ctive Directory Sites and Services console console prompting for a password for the username MyUserName

        .NOTES

        #>


    #requires -Module ActiveDirectory

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

    [CmdletBinding(
        SupportsShouldProcess,
        DefaultParameterSetName='MDSCredential'
    )]
    Param(
        [parameter(
            Position=0,
            ParameterSetName='MDSCredential'
        )]
        [ValidateNotNullOrEmpty()]
        [String]$MDSCredential,

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

    Begin {}
    Process {
        Try {
            $ArgumentList = 'Start-Process -FilePath $env:SystemRoot\System32\mmc.exe -ArgumentList $env:SystemRoot\System32\dssite.msc -Verb RunAs'
            $Parameters = @{
                'ArgumentList' = $ArgumentList
            }

            # MDSCredential
            If ($PSBoundParameters.MDSCredential) {
                $Credential = Get-MDSCredential -Name $MDSCredential -ErrorAction Stop
            }

            # Add credentials to parameter list
            If ($null -ne $Credential) {
                $Parameters.Add('Credential',$Credential)
            }

            $ShouldProcessTarget = $Credential.UserName
            If ($PSCmdlet.ShouldProcess($ShouldProcessTarget,$MyInvocation.MyCommand)) {
                Start-Process PowerShell @Parameters -WindowStyle Hidden
            }
        }
        Catch {
            Write-Error $PSItem
        }
    }
    End {}
}