Functions/Enter-RdpSession.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
<#
.SYNOPSIS
    Opens an RDP Session to a target machine
.DESCRIPTION
    Opens mstsc.exe with the passed -ComputerName parameter.

    If -Credential is set, it will use cmdkey.exe to save the credential
    for passthrough.

.PARAMETER ComputerName
    Mandatory - ComputerName, IpAddress or fqdn of the target machine
.PARAMETER Credential
    Optional - Credential objeect to be passed to the remote desktop session.
.PARAMETER CleanupCredentials
    Optional - Switch to remove any related credentials when the RDP session
    exits.

.EXAMPLE
    Enter-RdpSession -ComputerName 'dc01.local'
.EXAMPLE
    $cred = Get-Credential
    Enter-RdpSession -ComputerName 'dc01.local' -Credential $cred
#>

function Enter-RdpSession {
    [CmdletBinding()]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUsePSCredentialType", "Credential")]
    param(
        [Parameter(Mandatory=$true,Position=1)]
        [string]$ComputerName,

        [System.Management.Automation.PSCredential]
        [System.Management.Automation.CredentialAttribute()]
        $Credential,

        [switch]$CleanupCredentials
    )

    $rdcProcess = New-Object System.Diagnostics.Process
    if ($Credential) {
        $Password = ''
        if ($Credential.GetNetworkCredential()) {
            $Password=$Credential.GetNetworkCredential().password
        } else {
            $Password=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.password))
        }

        Write-Verbose "Adding Credentials for $ComputerName to Windows Credential Store"
        $rdcProcess.StartInfo.FileName = [Environment]::ExpandEnvironmentVariables("%SystemRoot%\system32\cmdkey.exe")
        $rdcProcess.StartInfo.Arguments = "/generic:TERMSRV/$ComputerName /user:$($Credential.UserName) /pass:`"$Password`""
        $rdcProcess.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
        [void]$rdcProcess.Start()
    }

    Write-Verbose "Connecting to RDP Session: $ComputerName"
    $rdcProcess.StartInfo.FileName = [Environment]::ExpandEnvironmentVariables("%SystemRoot%\system32\mstsc.exe")
    $rdcProcess.StartInfo.Arguments = "/v $ComputerName"
    $rdcProcess.StartInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Normal
    [void]$rdcProcess.Start()


    if ($CleanupCredentials) {
        Write-Verbose "Waiting for RDP Session to exit..."
        [void]$rdcProcess.WaitForExit()
        if ($Credential) {
            Write-Verbose "Removing Credentials from Windows Credential Store"
            $rdcProcess.StartInfo.FileName = [Environment]::ExpandEnvironmentVariables("%SystemRoot%\system32\cmdkey.exe")
            $rdcProcess.StartInfo.Arguments = "/delete:TERMSRV/$ComputerName"
            [void]$rdcProcess.Start()
        }
    }
}