powershell - Win32_服务 : Use SetSecurityDescriptor() from PowerShell

标签 powershell wmi

我正在尝试从 PowerShell 为 Windows 服务的用户设置权限。但 WMI 完全忽略我的更改。我做错了什么?

$service = Get-WmiObject -EnableAllPrivileges Win32_Service | Where-Object {$_.Name -eq "HubertService"}
$descriptor = $service.GetSecurityDescriptor()
$descriptor.Descriptor.Owner

到目前为止,这有效。我现在有了一个很好的安全描述符。我可以显示所有者。

现在,如果我更改任何内容,例如其中一个 ACE 的 AccessMask,这也有效:

$descriptor.Descriptor.DACL[0].AccessMask = 0

我什至可以将这个修改后的描述符写入我的 Win32_Service 对象:

$service.InvokeMethod("SetSecurityDescriptor", $descriptor, $null)

但是,之前和之后

$service.Put()

当我创建新的 $service 变量并对其运行 GetSecurityDeciptor() 时,修改根本不会显示。

我做错了什么?

最佳答案

经过一番尝试,我找到了解决方案。

# Get service object and its security descriptor
$service = Get-WmiObject -EnableAllPrivileges Win32_Service | Where-Object {$_.Name -eq "HubertService"}
$sd = ($service.GetSecurityDescriptor()).Descriptor
$adacl = $sd.DACL
$adacl.Count # Shows current number of ACEs in the DACL

# Create new ACE for new user to add
$ace = ([WMIClass]"Win32_ACE").CreateInstance()
$trustee = ([WMIClass]"Win32_Trustee").CreateInstance()
$account = New-Object System.Security.Principal.NTAccount("mydomain","hubert")
$sid = $account.Translate([System.Security.Principal.SecurityIdentifier])

# Fill in trustee and add trustee to ACE
$trustee.Domain = "mydomain"
$trustee.Name = "hubert"
$trustee.SIDString = $sid.Value # Don't need byte array
$ace.Trustee = $trustee

# Add ACE to DACL and replace DACL in security descriptor
$adacl += $ace
$sd.DACL = $adacl
$service.SetSecurityDescriptor($sd) # This appears to work just like that

也可以通过将 DACL 替换为不删除 ACE 的 DACL 来再次删除新的 ACE。

关于powershell - Win32_服务 : Use SetSecurityDescriptor() from PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20617081/

相关文章:

wmi - WMI 是制作硬件指纹的好方法吗?

powershell - 关于 ISE 与带有 SystemEvents 的控制台的问题

c# - ManagementObjectSearcher WMIquery 很慢

PowerShell 在 Invoke-Command 上生成参数列表

windows - 如何使用批处理获取前景窗口?

c++ - 从多个类中选择 WMI TargetInstance?

c# - 如何在.net中发布wmi类?

command-line - Windows事件的“In words”部分

powershell - 使用Invoke-Command在远程计算机上处​​理命令的列表

powershell - 如何合并和压缩多个脚本和CSS文件供生产使用?