Dela via


Starta en process

Description

Det här exemplet visar hur du kan använda resursen WindowsProcess för att säkerställa att en process körs.

Med Se till inställd på Present, Sökväg inställd på C:\Windows\System32\gpresult.exeoch Argument inställd på /h C:\gp2.htmbörjar gpresult.exe resursen med de angivna argumenten om den inte körs.

Med Invoke-DscResource

Det här skriptet visar hur du kan använda resursen WindowsProcess med cmdleten Invoke-DscResource för att säkerställa gpresult.exe att körs med argumenten /h C:\gp2.htm.

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

Med en konfiguration

Det här kodfragmentet visar hur du kan definiera ett Configuration med ett WindowsProcess resursblock för att säkerställa gpresult.exe att körs med argumenten /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'
        }
    }
}