共用方式為


啟動處理序

描述

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

[確定] 設定為 、[路徑] 設定為 C:\Windows\System32\gpresult.exePresent ,並將 [引數] 設定為 /h C:\gp2.htm ,如果資源未執行,則資源會以指定的引數開頭 gpresult.exe

使用 Invoke-DscResource

此腳本示範如何搭配 Cmdlet 使用 WindowsProcess 資源,以確保 gpresult.exe 使用 引數 /h C:\gp2.htm 執行。 Invoke-DscResource

[CmdletBinding()]
param()

begin {
    $SharedParameters = @{
        Name       = 'WindowsProcess'
        ModuleName = 'PSDscResource'
        Properties = @{
            Path      = 'C:\Windows\System32\gpresult.exe'
            Arguments = '/h C:\gp2.htm'
            Ensure    = 'Present'
        }
    }

    $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 使用 引數 執行 /h C:\gp2.htm

Configuration Start {
    Import-DSCResource -ModuleName 'PSDscResources'

    Node localhost {
        WindowsProcess ExampleWindowsProcess {
            Path      = 'C:\Windows\System32\gpresult.exe'
            Arguments = '/h C:\gp2.htm'
            Ensure    = 'Present'
        }
    }
}