Aan de slag met Desired State Configuration (DSC) voor Windows

In dit artikel wordt uitgelegd hoe u aan de slag gaat met PowerShell Desired State Configuration (DSC) voor Windows. Zie Aan de slag met Windows PowerShell Desired State Configuration voor algemene informatie over DSC.

Ondersteunde versies van Windows-besturingssysteem

De volgende versies worden ondersteund:

  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • Windows 11
  • Windows 10

Het zelfstandige product van Microsoft Hyper-V Server bevat geen implementatie van Desired State Configuration, dus u kunt het niet beheren met PowerShell DSC of Azure Automation State Configuration.

DSC installeren

PowerShell Desired State Configuration is opgenomen in Windows en bijgewerkt via Windows Management Framework. De nieuwste versie is Windows Management Framework 5.1.

Notitie

U hoeft de Windows Server-functie DSC-Service niet in te schakelen om een machine te beheren met behulp van DSC. Deze functie is alleen nodig bij het bouwen van een Windows Pull Server-exemplaar.

DSC voor Windows gebruiken

In de volgende secties wordt uitgelegd hoe u DSC-configuraties maakt en uitvoert op Windows-computers.

Een MOF-document voor configuratie maken

Het trefwoord Windows PowerShell Configuration wordt gebruikt om een configuratie te maken. In de volgende stappen wordt beschreven hoe u een configuratiedocument maakt met behulp van Windows PowerShell.

Een module installeren die DSC-resources bevat

Windows PowerShell Desired State Configuration bevat ingebouwde modules met DSC-resources. U kunt ook modules van externe bronnen zoals de PowerShell Gallery laden met behulp van de PowerShellGet-cmdlets.

Install-Module 'PSDscResources' -Verbose

Definieer een configuratie en genereer het configuratiedocument:

Configuration EnvironmentVariable_Path
{
    param ()

    Import-DscResource -ModuleName 'PSDscResources'

    Node localhost
    {
        Environment CreatePathEnvironmentVariable
        {
            Name = 'TestPathEnvironmentVariable'
            Value = 'TestValue'
            Ensure = 'Present'
            Path = $true
            Target = @('Process', 'Machine')
        }
    }
}

EnvironmentVariable_Path -OutputPath:"./EnvironmentVariable_Path"

De configuratie toepassen op de machine

Notitie

Als u WILT toestaan dat DSC wordt uitgevoerd, moet Windows worden geconfigureerd voor het ontvangen van externe PowerShell-opdrachten, zelfs wanneer u een localhost configuratie uitvoert. Als u uw omgeving correct wilt configureren, hoeft u alleen Set-WsManQuickConfig -Force maar een PowerShell-terminal met verhoogde bevoegdheid te gebruiken.

U kunt configuratiedocumenten (MOF-bestanden) toepassen op een computer met de cmdlet Start-DscConfiguration .

Start-DscConfiguration -Path 'C:\EnvironmentVariable_Path' -Wait -Verbose

De huidige status van de configuratie ophalen

De cmdlet Get-DscConfiguration voert een query uit op de huidige status van de machine en retourneert de huidige waarden voor de configuratie.

Get-DscConfiguration

De cmdlet Get-DscLocalConfigurationManager retourneert de huidige metaconfiguratie die op de computer is toegepast.

Get-DscLocalConfigurationManager

De huidige configuratie van een computer verwijderen

Het document Remove-DscConfiguration

Remove-DscConfigurationDocument -Stage Current -Verbose

Instellingen configureren in Lokale Configuration Manager

Pas een Meta Configuration MOF-bestand toe op de computer met behulp van de cmdlet Set-DSCLocalConfigurationManager . Vereist het pad naar de metaconfiguratie-MOF.

Set-DSCLocalConfigurationManager -Path 'c:\metaconfig\localhost.meta.mof' -Verbose

Windows PowerShell Desired State Configuration logboekbestanden

Logboeken voor DSC worden naar het Microsoft-Windows-Dsc/Operational Windows-gebeurtenislogboek geschreven. U kunt andere logboeken inschakelen voor foutopsporing door de stappen in Where Are DSC-gebeurtenislogboeken te volgen.