Skip to content

Instantly share code, notes, and snippets.

@exactmike
Created November 8, 2018 18:42
Show Gist options
  • Select an option

  • Save exactmike/04788dc4200b06f40006ed6f0584af2b to your computer and use it in GitHub Desktop.

Select an option

Save exactmike/04788dc4200b06f40006ed6f0584af2b to your computer and use it in GitHub Desktop.
RDP Connection Function
function Connect-RDP {
param (
[Parameter(Mandatory=$true)]
[string[]]$ComputerName,
[System.Management.Automation.Credential()]
$Credential
)
#https://www.powershellmagazine.com/2014/04/18/automatic-remote-desktop-connection/
# take each computername and process it individually
$ComputerName | ForEach-Object {
# if the user has submitted a credential, store it
# safely using cmdkey.exe for the given connection
if ($PSBoundParameters.ContainsKey('Credential'))
{
# extract username and password from credential
$User = $Credential.UserName
$Password = $Credential.GetNetworkCredential().Password
# save information using cmdkey.exe
cmdkey.exe /generic:$_ /user:$User /pass:$Password
}
# initiate the RDP connection
# connection will automatically use cached credentials
# if there are no cached credentials, you will have to log on
# manually, so on first use, make sure you use -Credential to submit
# logon credential
mstsc.exe /v $_ /f
Start-Sleep -Seconds 30
cmdkey.exe /delete:$_
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment