Automating RDP connection using powershell script failed to bypass Legal Notice

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

Function Connect-Mstsc {

param (
[string] $ComputerName,
[string] $User,
[string] $Password,
[PSCredential] $Credential,
[switch] $Admin,
[switch] $MultiMon,
[switch] $FullScreen,
[switch] $Public,
[int] $Width,
[int] $Height,
[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’)) {

$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’)) {
if ($Wait) {
$null = $Process.WaitForExit()

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

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


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;
$wshell.AppActivate($WindowsHandle) | Out-Null

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

function Click-MouseButton
[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)

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.