Aan de slag met Desired State Configuration (DSC) voor Windows
In dit onderwerp 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-besturingssystemen
De volgende versies worden ondersteund:
- Windows Server 2022
- Windows Server 2019
- Windows Server 2016
- Windows Server 2012R2
- Windows Server 2012
- Windows Server 2008 R2 SP1
- Windows 11
- Windows 10
- Windows 8.1
- Windows 7
De SKU van het zelfstandige Microsoft Hyper-V Server-product bevat geen implementatie van Desired State Configuration en kan dus niet worden beheerd door 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 uit 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 computer
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 eenvoudig correct wilt configureren, voert u uit Set-WsManQuickConfig -Force
in een PowerShell-terminal met verhoogde bevoegdheid.
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 geschreven naar Windows-gebeurtenislogboek in het pad Microsoft-Windows-Dsc/Operational
.
Aanvullende logboeken voor foutopsporing kunnen worden ingeschakeld door de stappen in Where Are DSC-gebeurtenislogboeken te volgen.