DSC配置

#配置Remote Desktop Services服务为 自启动,并运行

Configuration Myservice
{
# A Configuration block can have zero or more Node blocks
Node "localhost"
{
Service ServiceExample
{
Name = "TermService"
StartupType = "Automatic"
State = "Running"
}
}
}

Myservice

Start-DscConfiguration -wait -Verbose -Path .\Myservice

=============================================

#配置目录下文件

Configuration MyWebConfig
{
# A Configuration block can have zero or more Node blocks
Node "localhost"
{
# File is a built-in resource you can use to manage files and directories
# This example ensures files from the source directory are present in the destination directory
File MyFileExample
{
Ensure = "Present" # You can also set Ensure to "Absent"
Type = "Directory“ # Default is “File”
Recurse = $true
#SourcePath = $WebsiteFilePath # This is a path that has web files
SourcePath = "C:\inetpub\wwwroot"
DestinationPath = "C:\inetpub\wwwrootdes" # The path where we want to ensure the web files are present
#DependsOn = "[WindowsFeature]MyRoleExample" # This ensures that MyRoleExample completes successfully before this block runs
}
Log AfterDirectoryCopy
{
# The message below gets written to the Microsoft-Windows-Desired State Configuration/Analytic log
Message = "Finished running the file resource with ID DirectoryCopy"
DependsOn = "[File]MyFileExample" # This means run "DirectoryCopy" first.
}
}
}

MyWebConfig

 

Start-DscConfiguration -wait -Verbose -Path .\MyWebConfig

 

#带有参数的DSCConfiguration

Configuration MyParametrizedConfiguration
{
    # Parameters are optional
    param ($MyTargetNodeName, $MyGroupName)

    Node $MyTargetNodeName
    {
        # Group is a built-in resource that you can use to manage local Windows groups
        # This example ensures the existence of a group with the name specified by $MyGroupName
        Group MyGroupExample
        {
            Ensure = "Present" # Checks whether a group by this GroupName already exists and creates it if it does not
            Name = $MyGroupName
        }
    }
}  
MyParametrizedConfiguration -MyTargetNodeName "Server001" -MyGroupName "TestGroup" 

Start-DSCConfiguration MyParametrizedConfiguration -wait

PowerShell内置DSC资源:http://technet.microsoft.com/en-us/library/dn282120.aspx

 

=======================================================================

configuration dsc_service
{
param ($servicename)
Log dsc_service_log
{Message = "It's a message from DSC:Servicename = $servicename"}

Service ServiceExample
{
Name = $servicename
StartupType = "Automatic"
State = "Running"
}
}

#查看已定义的ConfigurationType
get-command -CommandType Configuration

get-command -CommandType Configuration|Remove-Item (删除)

 启用配置:

dsc_service -servicename Spooler(如果没有定义参数的话,则不需要加参数选项),默认配置文件保存到当前目录下,生成后可以将其移走

将配置推送到目标计算机:

Start-Dscconfiguration -Computername localhost .\dsc_service (由于未在配置中定义目标节点,所以此处使用computername参数作为目标计算机)

-Wait:等待配置完成

-Verbose:输出详细信息流到窗口

查看当前计算机已有的DSC配置(只有start-dscconfiguration之后才能get-dscconfiguration,此处为Message说明)

Get-DscConfiguration

 

查找DSC可用资源及其相应参数:

Get-DscResource

(Get-DscResource -Name File).properties|ft -Wrap

 

The Restore-DscConfiguration cmdlet restores the previous configuration for the node, if a previous configuration exists. Specify computers by using Common Information Model (CIM) sessions. If you do not specify a target computer, the cmdlet restores the configuration of the local computer.

$Session = New-CimSession –ComputerName "Server01" –Credential ACCOUNTS\PattiFuller
Restore-DscConfiguration -CimSession $Session
posted on 2014-02-28 17:04  momingliu11  阅读(910)  评论(0编辑  收藏  举报