Zum Inhalt springen

Benutzer:MovGP0/Powershell/DSC

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 26. August 2016 um 13:50 Uhr durch MovGP0 (Diskussion | Beiträge) (Configuration). Sie kann sich erheblich von der aktuellen Version unterscheiden.
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      

Powershell Desired State Configuration (DSC)

Überblick

  • Basiert auf offenem Standard
  • Deklarative Konfiguration
  • Ablauf
    1. Authoring: Imperative und Deklarative PS-Befehle erzeugen .MOF-Dateien
    2. Staging: Konfiguration wird für Client berechnet
    3. „Make it so“: Deklarative Konfiguration wird durch DSC-Provider umgesetzt

Vorraussetzungen

  • Powershell 4.0+
    • Windows 7 SP1+
    • Windows Server 2008 R2 SP1+
    • Linux[1]
  • Windows Management Framework 4.0+
    • Common Information Model (CIM) Namespace
      Get-CimClass -Namespace Root\Microsoft\Windows\DesiredStateConfiguration
      
    • DSC cmdlets, providers, resources
      $env:PSModulePath
      C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\PSDesiredStateConfiguration
      
  • .NET Framework 4.5+
Remoting
  • Adminstrator Cedentials
  • PowerShell Remoting enabled [2]
    Enable-PSRemoting -Force
    
  • Remote management enabled [2]
    # Admin
    Start-Service WinRM
    winrm set winrm/config/client '@{TrustedHosts="RemoteComputer1,RemoteComputer2"}'
    
  • Empfohlen
    • Domain System (easier remote management)
    • Public Key Infrastructure (PKI) for SSL and encryption

Commands

Get-Command -Module PSDesiredStateConfiguration

DSC Resources

# get all DSC resources
Get-DSCResource

# Get (configurable) properties for specific resource
Get-DSCResource -Name 'File' | Select-Object -ExpandProperty 'Properties'

# Get configuration syntax template
Get-DSCResource -Name 'File' -Syntax

# Get information for specific resource
Get-DSCResource -Name 'Service' | Select-Object *
Standard Resource Providers
Provider Beschreibung ab Version
File Dateien und Verzeichnisse 4.0
SignatureValidation 5.0
Archive Entpacken von ZIP Dateien 4.0
Environment Umgebungsvariablen 4.0
Group Benutzergruppen 4.0
GroupSet 5.0
Log Logging 4.0
Package führt Windows Installer und setup.exe Dateien aus 4.0
ProcessSet 5.0
Registry Verwaltet Registry-Keys 4.0
Script Führt Powershell-Skript aus 4.0
Service Verwaltet Hintergrunddienste 4.0
ServiceSet 5.0
User Verwaltet Benutzerkonten 4.0
WaitForAll 5.0
WaitForAny 5.0
WaitForSome 5.0
WindowsFeature Windows features und Serverrollen 4.0
WindowsFeatureSet 5.0
WindowsOptionalFeature 5.0
WindowsOptionalFeatureSet 5.0
WindowsPackageCab 5.0
WindowsProcess Verwalten von Windows-Prozessen 4.0
Resource Wave
Community Extensions

Custom DSC Resources

Resourcen sind Module mit 3 Funktionen

  • Get-TargetResource
  • Set-TargetResource
  • Test-TargetResource

Anmerkung: Module müssen

  • im Modul-Pfad $env:PSModulePath hinterlegt werden
  • den gleichen Namen wie der Ordner haben (./MyModule/MyModule.psm1)

Deployment

  • Push-Model
    Start-DscConfiguration
    
  • Pull-Model
    • Server gets configuration from HTTP/HTTPS server or SMP file share

Configuration

MyConfiguration.ps1
Configuration MyConfiguration 
{
    # Import-DSCResource is a keyword and not a PS-command
    Import-DSCResource -Module @('WindowsFeature','File','RemoteRegistry')

    Node Computername {
        WindowsFeature Backup {
            Name="Windows-Server-Backup"
            Ensure="Present"
        }
        File Development {
            Type="Directory"
            Ensure="Present"
            DestinationPath="C:\Development"
        }
        Service RemoteRegistry {
            Name="RemoteRegistry", 
            Startup="Automatic"
            State="Running"
        }
    }

    Node @("Server1", "Server2") {
        # ...
    }
}
Execute
# Load configuration 
. .\MyConfiguration.ps1
# Create MOF file
MyConfiguration
# Execute configuration on computer
Start-DscConfiguration -Path .\MyConfiguration

ConfigurationData

MyConfiguration.psd1
$ConfigurationData = @{
    AllNodes = @( # required key
        @{NodeName = "FP-123"; Role = "FilePrint"}, 
        @{NodeName = "WS-421"; Role = "WebServer"}, 
        @{NodeName = "*"; Features = "Windows-Server-Backup","PowerShell-V4"}
    );
    MyData = @{ # optional arbitrarily named key
        MyData = Import-CSV "C:\Development\Translations.csv"
    }
}
MyConfiguration.ps1
Configuration MyConfiguration 
{
    Node $AllNodes.Nodename {
        $Node.features.foreach({
            WindowsFeature $_ { 
                Name = $_
                Ensure = "Present"
            }
        })

        Service RemoteRegistry {
            Name = "RemoteRegistry", 
            StartupType = "Automatic"
            State = "Running"
        }
    }
}
Create/Validate MOF

PartialConfiguration

[DSCLocalConfigurationManager()]
Configuration PartialConfigDemo 
{
    Node localhost 
    {
        PartialConfiguration ServiceAccountConfig 
        {
            Description = 'Configuration to add the SharePoint service account to the Administrators group.'
            RefreshMode = 'Push'
        }
        PartialConfiguration SharePointConfig 
        {
            Description = 'Configuration for the SharePoint server'
            RefreshMode = 'Push'
        }
    }
}
PartialConfigDemo

Quellen

  1. Eric Slesar: Get started with Desired State Configuration (DSC) for Linux. In: MSDN. Microsoft, 22. August 2016, abgerufen am 26. August 2016 (englisch).
  2. a b Chris B. Hoffman: How to Run PowerShell Commands on Remote Computers. How-To Geek, 22. Juni 2012, abgerufen am 26. August 2016 (englisch).