动态创建DSC配置文件的最佳方法是什么?
我的任务是维护复杂的文件夹结构,包括权限。这是目前使用自定义PowerShell模块完成的。当对文件夹结构本身进行更改时,会出现问题。
使用DSC可以消除问题的依从性。手动生成20k文件夹的DSC配置是绝对不可能的。我想通过PowerShell创建一些输入的DSC配置。这样,可以及时引入变化,并在DSC配置审查之后应用。
或者我完全走错了轨道,我可以从DSC配置中的输入生成结构吗?
发布于 2016-08-26 15:29:34
当您编写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。有很多文章(和书籍)是关于如何创建一个定制的资源,如果你想看看这些。
发布于 2021-05-17 20:28:59
这并不好看,但我在NTFS权限方面做了如下工作,如果您没有设置子文件夹访问权限,您可能需要进行扩展。我没有看到一种动态创建配置的简单方法,所以我使用不同的params集合重新定位。很明显,这是五年后的事了,所以你可能想出了些什么。顶部的开关基本上是用来替换节点定义文件中的变量。
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]
}https://stackoverflow.com/questions/39169413
复制相似问题