【问题标题】:Create DSC Configuration dynamically动态创建 DSC 配置
【发布时间】:2016-08-26 15:08:11
【问题描述】:

TLDR;动态创建 DSC 配置文件的最佳方式是什么?

我的任务是维护一个复杂的文件夹结构,包括权限。目前这是通过自定义 PowerShell 模块完成的。对文件夹结构本身进行更改时会出现问题。

使用 DSC 可以消除合规方面的问题。手动为 20k 文件夹生成 DSC 配置是绝对不可能的。我想通过 PowerShell 从一些输入创建 DSC 配置。这样一来,可以及时引入更改,并在审核 DSC 配置后应用。

还是我完全走错了路,我只能从 DSC 配置中的输入生成结构?

【问题讨论】:

  • 我有一个类似的挑战,我有一个由几种不同资源类型组成的系统,并且想从正在运行的系统创建一个 DSC 配置脚本。我以前(大约五年前)编写过这个脚本,这很糟糕。想知道现在是否有任何帮助功能,类似于 DSC 资源设计器,但用于 DSC 配置项。有人知道这样的事情吗?

标签: powershell ntfs dsc


【解决方案1】:

当您编写 DSC 配置时,它是一个在设计时执行以最终生成 MOF 文件的脚本。所以你可以这样做:

Configuration Folders {

    Get-Content 'myfolderlist.txt' | ForEach-Object {

        File $($_ -replace '\\','_')
        {
            DestinationPath = $_
            Ensure = "Present"
        }
    }
}

这并不涉及权限,但它显示了如何在 DSC 配置中使用循环。这里要记住的重要一点是,这将在设计时生成一个具有 20k File 资源的静态配置 (MOF) 文件。当 DSC 运行时,循环不会运行(也根本不存在)。

DSC 并不是最快的东西。在 20,000 个资源上进行测试/设置可能真的很慢,而且有些资源密集。我觉得这可能不是这项工作的工具。

或者,您可以创建一个自定义 DSC 资源来执行测试和设置文件夹结构和权限的所有逻辑,这样它就可以在一个资源中完成。

基本上,这是一个美化的计划任务,但这可能没问题,特别是如果您想在更广泛的意义上使用 DSC。如果您想看一看,有很多关于如何创建自定义资源的文章(和书籍)。

【讨论】:

  • 谢谢,这是缺少的链接。我将报告我的测试环境对事物速度的看法。
【解决方案2】:

这不是很漂亮,但我为 NTFS 权限做了下面的事情,如果你没有设置子文件夹访问权限等,你可能需要扩展。我没有看到动态创建配置的简单方法,所以我重新调整用途不同的参数集。显然这是 5 年后的事了,所以你可能想出了一些东西。顶部的开关基本上是替换节点定义文件中的变量。

        Function NtfsPermissions
        {
            Param (
                [Parameter(Mandatory=$true)]
                [ValidateSet("Present","Absent")]
                [string]$Ensure,
                [Parameter(Mandatory=$true)]
                [string]$Account,
                [Parameter(Mandatory=$true)]
                [string]$Path,
                [string[]]$FileSystemRights,
                [string]$Inheritance,
                [string]$Depends
            )
        #Switches are used to dynamically replace accounts and paths that can't be set in nodedefinition file
            switch ($Account)
            {
                "SQLAGENT"
                {
                    $Account = $Node.gSqlAgt
                    break
                }
                "SQLSVC"
                {
                    $Account = $Node.gSqlSvc
                    break
                }
                "SQLIS"
                {
                    $Account = $Node.gSqlIs
                    break
                }
            }
            switch ($Path)
            {
                "AuditPath"
                {
                    $Path = $Node.AuditPath
                    break
                }
                "LogDir"
                {
                    $Path = $Node.LogDir
                    break
                }
                "DataDir"
                {
                    $Path = $Node.DataDir
                    break
                }
                "TempdbDir"
                {
                    $Path = $Node.TempdbDir
                    break
                }
            }
            if ($Ensure -ne "Absent")
            {
                cNtfsPermissionEntry $($Account + $Path.Replace(':','_'))
                {
                    Ensure = $Ensure
                    Path = $Path
                    Principal = $Account
                    AccessControlInformation = @(
                        cNtfsAccessControlInformation
                        {
                            AccessControlType = 'Allow'
                            FileSystemRights = $FileSystemRights
                            Inheritance = $Inheritance
                            NoPropagateInherit = $false
                        }
                        )
                    DependsOn = $("[File]$Depends")
                    }
                    
            }
            else
            {
                cNtfsPermissionEntry $($Account + $Path.Replace(':','_'))
                    {
                        Ensure = $Ensure
                        Path = $Path
                        Principal = $Account
                        #Need depends on, just not sure how to structure yet
                        DependsOn = "[File]" + $Depends
                }
            
            }
    }
    $NtfsEntries = $ConfigurationData.NonNodeData.Roles.($Node.Role[0]).NtfsPerms #Need to find a better approach to reference Role
        foreach ($ntfs in $NtfsEntries) {
            NtfsPermissions -Ensure $ntfs[0] -Account $ntfs[1] -Path $ntfs[2] -FileSystemRights $ntfs[3] -Inheritance $ntfs[4] -Depends $ntfs[5]
        }

【讨论】:

    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 2017-09-16
    • 2017-08-19
    • 2023-03-11
    • 1970-01-01
    • 2021-01-20
    • 2020-09-01
    • 2018-11-06
    相关资源
    最近更新 更多