博客
关于我
Powershell DSC 5.0 - 参数,证书加密账号,以及安装顺序
阅读量:797 次
发布时间:2023-03-04

本文共 3877 字,大约阅读时间需要 12 分钟。

DSC( Desired State Configuration)是微软的一款强大的配置管理工具,主要用于自动化配置和管理计算机资源。在这一节中,我们将简单介绍DSC的三个核心功能:参数化配置文件、账号加密以及设置多个服务的安装顺序。

1. 参数化配置文件

配置文件是DSC的核心基石,它本质上可以看作一个函数。配置文件可以调用其他函数,并且可以通过参数化的方式重复使用各种数值。默认情况下,配置文件有三个主要参数:

  • OutputPath:MOF文件的输出路径。
  • ConfigurationData:配置文件的参数数据,通常以哈希表形式存在。
  • InstanceName:实例名称,通常可以默认值。

为了更好地管理和复用参数,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

2. 安全账号配置

在实际应用中,账号配置需要高度安全。直接暴露密码或明文账号是非常不安全的。以下是一个不安全的示例:

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        }    )}

在这个示例中,密码被直接暴露在配置文件中,存在安全隐患。为了解决这个问题,可以采取以下安全措施:

  • 使用证书:通过公司的PKI服务创建客户端证书,并将证书导出到安全的存储位置。
  • 加密配置文件:在配置文件中指定证书文件路径,确保密码加密存储。
  • 例如,修改后的安全配置文件如下:

    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'        }    )}

    3. 服务安装顺序

    在安装多个服务时,可能会存在依赖关系。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/

    你可能感兴趣的文章
    pm2通过配置文件部署nodejs代码到服务器
    查看>>
    PML调用PDMS内核命令研究
    查看>>
    PMM安装-第一篇
    查看>>
    PMP知识要点(第九章)
    查看>>
    PNETLab 镜像包官方下载太慢?不急,最新版本PNET_4.2.10分享!
    查看>>
    POCO库中文编程参考指南(4)Poco::Net::IPAddress
    查看>>
    Quartz基本使用(二)
    查看>>
    POC项目安装与使用指南
    查看>>
    Podman核心技术详解
    查看>>
    pods 终端安装 第三方框架的一些命令
    查看>>
    Podzielno
    查看>>
    PoE、PoE+、PoE++ 三款交换机如何选择?一文带你了解
    查看>>
    PoE三种标准:标准 PoE、PoE+、PoE++,网络工程师必知!
    查看>>
    POI 的使用
    查看>>
    poi 读取单元格为null者空字符串
    查看>>
    poi-tl简介与文本/表格和图片渲染
    查看>>
    pointnet分割自己的点云数据_PointNet解析
    查看>>
    POI实现Excel导入Cannot get a text value from a numeric cell
    查看>>
    POI实现Excel导入时提示NoSuchMethodError: org.apache.poi.util.POILogger.log
    查看>>
    POI实现Excel导出时常用方法说明
    查看>>