Stop a process

Description

This example shows how you can use the WindowsProcess resource to ensure a process isn't running.

With Ensure set to Absent, Path set to C:\Windows\System32\gpresult.exe, and Arguments set to an empty string, the resource stops any running gpresult.exe process.

With Invoke-DscResource

This script shows how you can use the WindowsProcess resource with the Invoke-DscResource cmdlet to ensure gpresult.exe isn't running.

[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
    }
}

With a Configuration

This snippet shows how you can define a Configuration with a WindowsProcess resource block to ensure gpresult.exe isn't running.

Configuration Stop {
    Import-DSCResource -ModuleName 'PSDscResources'

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