共用方式為


在使用者下停止進程

描述

此範例示範如何使用 WindowsProcess 資源來確保進程未執行,並使用指定的帳號視需要加以停止。

如果您未使用 Credential 參數明確傳遞認證,系統會提示您輸入 認證 。 資源的 Credential 屬性會設定為這個值。

[確定 ] 設定為 Absent[路徑 ] 設定為 C:\Windows\System32\gpresult.exe ,並將 [引數 ] 設定為空字串時,資源會停止任何執行 gpresult.exe 中的進程。 因為已設定 Credential 屬性,所以資源會停止該程式做為該帳戶。

使用 Invoke-DscResource

此腳本示範如何搭配 Invoke-DscResource Cmdlet 使用 WindowsProcess 資源,以確保 gpresult.exe 未執行,並將它停止為使用者指定的帳號。

[CmdletBinding()]
param(
    [ValidateNotNullOrEmpty()]
    [System.Management.Automation.PSCredential]
    [System.Management.Automation.Credential()]
$Credential = (Get-Credential)
)

begin {
    $SharedParameters = @{
        Name       = 'WindowsFeatureSet'
        ModuleName = 'PSDscResource'
        Properties = @{
            Path       = 'C:\Windows\System32\gpresult.exe'
            Arguments  = ''
            Credential = $Credential
            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 StopUnderUser {
    [CmdletBinding()]
    param(
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = (Get-Credential)
    )

    Import-DSCResource -ModuleName 'PSDscResources'

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