قالب وردپرس درنا توس
Home / Tips and Tricks / It's easier than you think – CloudSavvy IT

It's easier than you think – CloudSavvy IT



  Powershell Logo

Have you ever thought about trying DSC but found the learning curve to be too steep? Or maybe you never saw yourself using it in production, so you didn't bother?

Using DSC doesn't have to be difficult or complex, and you don't need a massive infrastructure to make it worthwhile. This article will talk about DSC and how to take the first steps, and it includes examples of how to get started by putting it into production on a small scale.

What is DSC?

Desired State Configuration (DSC) is a configuration management system built into Windows that allows you to configure your system as a code. It uses a configuration written in PowerShell that is later converted to a Managed Object Format (MOF).

The MOF file is later read and applied by the Local Configuration Manager Service to the target node (computer).

The configuration created and executed uses different DSC sources from DSC modules. DSC modules are like a regular PowerShell module and its resources are its functions. For example, you can install them with Install-Module and they are stored under the PSModulePath.

What can I use DSC for?

The hardest part about DSC for many is to find use cases for it. People tend to only talk and write about DSC when it comes to orchestrating and running large environments, or testing and QA for different apps, which is a distant topic for many administrators.

We can use DSC for our daily routines and much more, without having to restructure our entire organization. I have compiled a list of a few areas that you can automate in whole or in part using DSC:

Example: Reading and writing a DSC configuration

A few lines of DSC can be equal to hundreds of lines with raw PowerShell, that is why it can be so incredibly powerful.

A DSC configuration mainly consists of 2 parts: the DSC configuration and the configuration data.

The configuration data (explained below) can either be included externally, usually from a PSD file or included in the configuration file. The recommended and most maintainable approach is to include this from a separate file.

The following is an example of how to create a DSC MOF that downloads and installs a generic MSI when applied:

  Configuration MyDSCConfiguration {
# Import DSC modules with resources for use in the configuration
Import-DscResource -ModuleName PSDesiredStateConfiguration
Import-DscResource -ModuleName xPSDesiredStateConfiguration -ModuleVersion "8.1
0.0.0" ############################################## # Creates configuration for the server fileserver-1 ############################################## Node "fileserver-1" { # Create C: ProgramData SoftwarePackages to download files to all nodes File Software Packages Packages { # Identifier "SoftwarePackagesDirectory" using the DSC source "File" from DSC module "PSDesiredStateConfiguration" DestinationPath = "C: ProgramData SoftwarePackages" Type = "Directory" # Is a directory Sure = "Present" # Make sure it exists } # Download MSI xRemoteFile DownloadBackupAgent { # The xRemoteFile source of xPSDesiredStateConfiguration DSC module used to download an MSI over HTTP DependsOn = "[File] SoftwarePackagesDirectory" # Tells DSC not to run if previous step is not completed Uri = "http://web.contoso.com/packages/backupagent.msi" # Where to download the external file from DestinationPath = "C: ProgramData SoftwarePackages BackupAgent.msi" # Where to save the downloaded file } # Install MSI xMsiPackage InstallBackupAgent { # The xMsiPackage source of xPSDesiredStateConfiguration DSC module used to install the MSI we just downloaded DependsOn = "[xRemoteFile] DownloadBackupAgent" # Depends if the previous step is complete before starting ProductId = "d29c3fa9-e03e-40e0-a6ed-556c6f05476a" # The MSI package ProductID Path = "C: ProgramData SoftwarePackages BackupAgent.msi" # Path to the MSI file we just downloaded Make sure "present" # Make sure it is present (install the MSI), & # 39; Make = "Absent" & # 39; would remove the MSI } } }

After this code is executed, a configuration function is created in the current PowerShell session that can be used. It creates a subfolder with the same name as the configuration where it stores the generated MOF files:

  PS51> MyDSCConfiguration

Directory: C:  Tutorials  DSC  MyDSCConfiguration


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a ---- 2019-10-28 16:38 3952 fileserver-1.mof 

You can apply all DSC configurations in the MyDSCConfiguration folder to their host by running:

  PS51> Start-DscConfiguration -Path.  MyDSCConfiguration -Wait 

Using DSC with ConfigurationData

Configuration data contains configuration data for the DSC configuration. The information contains a node name (computer name) and may contain other information such as information about installing an MSI package or registry key values, etc. This gives us a dynamic and more versatile approach to DSC so that it can be reused and easily changed .

Suppose you have these three servers, each of which requires a unique combination of packages:

  • fileserver-1
  • database-1
    • Needs: BackupAgent, BackupAgent-DatabaseAddon
  • appserver-1
    • Needs: BackupAgent, MonitoringAgent

The best approach is to create a file with the configuration information you need, not only by providing the nodes (servers), but also a list of packages to it add:

  # Save as ConfigurationData.psd1

@ {
#############################
# Specify server nodes
#############################
AllNodes = @ (
@ {
NodeName = "*"
}

# File server node
@ {
NodeName = "fileserver-1"
Role = "Fileserver"

# Selects which packages to install
Packages = @ (
& # 39; BackupAgent & # 39;
)
},

# Database server node
@ {
NodeName = "database-1"
Role = "Database"

# Selects which packages to install
Packages = @ (
& # 39; BackupAgent & # 39 ;,
& # 39; BackupAgent-DataBaseAddon & # 39;
)
},

# Appserver node
@ {
NodeName = "appserver-1"
Role = "Appserver"

# Selects which packages to install
Packages = @ (
& # 39; BackupAgent & # 39 ;,
& # 39; MonitoringAgent & # 39;
)
}
)

#############################
# Define each package
#############################
Packages = @ (
@ {
Name = "BackupAgent"
ProductId = "3c4e21b5-8de2-408e-84e0-a42b0507b8b1"
FileName = "BackupAgent.msi"
Sure = "Present"
},
@ {
Name = "BackupAgent-DataBaseAddon"
ProductId = "97f860f6-0a58-4bf3-aef8-abc0f796e9df"
FileName = "BackupAgent-DataBaseAddon.msi"
Sure = "Present"
},
@ {
Name = "MonitoringAgent"
ProductId = "77b29b12-c222-4d85-b812-bbd4136ad0ff"
FileName = "MonitoringAgent.msi"
Sure = "Present"
}
)
} 

You can now use this configuration data file to dynamically generate DSC configurations for your servers:

  Configuration MyDSCConfiguration {
# Import DSC modules with resources for use in the configuration
Import-DscResource -ModuleName PSDesiredStateConfiguration
Import-DscResource -ModuleName xPSDesiredStateConfiguration -ModuleVersion "8.10.0.0"

# Retrieve packages from the ConfigurationData
$ Packages = $ ConfigurationData.Packages

##############################################
# Applies to all nodes
##############################################
Node $ AllNodes.NodeName {

# Create C:  ProgramData  SoftwarePackages to download files to all nodes
File Software Packages Packages {
DestinationPath = "C:  ProgramData  SoftwarePackages"
Type = "Directory" # Is a directory
Sure = "Present" # Make sure it exists
}

# Start dynamically generating DSC configuration by going through all package names in Node.Packages
Foreach ($ PackageName in $ Node.Packages) {

$ Package = $ Packages. Where {$ _. Name -eq $ PackageName}

xRemoteFile "PackageDownload - $ ($ Package.Name)" {
DependsOn = "[File] SoftwarePackagesDirectory"
DestinationPath = "C:  SoftwarePackages  $ ($ Package.FileName)"
Uri = "http://web.contso.com/packages/$($Package.FileName)"
}

xMsiPackage "Install $ ($ Package.Name)" {
DependsOn = "[xRemoteFile] PackageDownload - $ ($ Package.Name)"
ProductId = $ Package.ProductId
Path = "C:  ProgramData  SoftwarePackages  $ ($ Package.FileName)"
Make sure = $ Package
}
}
}
} 

When you run this, the DSC function generates an MOF file for each of your servers that can be used to install the packages you defined in the ConfigurationData.psd1 file:

  PS51> MyDSCConfiguration -ConfigurationData.  ConfigurationData.psd1


Directory: C:  Tutorials  DSC  MyDSCConfiguration


Mode LastWriteTime Length Name
---- ------------- ------ ----
-a ---- 2019-10-28 17:25 3962 fileserver-1.mof
-a ---- 2019-10-28 17:25 6012 database-1.mof
-a ---- 2019-10-28 17:25 5894 appserver-1.mof

# Applies the configuration to the servers
PS51> Start-DscConfiguration -Path.  MyDSCConfiguration -Wait 

If you want to add new packages, please edit your ConfigurationData.psd1 file and run the previous code again.

Conclusion [19659005] It can be a fairly short and steep learning curve to get started with DSC. But the hardest part for many is to find usage scenarios where to use it.

Using the previous method that uses HTTP (or HTTPS) to retrieve packets, you have a versatile way to distribute software to your servers. And since it is HTTPS, it is also easier to unmask and you can use it to manage software in DMZ and other less trusted networks.

In addition, using configuration data instead of hard coding everything in your DSC configurations gives you a much easier to maintain approach. You can even generate this configuration data from external sources like CSV, SQL, Sharepoint and everything else that can be read with PowerShell.

I hope you've got some ideas for your environment to make your daily tasks easier with DSC.


Source link