Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Description
This example shows how you can use the WindowsProcess
resource to ensure a process isn't running,
using a specified account to stop it if needed.
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 Absent
, Path set to C:\Windows\System32\gpresult.exe
, and
Arguments set to an empty string, the resource stops any running gpresult.exe
process. Because
the Credential property is set, the resource stops 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
isn't running, stopping it as a user-specified account.
[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
}
}
With a Configuration
This snippet shows how you can define a Configuration
with a WindowsProcess
resource block to
ensure gpresult.exe
isn't running, stopping it as a user-specified account.
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'
}
}
}