Edit

Start a process under a user

Description

This example shows how you can use the WindowsProcess resource to ensure a process is running under a specific account.

Important

The PSDscResources module isn't supported. It won't receive any updates, including security fixes.

The module was archived on June 13, 2024. The last release was version 2.12.0.0 on June 26, 2019.

The documentation on this page reflects the last release of the PSDscResources module.

You are prompted for a credential if you don't pass one explicitly with the Credential parameter. The Credential property of the resource is set to this value.

With Ensure set to Present, Path set to C:\Windows\System32\gpresult.exe, and Arguments set to /h C:\gp2.htm, the resource starts gpresult.exe with the specified arguments if it isn't running. Because the Credential property is set, the resource starts the process as that account.

With Invoke-DscResource

This script shows how you can use the WindowsProcess resource with the Invoke-DscResource cmdlet to ensure gpresult.exe is running with the arguments /h C:\gp2.htm as a user-specified account.

[CmdletBinding()]
param(
    [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  = '/h C:\gp2.htm'
            Credential = $Credential
            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
    }
}

With a Configuration

This snippet shows how you can define a Configuration with a WindowsProcess resource block to ensure gpresult.exe is running with the arguments /h C:\gp2.htm as a user-specified account.

Configuration StartUnderUser {
    [CmdletBinding()]
    param(
       [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  = '/h C:\gp2.htm'
            Credential = $Credential
            Ensure     = 'Present'
        }
    }
}