使用 Powershell 開啟開始功能表

使用 Powershell 開啟開始功能表

我目前正在開發業務流程自動化軟體,實際上使用者操作是由機器人模擬的。我需要將一些資訊傳遞到 Windows 7 的開始功能表,我想知道是否可以使用 powershell 腳本開啟 Windows 開始功能表?因為機器人可以理解開啟powershell的訊息。請任何建議都會很好。

答案1

是的,可以使用小VB

將此程式碼複製到記事本中,並另存為startmenu.vbs. [確保儲存為 startmenu.vbs.txt]

set wShell=wscript.createobject("wscript.shell")
wShell.sendkeys "^{ESC}"
Set WshShell = Nothing

然後,您可以使用 運行它cscript C:\somefilepath\startmenu.vbs

(顯然,您必須指定保存路徑)


或者,翻譯成電源外殼解決方案:

$wShell = New-Object -ComObject "wscript.shell"
$wShell.SendKeys("^{ESC}")

可以進一步縮短為:

(New-Object -ComObject "wscript.shell").SendKeys("^{ESC}")  

答案2

這是一個稍微詳細的解決方案,不模擬按鍵(對於 CTRL+ESC 綁定到另一個程式/指令等的情況)。

Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;

public class Win32API
{
    [DllImport("user32.dll")]    
    private static extern IntPtr FindWindow(string lpClassName, String lpWindowName);    
    [DllImport("user32.dll")]    
    private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);  

    public static void OpenStartMenu ()
    {
        const int SC_TASKLIST = 0xf130;
        const int WM_SYSCOMMAND = 0x112;
        IntPtr hWnd = FindWindow("Shell_TrayWnd", "");
        SendMessage(hWnd, WM_SYSCOMMAND, SC_TASKLIST, 0);
    }
}
"@
[Win32API]::OpenStartMenu()

相關內容