如何使RDP窗口从Powershell中还原?

时间:2019-05-02 15:23:04

标签: powershell

我已打开RDP连接并将其最小化,或在后台。我想恢复/激活它。

当我用记事本尝试此操作时,它正在恢复,但无法捕获rdp窗口。 我先检查打开的窗户

Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object MainWindowTitle, name, id

它使我将Mstsc作为rdp进程返回,但是当我尝试将其放在脚本中时不起作用。 如果我将其更改为记事本,它将正常工作(如果记事本已打开) 我必须在community.spiceworks.com上将这段代码感谢Bobmccoy。

function Set-WindowStyle {
param(
    [Parameter()]
    [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', 
                 'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', 
                 'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
    $Style = 'SHOW',
    [Parameter()]
    $MainWindowHandle = (Get-Process -Id $pid).MainWindowHandle
)
    $WindowStates = @{
        FORCEMINIMIZE   = 11; HIDE            = 0
        MAXIMIZE        = 3;  MINIMIZE        = 6
        RESTORE         = 9;  SHOW            = 5
        SHOWDEFAULT     = 10; SHOWMAXIMIZED   = 3
        SHOWMINIMIZED   = 2;  SHOWMINNOACTIVE = 7
        SHOWNA          = 8;  SHOWNOACTIVATE  = 4
        SHOWNORMAL      = 1
    }
    Write-Verbose ("Set Window Style {1} on handle {0}" -f $MainWindowHandle, $($WindowStates[$style]))

    $Win32ShowWindowAsync = Add-Type –memberDefinition @” 
    [DllImport("user32.dll")] 
    public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru

    $Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[$Style]) | Out-Null
}

(Get-Process -Name mstsc).MainWindowHandle | foreach { Set-WindowStyle MINIMIZE $_ }
(Get-Process -Name mstsc).MainWindowHandle | foreach { Set-WindowStyle RESTORE $_ }

如果在最后两句话中放入记事本,则此代码有效。我必须首先将其最小化,因为如果它在后台,它将无法恢复。但这不适用于rdp(mstsc)进程。

0 个答案:

没有答案