# Automating RDP connection using powershell script failed to bypass Legal Notice

**URL:** <https://forums.powershell.org/t/automating-rdp-connection-using-powershell-script-failed-to-bypass-legal-notice/14170>\
**Category:** PowerShell Help\
**Created:** [April 15, 2020, 2:15pm UTC](https://forums.powershell.org/t/automating-rdp-connection-using-powershell-script-failed-to-bypass-legal-notice/14170 "2020-04-15T14:15:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vignesh1721](https://avatars.discourse-cdn.com/v4/letter/v/dbc845/32.png) [@vignesh1721](https://forums.powershell.org/u/vignesh1721)\
**Post date:** [April 15, 2020, 2:15pm UTC](https://forums.powershell.org/t/automating-rdp-connection-using-powershell-script-failed-to-bypass-legal-notice/14170/1 "2020-04-15T14:15:59Z")

</div>

``

$server = “ServerName”  
$Cred1 = New-Object -TypeName pscredential -ArgumentList “UserName”,(ConvertTo-SecureString -String ‘password’ -AsPlainText -Force);

``

Function Connect-Mstsc {

``

[cmdletbinding(SupportsShouldProcess,DefaultParametersetName=‘UserPassword’)]  
param (  
[Parameter(Mandatory=$true,  
ValueFromPipeline=$true,  
ValueFromPipelineByPropertyName=$true,  
Position=0)]  
[Alias(‘CN’)]  
[string] $ComputerName,  
[Parameter(ParameterSetName=‘UserPassword’,Mandatory=$true,Position=1)]  
[Alias(‘U’)]  
[string] $User,  
[Parameter(ParameterSetName=‘UserPassword’,Mandatory=$true,Position=2)]  
[Alias(‘P’)]  
[string] $Password,  
[Parameter(ParameterSetName=‘Credential’,Mandatory=$true,Position=1)]  
[Alias(‘C’)]  
[PSCredential] $Credential,  
[Alias(‘A’)]  
[switch] $Admin,  
[Alias(‘MM’)]  
[switch] $MultiMon,  
[Alias(‘F’)]  
[switch] $FullScreen,  
[Alias(‘Pu’)]  
[switch] $Public,  
[Alias(‘W’)]  
[int] $Width,  
[Alias(‘H’)]  
[int] $Height,  
[Alias(‘WT’)]  
[switch] $Wait  
)

``

begin {  
[string]$MstscArguments = ‘’  
switch ($true) {  
{$Admin} {$MstscArguments += '/admin '}  
{$MultiMon} {$MstscArguments += '/multimon '}  
{$FullScreen} {$MstscArguments += '/f '}  
{$Public} {$MstscArguments += '/public '}  
{$Width} {$MstscArguments += "/w:$Width "}  
{$Height} {$MstscArguments += "/h:$Height "}  
}

``

if ($Credential) {  
$User = $Credential.UserName  
$Password = $Credential.GetNetworkCredential().Password  
}  
}  
process {  
foreach ($Computer in $ComputerName) {  
$ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo  
$Process = New-Object System.Diagnostics.Process

``

# Remove the port number for CmdKey otherwise credentials are not entered correctly

if ($Computer.Contains(‘:’)) {  
$ComputerCmdkey = ($Computer -split ‘:’)[0]  
} else {  
$ComputerCmdkey = $Computer  
}

``

$ProcessInfo.FileName = “$($env:SystemRoot)\system32\cmdkey.exe”  
$ProcessInfo.Arguments = “/generic:TERMSRV/$ComputerCmdkey /user:$User /pass:$($Password)”  
$ProcessInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden  
$Process.StartInfo = $ProcessInfo  
if ($PSCmdlet.ShouldProcess($ComputerCmdkey,‘Adding credentials to store’)) {  
[void]$Process.Start()  
}

``

$ProcessInfo.FileName = “$($env:SystemRoot)\system32\mstsc.exe”  
$ProcessInfo.Arguments = “$MstscArguments /v $Computer”  
$ProcessInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Normal  
$Process.StartInfo = $ProcessInfo  
if ($PSCmdlet.ShouldProcess($Computer,‘Connecting mstsc’)) {  
[void]$Process.Start()  
if ($Wait) {  
$null = $Process.WaitForExit()  
}  
}  
}  
}  
}

``

connect-mstsc -ComputerName $server -Credential $Cred1 -ErrorAction stop

``

##below code is used to skip certificate warning###

``

[void][System.Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’)

# Get the ID of the process

$WindowsHandle = Get-Process | Where-Object { $\_.ProcessName -match ‘mstsc’ } | Select-Object -ExpandProperty Id

# Activate the window

$wshell = New-Object -ComObject wscript.shell;  
$wshell.AppActivate($WindowsHandle) | Out-Null

``

``

##Below code needs to modified to click ok button legal notice banner

``

function Click-MouseButton  
{  
$signature=@’  
[DllImport(“user32.dll”,CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]  
public static extern void mouse\_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);  
'@

``

$SendMouseClick = Add-Type -memberDefinition $signature -name “Win32MouseEventNew” -namespace Win32Functions -passThru

``

$SendMouseClick::mouse\_event(0x00000002, 0, 0, 0, 0);  
$SendMouseClick::mouse\_event(0x00000004, 0, 0, 0, 0);  
}

``

[system.Reflection.Assembly]::LoadWithPartialName(“”) | out-null

``

# Set the exactly position of cursor in some iexplore hyperlink between the (open parenthesis) below:

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(790,675)  
Click-MouseButton

``

Experts am using the above powershell script to establish RDP connection without any manual intervention as the above code failed to click ok to bypass legal notice screen i have found click-mouse button function to bypass this but not working can someone help to correct the script?? Any help much appreciated.

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/automating-rdp-connection-using-powershell-script-failed-to-bypass-legal-notice/14170/2 "2024-05-16T20:29:55Z")

</div>


