当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 创建 DSC 配置模板

作者:精品下载站 日期:2024-12-14 07:42:30 浏览:14 分类:玩电脑

创建 DSC 配置模板


最近在休斯敦举行的 TechEd 期间,人们对所需状态配置 (DSC) 进行了很多讨论和兴奋。我不打算在这里讨论 DSC 本身,而是为那些刚刚开始使用的人解决一个小问题。当您构建配置时,如何确定要使用哪些资源设置?

首先,您可以使用 Get-DSCResource cmdlet,它将列出默认位置中的所有资源。

[玩转系统] 创建 DSC 配置模板

每种资源的属性也在那里,尽管您需要挖掘一点。

get-dscresource registry | Select -expand properties

[玩转系统] 创建 DSC 配置模板

从屏幕截图中,您可以看到哪些属性是必需的,以及其余属性的可能值。 Get-DSCResource cmdlet 甚至会更进一步,向您展示如何使用特定资源的完整语法。

get-dscresource registry -Syntax

[玩转系统] 创建 DSC 配置模板

要构建配置,您可以复制、粘贴和编辑。但我想要更多,就像我在涉及 PowerShell 时经常做的那样。因此,我编写了一个名为 New-DSCConfigurationTemplate 的函数,它将使用尽可能多的可用资源生成完整的配置。

#requires -version 4.0
#requires -module PSDesiredStateConfiguration

Function New-DSCConfigurationTemplate {

<#
.SYNOPSIS
Create a DSC configuration template
.DESCRIPTION
This command will create a DSC configuration template using any of the available DSC resources on the local computer. By default, it will create a configuration for all resources. The template will show all possible values for each set of resource properties. Mandatory property names are prefaced with a * , which you must delete.

If you don't specify a file path or to use the ISE, then the configuration will be written to the pipeline.
.PARAMETER Name
The name of the DSC resource
.PARAMETER UseISE
Open the template in the PowerShell ISE. You must be running this command in ISE or specify a path.
.PARAMETER Path
Save the file to the specified path. You can also opt to open the file in the ISE after creating it.
.EXAMPLE
PS C:\> New-DSCConfigurationTemplate File,Service,Registry -path d:\configs\template1.ps1 -useIse

Create a DSC configuration template for resources File, Service and Registry. The example code will save it to a file and then open the file in the PowerShell ISE.
.EXAMPLE
PS C:\> New-DSCConfigurationTemplate -useISE

Assuming this command is run in the ISE, it will create a configuration template using all DSC resources on the local computer and open it in the ISE as an untitled and unsaved file.
.LINK
Get-DSSResource
.LINK
https://jdhitsolutions.com/blog/2014/05/creating-a-dsc-configuration-template
.NOTES
Last Updated: May 17, 2014
Version     : 0.9
Author      : @JeffHicks

Learn more:
 PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones2/)
 PowerShell Deep Dives (http://manning.com/hicks/)
 Learn PowerShell in a Month of Lunches (http://manning.com/jones3/)
 Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
 
"Those who forget to script are doomed to repeat their work."

  ****************************************************************
  * DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
  * THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK.  IF   *
  * YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
  * DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.             *
  ****************************************************************
#>

[cmdletbinding()]
Param(
[parameter(Position=0,ValueFromPipeline=$True)]
[ValidateNotNullorEmpty()]
[string[]]$Name="*",
[switch]$UseISE,
[string]$Path
)

Begin {
    Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"  
    $template=@"
#requires -version 4.0

Configuration MyDSCTemplate {

#Settings with a * are mandatory. Delete the *.
#edit and delete resource properties as necessary

Node COMPUTERNAME {

"@

} #begin

Process {


foreach ($item in $name) {
Write-Verbose "Getting resource $item "
$resources = Get-DscResource -Name $item

    foreach ($resource in $resources) {
[string[]]$entry = "`n$($resource.name) <ResourceID> {`n"

    Write-Verbose "Creating resource entry for $($resource.name)"
    $entry+=  foreach ($item in $resource.Properties) {
     if ($item.IsMandatory) {
       $name="*$($item.name)"
     }
     else {
     $name = $item.name
     }

     if ($item.PropertyType -eq '[bool]') {
       $possibleValues = "`$True | `$False"
     }
    elseif ($item.values) {
      $possibleValues = "'$($item.Values -join "' | '")'"
     }
    else {
      $possibleValues=$item.PropertyType
    } 
    "$name = $($possibleValues)`n"

    } #foreach
 $entry+="} #end $($resource.name) resource`n`n"
 #add the resource listing to the template
 $template+=$entry
}
} #foreach item in $name

} #process

End {

Write-Verbose "closing template"
$template+=@"
 } #close node

} #close configuration

"@

if ($path) {
Write-Verbose "Saving template to $path"
  Try {
    $template | Out-File -FilePath $path -ErrorAction Stop
    if ($UseISE) {
        Write-Verbose "Opening $path in the ISE"
        ise $path
    }
  }
  Catch {
    Throw $_
  }
}
elseif ($UseISE -And ($host.name -match "PowerShell ISE")) {
    Write-Verbose "Creating a new ISE PowerShell tab"
    $new = $psise.CurrentPowerShellTab.Files.Add()
    Write-Verbose "Inserting template into a new tab"
    $new.Editor.InsertText($template)
}
elseif ($UseISE -And ($host.name -notmatch "PowerShell ISE")) {    
        Write-Warning "Could not open template in the ISE. Are you in it? Otherwise, specify a path or run this in the ISE."
  }
else {
    Write-Verbose "Writing template to the pipeline"
    $template
}

    #All finished
    Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end

} #end function

该函数将使用您指定的任何资源创建一个配置模板。

[玩转系统] 创建 DSC 配置模板

New-DSCConfigurationTemplate -Path c:\scripts\DSCConfigTemplate.ps1 -UseISE

[玩转系统] 创建 DSC 配置模板

我写这篇文章的假设是我可以运行一次来创建模板,然后将我需要的内容复制并粘贴到新配置中。另一种选择是为这些资源创建 ISE 片段。

就我个人而言,任何可以节省我打字时间的事情都是有很大帮助的。我希望你能让我知道你的想法。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯