【发布时间】:2017-05-31 03:10:58
【问题描述】:
我有一个脚本,旨在根据转换为字符串的 ACL 输出创建自定义 WMI 类的实例。这最终是通过该 WMI 类查询权限。
这个过程的关键是:
[cmdletbinding()]
param([Parameter(ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)]$Computer = '.')
$shares = gwmi -Class win32_share -ComputerName $computer | select -ExpandProperty Name
foreach ($share in $shares) {
$acl = $null
#Write-Host $share -ForegroundColor Green
#Write-Host $('-' * $share.Length) -ForegroundColor Green
$objShareSec = Get-WMIObject -Class Win32_LogicalShareSecuritySetting -Filter "name='$Share'" -ComputerName $computer
try {
$SD = $objShareSec.GetSecurityDescriptor().Descriptor
foreach($ace in $SD.DACL){
$UserName = $ace.Trustee.Name
If ($ace.Trustee.Domain -ne $Null) {$UserName = "$($ace.Trustee.Domain)\$UserName"}
If ($ace.Trustee.Name -eq $Null) {$UserName = $ace.Trustee.SIDString }
[Array]$ACL += New-Object Security.AccessControl.FileSystemAccessRule($UserName, $ace.AccessMask, $ace.AceType)
for( $i = 1; $i -lt $ACL.Length; $i++)
{
$permission = $ACL[$i] | Out-String
Write-Host "permission for $share is $permission"
Set-WmiInstance -Class TestShare -Puttype CreateOnly -Argument @{Name = $share; Permissions = $permission}
}
} #end foreach ACE
} # end try
catch
{
Write-host "Failed to create or update instance for share $share"
Write-Host ""
}
#$ACL
# Write-Host $('=' * 50)
} # end foreach $share
返回以下错误:
Set-WmiInstance : Critical error
At ...\GetShares.ps1:35 char:15
+ ... Set-WmiInstance -Class LDLocalShare -Puttype CreateOnly - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Set-WmiInstance], ManagementException
+ FullyQualifiedErrorId : SetWMIManagementException,Microsoft.PowerShell.Commands.SetWmiInstance
我将 Security.AccessControl.FileSystemAccessRule 转换为字符串的方式似乎存在问题,因为使用下面的代码并提供文字字符串可以创建一个没有问题的实例,并且具有适当的值:
Set-WmiInstance -Class TestShare -Puttype CreateOnly -Argument @{Name = "TestShare" ; Permissions = "TestPermission"}
我查看了与返回的错误相关的 technet 论坛帖子,但问题似乎总是与尝试创建未创建的类的实例有关。课程肯定在那里。是否有某种方法可以转换 Security.AccessControl.FileSystemAccessRule 而不会遇到这种情况,或者有其他方法将该信息存储在自定义 WMI 类的实例中?
编辑:$permission 的示例输出,它被转换为字符串
FileSystemRights : FullControl
AccessControlType : Allow
IdentityReference : Everyone
IsInherited : False
InheritanceFlags : None
PropagationFlags : None
【问题讨论】:
-
那么 $permission 中到底有什么?
-
@notjustme 这里是一个例子: FileSystemRights : FullControl AccessControlType : Allow IdentityReference : Everyone IsInherited : False InheritanceFlags : None PropagationFlags : None
标签: powershell wmi powershell-4.0 wmi-query