إيقاف عملية ضمن مستخدم

الوصف

يوضح هذا المثال كيف يمكنك استخدام WindowsProcess المورد لضمان عدم تشغيل عملية، باستخدام حساب محدد لإيقافه إذا لزم الأمر.

تتم مطالبتك ببيانات اعتماد إذا لم تقم بتمرير واحدة بشكل صريح باستخدام معلمة بيانات الاعتماد . يتم تعيين خاصية Credential للمورد إلى هذه القيمة.

مع تعيين Ensure إلى Absent، وتعيين المسار إلى C:\Windows\System32\gpresult.exe، وتعيين الوسيطات إلى سلسلة فارغة، يتوقف المورد عن أي عملية قيد التشغيل gpresult.exe . نظرا لتعيين الخاصية Credential ، يوقف المورد العملية مثل هذا الحساب.

مع Invoke-DscResource

يوضح هذا البرنامج النصي كيف يمكنك استخدام WindowsProcess المورد مع Invoke-DscResource cmdlet للتأكد من gpresult.exe عدم تشغيله، وإيقافه كحساب محدد من قبل المستخدم.

[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
    }
}

مع تكوين

توضح هذه القصاصة البرمجية كيف يمكنك تعريف Configuration مع كتلة WindowsProcess موارد للتأكد من gpresult.exe عدم تشغيلها، وإيقافها كحساب محدد من قبل المستخدم.

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'
        }
    }
}