停止進程
描述
此範例示範如何使用 WindowsProcess
資源來確保進程未執行。
將 [確定 ] 設定為 Absent
、 [路徑 ] 設定為 C:\Windows\System32\gpresult.exe
,並將 [引數 ] 設定為空字串時,資源會停止任何執行 gpresult.exe
中的進程。
使用 Invoke-DscResource
此腳本示範如何搭配 Invoke-DscResource
Cmdlet 使用 WindowsProcess
資源,以確保 gpresult.exe
未執行。
[CmdletBinding()]
param()
begin {
$SharedParameters = @{
Name = 'WindowsFeatureSet'
ModuleName = 'PSDscResource'
Properties = @{
Path = 'C:\Windows\System32\gpresult.exe'
Arguments = ''
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
}
}
使用組態
此程式碼片段示範如何使用資源區塊來定義 Configuration
WindowsProcess
,以確保 gpresult.exe
未執行。
Configuration Stop {
Import-DSCResource -ModuleName 'PSDscResources'
Node localhost {
WindowsProcess ExampleWindowsProcess {
Path = 'C:\Windows\System32\gpresult.exe'
Arguments = ''
Ensure = 'Absent'
}
}
}
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應