本文共 3877 字,大约阅读时间需要 12 分钟。
DSC( Desired State Configuration)是微软的一款强大的配置管理工具,主要用于自动化配置和管理计算机资源。在这一节中,我们将简单介绍DSC的三个核心功能:参数化配置文件、账号加密以及设置多个服务的安装顺序。
配置文件是DSC的核心基石,它本质上可以看作一个函数。配置文件可以调用其他函数,并且可以通过参数化的方式重复使用各种数值。默认情况下,配置文件有三个主要参数:
为了更好地管理和复用参数,DSC允许使用param关键字来定义参数。例如,可以通过以下配置定义一个需要计算机名称和指导ID的参数化配置文件:
[DSCLocalConfigurationManager()]Configuration LCM_HTTPPULL{ param ( [Parameter(Mandatory=$true)] [string[]]$ComputerName, [Parameter(Mandatory=$true)] [string]$guid ) Node $ComputerName { Settings { AllowModuleOverwrite = $True ConfigurationMode = 'ApplyAndAutoCorrect' RefreshMode = 'Pull' ConfigurationID = $guid } ConfigurationRepositoryWeb PullServer { Name = 'PullServer' ServerURL = 'http://dc.company.pri:8080/PSDSCPullServer.svc' AllowUnsecureConnection = $true } }}# 生成计算机列表$ComputerName = 's1', 's2'# 创建 GUID$guid = [guid]::NewGuid()# 创建 Computer.Meta.Mof 并输出-ComputerName $ComputerName -Guid $guid -OutputPath c:\DSC\HTTP# 推送配置LCMSet-DSCLocalConfigurationManager -ComputerName $computername -Path c:\DSC\HTTP –Verbose 在实际应用中,账号配置需要高度安全。直接暴露密码或明文账号是非常不安全的。以下是一个不安全的示例:
Configuration DirTest{ param ( [Parameter(Mandatory=$true)] [string[]]$ComputerName, [pscredential]$credential ) Node $computerName { File DirTest1 { DestinationPath = 'c:\DirTest' Type = 'Directory' Ensure = 'Present' Credential = $Credential } }}Dirtest -computername sydittest -Credential (Get-Credential) -ConfigurationData C:\Scripts\DSC1\Mod6\2a.config_data.psd1 -OutputPath c:\DSCSecure@{ AllNodes = @( @{ NodeName = 'sydittest' PSDscAllowPlainTextPassword = $True } )} 在这个示例中,密码被直接暴露在配置文件中,存在安全隐患。为了解决这个问题,可以采取以下安全措施:
例如,修改后的安全配置文件如下:
Configuration DirTest{ param ( [Parameter(Mandatory=$true)] [string[]]$ComputerName, [pscredential]$credential ) Node $computerName { File DirTest1 { DestinationPath = 'c:\DirTest' Type = 'Directory' Ensure = 'Present' Credential = $Credential } }}Dirtest -computername sydittest -Credential (Get-Credential) -ConfigurationData C:\Scripts\DSC1\Mod6\2b.config_data.psd1 -OutputPath c:\DSCSecure@{ AllNodes = @( @{ NodeName = 'sydittest' CertificateFile = 'c:\temp\sydittest.cer' } )} 在安装多个服务时,可能会存在依赖关系。DSC提供了DependsOn关键字来定义这些依赖关系。默认情况下,DSC会随机安装服务,这种行为可以防止过度依赖关系导致的环境变化问题。
例如,安装IIS和其管理界面时,可以通过DependsOn确保IIS先安装完成:
Configuration InstallIIS{ Node sydittest { WindowsFeature IIS { Ensure = 'Present' Name = 'web-server' } WindowsFeature IISMgmt { Ensure = 'Present' Name = 'web-Mgmt-Service' DependsOn = "[WindowsFeature]IIS" } WindowsFeature IISConsole { Ensure = 'Present' Name = 'web-mgmt-console' DependsOn = "[WindowsFeature]IIS" } File DefaultWebSite { Ensure = 'Present' Type = 'Directory' Force = $True Recurse = $True SourcePath = 'c:\sites\inetpub\wwwroot' DestinationPath = 'C:\inetpub\wwwroot' DependsOn = "[WindowsFeature]IIS" } }}InstallIIS -OutputPath C:\DSC\mod6Start-DscConfiguration -ComputerName sydittest -Path C:\dsc\mod6 -wait -verbose -force 通过合理使用DependsOn关键字,可以确保服务安装的正确顺序,避免因依赖关系问题导致的配置失败。
DSC通过参数化配置文件、支持证书加密账号以及定义服务安装顺序,提供了强大的配置管理功能。合理使用这些功能,可以显著提升配置管理的安全性和可靠性。
转载地址:http://flxfk.baihongyu.com/