Forum / Remote Desktop Manager - PowerShell Repository

Script - to send Keys (as if someone is in front of the keyboard) to a Windows popup

  • Create an Issue
  • Cancel

Hi, do to a short story, at work, they implemented the GPO that Force a login at each connection, meaning that "saved" credentials can't be used and we get a popup to type the Password (User-ID is kept). The thing is we can't change back the GPO (security reason), but we have to connect to hundreds of Windows servers and typing our password on ANY SINGLE session doesn't make sense.

We were told by support that they support Scripts with "events". Maybe we could launch a Script that will send the Password to the window that popup that say we can't use Saved Credentials and retype our password for us! The keys need to be sent to the computer that has RDM installed (not the Remote Session) since the popup appears before connection and forcing to re-enter our passwords.

I tried using an example of the web, but specifying the "Sécurité Windows" window title or the "Remote Desktop Manager" title doesn't work. since i'm not good in scripting, maybe someone here can make that work quickly! Any help would be appreciate !

Thanks !


Here is the test code :


<#
.SYNOPSIS
Send a sequence of keys to an application window

.DESCRIPTION
This Send-Keys script send a sequence of keys to an application window.
To have more information about the key representation look at http://msdn.microsoft.com/en-us/library/System.Windows.Forms.SendKeys(v=vs.100).aspx
(C)2013 Massimo A. Santin - Use it at your own risk.

.PARAMETER ApplicationTitle
The title of the application window

.PARAMETER Keys
The sequence of keys to send

.PARAMETER WaitTime
An optional number of seconds to wait after the sending of the keys

.EXAMPLE
Send-Keys "foobar - Notepad" "Hello world"

Send the sequence of keys "Hello world" to the application titled "foobar - Notepad".

.EXAMPLE
Send-Keys "foobar - Notepad" "Hello world" -WaitTime 5

Send the sequence of keys "Hello world" to the application titled "foobar - Notepad"
and wait 5 seconds.

.EXAMPLE
New-Item foobar.txt -ItemType File; notepad foobar.txt ; Send-Keys "foobar - Notepad" "Hello world{ENTER}Ciao mondo{ENTER}" -WaitTime 1; Send-Keys "foobar - Notepad" "^s"

This command sequence creates a new text file called foobar.txt, opens the file using a notepad,
writes some text and saves the file using notepad.

.LINK
http://msdn.microsoft.com/en-us/library/System.Windows.Forms.SendKeys(v=vs.100).aspx
#>

param (
[Parameter(Mandatory=$True,Position=1)]
[string]
$ApplicationTitle,

[Parameter(Mandatory=$True,Position=2)]
[string]
$Keys,

[Parameter(Mandatory=$false)]
[int] $WaitTime
)

# load assembly cotaining class System.Windows.Forms.SendKeys
[void] [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#Add-Type -AssemblyName System.Windows.Forms

# add a C# class to access the WIN32 API SetForegroundWindow
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class StartActivateProgramClass {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@

# get the applications with the specified title
$p = Get-Process | Where-Object { $_.MainWindowTitle -eq $ApplicationTitle }
if ($p)
{
# get the window handle of the first application
$h = $p[0].MainWindowHandle
# set the application to foreground
[void] [StartActivateProgramClass]::SetForegroundWindow($h)

# send the keys sequence
# more info on MSDN at http://msdn.microsoft.com/en-us/library/System.Windows.Forms.SendKeys(v=vs.100).aspx
[System.Windows.Forms.SendKeys]::SendWait($Keys)
if ($WaitTime)
{
Start-Sleep -Seconds $WaitTime
}
}

Clock2 yrs

Hello,

Have you considered using a Typing Macro in the Events you mentioned?
Of course this will type "given" text mindlessly to the window that has the focus... You probably understand that the password will be displayed if the focus is not at the right place.

You should take a look here: https://help.remotedesktopmanager.com/settings_events_autotypingmacro.htm

Do I understand that the GPO will prevent you from copy / pasting the password in the RDP session?

Regards,



Alex Belisle

signaturesignature

Clock2 yrs

Alexandre Bélisle wrote:

Hello,

Have you considered using a Typing Macro in the Events you mentioned?
Of course this will type "given" text mindlessly to the window that has the focus... You probably understand that the password will be displayed if the focus is not at the right place.

You should take a look here: https://help.remotedesktopmanager.com/settings_events_autotypingmacro.htm

Do I understand that the GPO will prevent you from copy / pasting the password in the RDP session?

Regards,


I can't for now since it's a feature reserved for "Enterprise" licence and for now, i'm still on the free licence. Waiting for the management to accept to acquire 2 licence.

In the meantine, I had success with this simple code that i launch in the morning at the same time I launch RDM Free and it "monitor" the window that are opening and look for one with "Sécurité Windows" or "Windows Security" and if so, it do a "SendKeys" of the password + ENTER key. That mostly fix my issue.

<#
.SYNOPSIS
Push Crentials to a Windows Security Popup when Connecting to Windows 2016 Servers with a GPO that prevent saved credentials to be use.
.NOTES
Useful to enter credentials for RDP.
#>
# import required assemblies
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms

Do {
# helper function to locate a open program using by a given Window name
Try {
[Microsoft.VisualBasic.Interaction]::AppActivate("Sécurité Windows")
Start-Sleep -Milliseconds 500
[System.Windows.Forms.SendKeys]::SendWait('MY_PWD{ENTER}')
$windowFound = $true;
write-host "Trouvé une fenêtre"
} Catch {
# do nothing
write-host "pas de fenetre"
}
Start-Sleep -Milliseconds 1500
}while($true)

Clock2 yrs