标签:poweshell function dsc parameter dependson certificate
这一节主要是简单的过一下DSC的3个基本功能,如何参数化配置文件,加密账号,以及如何设置多个服务的安装顺序。
Configuration file(配置文件)本质就是function(函数),函数可以调用另一个函数,还可以参数化很多数值以便重复使用,配置文件也一样。
配置文件自己默认有3个参数:
OutputPath, MOF文件的输出路径
ConfigurationData,这个是参数的配置文件,结构是哈希表
InstanceName,实例名,一般默认即可
我们也可以通过param关键字来定义,例如
[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
}
}
}
# Computer list
$ComputerName=‘s1‘, ‘s2‘
# Create Guid for the computers
$guid=[guid]::NewGuid()
# Create the Computer.Meta.Mof in folder
LCM_HTTPPULL -ComputerName $ComputerName -Guid $guid -OutputPath c:\DSC\HTTP
# Explorer c:\DSC\HTTP
# Send to computers LCM
Set-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
# Send to computers LCM
Start-DscConfiguration -ComputerName sydittest -Path C:\DSCSecure –Verbose@{
AllNodes = @(
@{
NodeName=‘sydittest‘
PSDscAllowPlainTextPassword=$True
}
)
}可以看见是明文的,很不安全
下面是安全的做法
首先生成一个证书,豆子已经安装了PKI,所以从MMC里面打开很容易就可以创建一个新的客户端证书,然后导出来
配置文件,注意这个哈希表里面和前面不一样了
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
# Send to computers LCM
Start-DscConfiguration -ComputerName sydittest -Path C:\DSCSecure –Verbose@{
AllNodes = @(
@{
NodeName=‘sydittest‘
CertificateFile = ‘c:\temp\sydittest.cer‘
}
)
}可以看见密码已经加密了
最后简单的看看dependon的关键字。当我们安装多个服务的时候,有时候会存在依赖关系。比如我要安装一个集群,但是安装之前我要确保Domain服务已经安装了。这种依赖关系可以通过dependon来定义。请注意,DSC默认的规则是随机安装,因为他不希望存在过度的依赖关系,这样一旦环境发生变化,可能导致整个配置失败。
例如,我需要安装IIS,然后再安装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‘
}
File DefaultWebSite {
Ensure = "Present"
Type = "Directory“ # Default is “File”
Force = $True
Recurse = $True
SourcePath = "c:\sites\inetpub\wwwroot\"
DestinationPath = "C:\inetpub\wwwroot\"
DependsOn = "[WindowsFeature]IIS"
}
}
}
InstallIIS -OutputPath C:\DSC\mod6
Start-DscConfiguration -ComputerName sydittest -Path c:\dsc\mod6 -wait -verbose -force本文出自 “麻婆豆腐” 博客,请务必保留此出处http://beanxyz.blog.51cto.com/5570417/1699083
Powershell DSC 5.0 - 参数,证书加密账号,以及安装顺序
标签:poweshell function dsc parameter dependson certificate
原文地址:http://beanxyz.blog.51cto.com/5570417/1699083