说明
此示例演示如何使用 WindowsProcess 资源来确保进程正在运行。
将“确保”设置为Present“路径”C:\Windows\System32\gpresult.exe和“参数”设置为/h C:\gp2.htm“时,如果资源未运行,则资源以指定的参数开头gpresult.exe。
使用 Invoke-DscResource
此脚本演示如何 WindowsProcess 将资源与 cmdlet 配合使用 Invoke-DscResource ,以确保 gpresult.exe 使用参数 /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
}
}
使用配置
此代码片段演示如何使用资源块定义,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'
}
}
}