PowerShell 脚本示例,可以实现一键关闭显示器的功能:

PowerShell 脚本示例,可以实现一键关闭显示器的功能:

powershellCopy Code
# 一键关闭显示器的函数
function Turn-OffDisplay {
    # 调用 User32.dll 中的 API 来关闭显示器
    Add-Type -TypeDefinition @"
    using System;
    using System.Runtime.InteropServices;

    public class Display {
        [DllImport("user32.dll")]
        public static extern void SendMessage(int hWnd, int Msg, int wParam, int lParam);
        public const int HWND_BROADCAST = 0xFFFF;
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MONITORPOWER = 0xF170;
        public const int MONITOR_OFF = 2;

        public static void TurnOff() {
            SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_OFF);
        }
    }
"@
    [Display]::TurnOff()
}

# 调用函数关闭显示器
Turn-OffDisplay

使用说明:

  1. 保存脚本

    • 将上述代码保存为 .ps1 文件,例如 TurnOffDisplay.ps1
  2. 运行脚本

    • 以管理员身份打开 PowerShell。
    • 导航到脚本所在目录并执行该脚本,例如:.\TurnOffDisplay.ps1

注意事项:

  • 此脚本将立即关闭显示器,需按任意键或移动鼠标才能恢复。

 

posted @ 2024-09-23 17:03  suv789  阅读(316)  评论(0)    收藏  举报