共用方式為


停止進程

描述

此範例示範如何使用 WindowsProcess 資源來確保進程未執行。

[確定 ] 設定為 Absent[路徑 ] 設定為 C:\Windows\System32\gpresult.exe ,並將 [引數 ] 設定為空字串時,資源會停止任何執行 gpresult.exe 中的進程。

使用 Invoke-DscResource

此腳本示範如何搭配 Invoke-DscResource Cmdlet 使用 WindowsProcess 資源,以確保 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
    }
}

使用組態

此程式碼片段示範如何使用資源區塊來定義 ConfigurationWindowsProcess ,以確保 gpresult.exe 未執行。

Configuration Stop {
    Import-DSCResource -ModuleName 'PSDscResources'

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