Hinzufügen blockierter Tastenkombinationen

Unterstützte Editionen
✅ IoT Enterprise LTSC
✅ IoT Enterprise
✅ Enterprise LTSC
✅ Enterprise
✅ Education

Das folgende Windows PowerShell-Beispielskript verwendet die Windows Management Instrumentation (WMI)-Anbieter für den Tastaturfilter, um drei Funktionen zu erstellen, um den Tastaturfilter so zu konfigurieren, dass der Tastaturfilter Tastenkombinationen blockiert. Es werden mehrere Verwendungsmöglichkeiten der einzelnen Funktionen veranschaulicht.

Die erste Funktion, Block-Predefined-Key, blockiert Tastenkombinationen, die für den Tastaturfilter vordefiniert sind.

Die zweite Funktion, Block-Custom-Key, blockiert benutzerdefinierte Tastenkombinationen mithilfe der englischen Tastennamen.

Die dritte Funktion, Block-Scancode, blockiert benutzerdefinierte Tastenkombinationen mithilfe des Tastaturscancodes für die Taste.

Block-rules.ps1

#
# Copyright (C) Microsoft. All rights reserved.
#

<#
.Synopsis
    This script shows how to use the built-in WMI providers to create and
    activate Keyboard Filter blocking rules through Windows PowerShell on
    the local computer.
.Parameter ComputerName
    Optional parameter to specify a remote machine that this script should
    manage.  If not specified, the script will execute all WMI operations
    locally.
#>
param (
    [String] $ComputerName
)

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

function Block-Predefined-Key($Id) {
    <#
    .Synopsis
        Block a predefined key combination.
    .Description
        Use Get-WMIObject to enumerate WEKF_PredefinedKey instances, find the
        instance matching Id, and set its Enabled property to 1. Enabled refers
        to the blocking rule; when the rule is enabled, the key combination is
        blocked.
    .Example
        Block-Predefined-Key "Ctrl+Alt+Del"
        Blocks the Ctrl+Alt+Delete key combination.
#>

    $predefined = Get-WMIObject -class WEKF_PredefinedKey @CommonParams |
        where {
            $_.Id -eq "$Id"
        };

    if ($predefined) {
        $predefined.Enabled = 1;
        $predefined.Put() | Out-Null;
        Write-Host "Activated predefined-key blocking rule for $Id"
    } else {
        Write-Error "$Id is not a valid predefined key"
    }
}


function Block-Custom-Key($Id) {
    <#
    .Synopsis
        Block a custom key combination.
    .Description
        Use Get-WMIObject to enumerate WEKF_CustomKey instances, find the
        instance matching Id, and set its Enabled property to 1. Enabled refers
        to the blocking rule; when the rule is enabled, the key combination is
        blocked.

        If the custom-key instance doesn't exist, add a new WEKF_CustomKey
        instance by using Set-WMIInstance.
    .Example
        Block-Custom-Key "Ctrl+V"
        Blocks the Ctrl+V key combination.
#>

    $custom = Get-WMIObject -class WEKF_CustomKey @CommonParams |
        where {
            $_.Id -eq "$Id"
        };

    if ($custom) {
        # The rule exists. Activate the blocking rule.
        $custom.Enabled = 1;
        $custom.Put() | Out-Null;
        "Activated custom-key blocking rule for $Id";

    } else {
        Set-WMIInstance `
            -class WEKF_CustomKey `
            -argument @{Id="$Id"} `
            @CommonParams | Out-Null
        "Added custom-key blocking rule for $Id.";
    }
}

function Block-Scancode($Modifiers, [int]$Code) {
    <#
    .Synopsis
        Block a key combination identified by its scan code.
    .Description
        Use Get-WMIObject to enumerate WEKF_Scancode instances, find the
        instance matching Modifiers and Scancode, and set its Enabled property
        to 1. Enabled refers to the blocking rule; when the rule is enabled,
        the key combination is blocked.

        If the scan-code instance doesn't exist, add a new WEKF_Scancode
        instance by using Set-WMIInstance.
    .Example
        Block-Scancode "Ctrl" 37
        Blocks Ctrl combined with keyboard scan code 37 (base 10).
#>

    $scancode =
        Get-WMIObject -class WEKF_Scancode @CommonParams |
            where {
                ($_.Modifiers -eq $Modifiers) -and ($_.Scancode -eq $Code)
            }

    if($scancode) {
        $scancode.Enabled = 1
        $scancode.Put() | Out-Null
        "Activated scan-code blocking rule for {0}+{1:X4}" -f $Modifiers, $Code
    } else {
        Set-WMIInstance `
            -class WEKF_Scancode `
            -argument @{Modifiers="$Modifiers"; Scancode=$Code} `
            @CommonParams | Out-Null

        "Added scan-code blocking rule for {0}+{1:X4}" -f $Modifiers, $Code
    }
}

# Some example uses of the functions defined above.
Block-Predefined-Key "Ctrl+Alt+Del"
Block-Predefined-Key "Ctrl+Esc"
Block-Custom-Key "Ctrl+V"
Block-Custom-Key "Numpad0"
Block-Custom-Key "Shift+Numpad1"
Block-Custom-Key "Shift+5"
Block-Scancode "Ctrl" 37

Windows PowerShell Skriptbeispiele für Tastaturfilter

Tastaturfilter WMI-Anbieterreferenz

Tastaturfilter