运行 C# 中用于启用键盘过滤器的 Powershell 脚本

Run Powershell script in C# to enable keyboardfilter

我有一个问题,我不知道还有什么可以尝试解决它。 我在 Windows 10 Enterprise 2016 机器上,我正在尝试使用 WMI (WEKF_PredefinedKey) 启用键盘快捷键过滤。

我创建了一个 Powershell skript,当通过 rightmousebutton -> 运行 和 PowerShell:运行 时,它可以满足我的需求:

$CommonParams = @{"namespace"="root\standardcimv2\embedded"}

function Disable-Predefined-Key($Id) {
$predefined = Get-WMIObject -class WEKF_PredefinedKey @CommonParams |
              where {
                $_.Id -eq "$Id"
              };

if ($predefined) {
    $predefined.Enabled = 1;
    $predefined.Put() | Out-Null;
    Write-Host Disabled $Id
    }
else {
    Write-Host $Id is not a valid predefined key
    }
}

Disable-Predefined-Key "Win+E"

在 运行 运行此脚本后,快捷方式 "Win+E" 被禁用。

现在的问题: 我需要从 C# WPF 应用程序 运行 这个脚本。 但是无论我做什么,我都无法从 C# 以任何方式禁用快捷方式。

我尝试过的方法(以管理员身份登录并在以下所有情况下包含一个带有 requestedExecutionLevel level="requireAdministrator" 的清单文件,只是为了确保我拥有正确的凭据):

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"powershell.exe";
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = Properties.Settings.Default.DisableKey;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();

并且:

using (PowerShell PowerShellInstance = PowerShell.Create())
{             
  PowerShellInstance.AddScript(@"C:\Users\Administrator\Desktop\Newfolder\disablekey.ps1", false);             
  Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

  foreach (PSObject outputItem in PSOutput)
  {
    if (outputItem != null)
    {
      //TODO: do something with the output item 
    }
  }
}

我还尝试将脚本的内容解析为字符串并尝试在 PowerShell 对象上执行它。 我也在上述方法之间尝试了几乎所有可以想到的方法,但似乎没有任何效果。 第一种方法打开 Powershell window,一眨眼我就可以看到输出,即 "Enabled Win+E",因此脚本似乎至少有 运行。 但是 Win+E 仍然有效。

更让我感到困惑的是,以下启用/禁用 UWF 过滤器(也包括 WMI,也需要管理员权限)的代码工作得很好:

using (PowerShell PowerShellInstance = PowerShell.Create())
{
 PowerShellInstance.AddScript(Properties.Settings.Default.EnableUWF);
 // [0] = result or error
 var result = PowerShellInstance.Invoke();
}

使用以下 EnableUWF 设置值:

$COMPUTER = "localhost"
$NAMESPACE = "root\standardcimv2\embedded"

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned;
$objUWFInstance = Get-WMIObject -namespace $NAMESPACE -class UWF_Filter;
$retval = $objUWFInstance.Enable();
if ($retval.ReturnValue -eq 0) {"Unified Write Filter will be enabled after the next system restart."}
else {"Unknown Error: " + "{0:x0}" -f $retval.ReturnValue}

备注: 在 Keyboardfilter 脚本中设置 ExecutionPolicy 没有任何区别(甚至不受限制)。 安装的 PowerShell 版本: 专业 5 未成年人 1 构建 14393 修订版 1944

希望有人能帮我解决这个问题。

我建议在 WPF 应用程序中使用 C# 的本机 WMI 库,而不是使用 PowerShell。 首先,通过 NuGet 下载 System.Management。 然后为您的 KeyboardFilter

创建一个新的 class
using System.Management;

namespace YourWPFApp
{
    public class KeyboardFilter
    {
        public static void DisablePredfinedKey(string keyId)
        {
            ManagementScope scope = new ManagementScope
            {
                Path = new ManagementPath
                {
                    NamespacePath = @"root\standardcimv2\embedded",
                    ClassName = "WEKF_PredefinedKey"
                }
            };
            ManagementClass wekfPredefinedKey = new ManagementClass(scope.Path);
            var output = wekfPredefinedKey.InvokeMethod("Enable", new object[] { keyId });
        }
    }
}

然后从您的 ViewModel 或 CodeBehind 调用您的 DisablePredefinedKeys 方法。

KeyboardFilter.DisablePredfinedKey("Win+E");

注意:我还没有在 Windows 10

上测试过这个

已在 Windows 10 Enterprise 上测试过。

注意:如果在 64 位系统上 运行,请务必关闭 "Prefer 32-bit" 进行编译。