PowerShell 可以与 RDP 提示交互吗?

Can PowerShell interact with RDP prompts?

我目前正在编写一个脚本来自动执行一些检查,我有一些客户端我想自动登录到他们的服务器之一或使用通过 RDweb 托管的应用程序。

现在我的脚本工作正常,但是,我只能达到它开始执行 RDP 指针的程度,我想知道是否有办法点击 "connect" :

我目前使用的方法是运行这个:

[System.Diagnostics.Process]::Start("c:\file\path\file.rdp")

是否有更好的方法来 运行 .RDP 文件,它也允许您 "Connect"?我也尝试再次勾选"don't ask me",第二天它仍然会提示我这个消息。

我发现启动 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 )调用它。

也许您可以调整脚本以使用此 cmdlet 而不是 file.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    %