在使用者下啟動程式
描述
此範例示範如何使用 WindowsProcess
資源來確保進程在特定帳戶下執行。
如果您未使用 Credential 參數明確傳遞認證,系統會提示您輸入 認證 。 資源的 Credential 屬性會設定為這個值。
將 [確定] 設定為 Present
,[路徑] 設定為 C:\Windows\System32\gpresult.exe
,而 [引數] 設定 /h C:\gp2.htm
為 ,則資源會在未執行時以指定的引數開頭 gpresult.exe
。 因為已設定 Credential 屬性,所以資源會以該帳戶的形式啟動進程。
使用 Invoke-DscResource
此腳本示範如何搭配 Cmdlet 使用 WindowsProcess
資源,以確保 gpresult.exe
以引數 /h C:\gp2.htm
作為使用者指定的帳號執行。 Invoke-DscResource
[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
}
}
使用組態
此程式碼片段示範如何使用資源區塊來定義 , Configuration
WindowsProcess
以確保 gpresult.exe
以引數 /h C:\gp2.htm
作為使用者指定的帳號執行。
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'
}
}
}
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應