【问题标题】:Windows 10 - Run a command when connecting to a device, say, a dock or monitor [Automate]Windows 10 - 连接到设备时运行命令,例如扩展坞或显示器 [自动化]
【发布时间】:2021-07-22 11:53:17
【问题描述】:

在 Windows 10 中,当 PC 连接到特定设备(如扩展坞)时,是否可以自动运行 cmd/powershell 命令? (就像连接特定设备触发命令一样)

我想做这样的事情-

If [device1 connected] -> Run Command A
If [device1 disconnected] -> Run Command B
If [device2 connected] -> Run Command C
...

【问题讨论】:

    标签: windows powershell batch-file cmd windows-10


    【解决方案1】:

    是的。

    https://www.winhelponline.com/blog/trigger-launch-program-script-connecting-to-specific-network/ 描述了使用任务调度程序在事件日志事件上运行脚本:在事件日志中创建通知的任何内容都可以用作触发器。

    在事件日志中查找事件:记下日志名称和事件 ID。

    在“常规”选项卡中为您的任务命名,然后单击“触发器”,然后单击“新建”。选择“On an event”:选择上面提到的 Log,然后输入 Event ID。

    https://devblogs.microsoft.com/scripting/use-powershell-to-monitor-and-respond-to-events-on-your-server/ 描述 使用 WMI 作为永久事件使用者:事件使用者可以链接到许多 Windows 事件,包括连接/断开连接事件,而不仅仅是事件日志事件,但不是不言自明的。

    $consumerPath = Set-WmiInstance -Class ActiveScriptEventConsumer

    【讨论】:

    • help Set-WmiInstance -Full 新的 CIM cmdlet,引入了 Windows PowerShell 3.0,执行与 WMI cmdlet 相同的任务。 CIM cmdlet 符合 WS-Management (WSMan) 标准和通用信息模型 (CIM) 标准。这使 cmdlet 能够使用相同的技术来管理基于 Windows 的计算机和运行其他操作系统的计算机。不要使用Set-WmiInstance,而是考虑使用Set-CimInstance (/powershell/module/cimcmdlets/set-ciminstance) 或New-CimInstancecmdlets。
    猜你喜欢
    • 2022-08-11
    • 2016-06-06
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多