Public/Start-MDSADUC.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
Function Start-MDSADUC {
    <#
        .SYNOPSIS
        Open the Active Directory Users and Computers console

        .DESCRIPTION
        Open the Active Directory Users and Computers console as the current user, with a stored MDSCredential, or prompt for a credential

        .EXAMPLE
        Start-MDSADUC

        Open the ADUC console as the current user

        .EXAMPLE
        Start-MDSADUC -MDSCredential MyCred1

        Open the ADUC console with a stored MDSCredential

        .EXAMPLE
        Start-MDSADUC -Credential MyUserName

        Open the ADUC console prompting for a password for the username MyUserName

        .NOTES

    #>

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

    #requires -Module ActiveDirectory

    [CmdletBinding(
        SupportsShouldProcess,
        DefaultParameterSetName='MDSCredential'
    )]
    Param(
        [parameter(Position=0,ParameterSetName='MDSCredential')]
        [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\dsa.msc -Verb RunAs'
            $Parameters = @{
                ArgumentList = $ArgumentList
                ErrorAction  = 'Stop'
            }

            # Capture MDS Credentials
            If ($PsCmdlet.ParameterSetName -eq 'MDSCredential' -and -not [string]::IsNullOrEmpty($MDSCredential)) {
                $Credential = Get-MDSCredential -Name $MDSCredential
            }

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

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