Решение, которое я нашел для запуска сеанса RDP, который, кажется, работает достаточно хорошо, заключается в следующем:
function Connect-RDP {
param (
[Parameter(Mandatory=$true)]
$ComputerName,
[System.Management.Automation.Credential()]
$Credential
)
# 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
}
}
Затем вы вызываете его с помощью Connect-rdp -ComputerName myserver -Credential (Get-Credential )
.
Возможно, вы можете настроить свой сценарий так, чтобы использовать этот командлет вместо вашего файла .rdp.
Я нашел решение здесь: https://www.powershellmagazine.com/2014/04/18/automatic-remote-desktop-connection/
Другой способ, которым выМожно попробовать это:
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
# Get the ID of the process
$WindowsHandle = Get-Process | Where-Object { $_.MainWindowTitle -Match 'Remote Desktop Connection' } | Select-Object -ExpandProperty Id
# Activate the window
$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate($WindowsHandle) | Out-Null
# SendKey to connect
[System.Windows.Forms.SendKeys]::SendWait("%{c}")
%{c}
обозначает ALT + C
Клавиши-модификаторы:
Key | Code
-----------
SHIFT +
CTRL ^
ALT %