Не удалось отправить нормально во время автоматизации RDP на баннер с юридическим уведомлением - PullRequest
0 голосов
/ 16 апреля 2020

$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


[System.Windows.Forms.SendKeys]::SendWait(“{TAB}”)
[System.Windows.Forms.SendKeys]::SendWait(“{TAB}”)
[System.Windows.Forms.SendKeys]::SendWait(“{TAB}”)
[System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)


##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("Remote Desktop Connection") | 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

Эксперты могут помочь мне в создании сценария. Я использую приведенный выше сценарий для автоматизации RDP-подключения и нажимаю кнопку «ОК» на экране официального уведомления при входе в RDP, но сценарию не удалось отправить кнопку «ОК», любая помощь очень ценится!

если вышеописанный скрипт не может его сделать, то предложите несколько способов автоматизации RDP-соединения с сервером и обход баннера с юридическим уведомлением

...