繁体   English   中英

PowerShell 可以与 RDP 提示交互吗?

[英]Can PowerShell interact with RDP prompts?

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

现在我的脚本工作正常,但是,我只能到达它将开始执行 RDP 指针的地步,我想知道是否有办法点击“连接”:

在此处输入图像描述

我目前用来运行它的方法:

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

有没有更好的方法来运行 .RDP 文件,它也可以让你“连接”? 我还尝试再次勾选“不要问我”,第二天它仍然会提示我这条消息。

我发现启动 RDP session 似乎工作得很好的解决方案如下:

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    %

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM