プロセスを停止する

説明

この例では、リソースを使用 WindowsProcess してプロセスが実行されていないことを確認する方法を示します。

[確認] を [パス]C:\Windows\System32\gpresult.exeAbsent設定し、引数を空の文字列に設定すると、リソースは実行中gpresult.exeのプロセスを停止します。

Invoke-DscResource

このスクリプトは、コマンドレットでリソースを WindowsProcess 使用して実行されていないことを Invoke-DscResource 確認 gpresult.exe する方法を示しています。

[CmdletBinding()]
param()

begin {
    $SharedParameters = @{
        Name       = 'WindowsFeatureSet'
        ModuleName = 'PSDscResource'
        Properties = @{
            Path      = 'C:\Windows\System32\gpresult.exe'
            Arguments = ''
            Ensure    = 'Absent'
        }
    }

    $NonGetProperties = @(
        'Ensure'
    )
}

process {
    $TestResult = Invoke-DscResource -Method Test @SharedParameters

    if ($TestResult.InDesiredState) {
        $QueryParameters = $SharedParameters.Clone()

        foreach ($Property in $NonGetProperties) {
            $QueryParameters.Properties.Remove($Property)
        }

        Invoke-DscResource -Method Get @QueryParameters
    } else {
        Invoke-DscResource -Method Set @SharedParameters
    }
}

構成を使用する

このスニペットは、リソース ブロックを Configuration 使用 WindowsProcess して定義して、実行されていないことを確認 gpresult.exe する方法を示しています。

Configuration Stop {
    Import-DSCResource -ModuleName 'PSDscResources'

    Node localhost {
        WindowsProcess ExampleWindowsProcess {
            Path      = 'C:\Windows\System32\gpresult.exe'
            Arguments = ''
            Ensure    = 'Absent'
        }
    }
}