-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-WMINameSpaceSecurity.ps1
106 lines (53 loc) · 2.42 KB
/
Get-WMINameSpaceSecurity.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function Get-WMINameSpaceSecurity {
# Copyright (c) Microsoft Corporation. All rights reserved.
# For personal use only. Provided AS IS and WITH ALL FAULTS.
# Get-WmiNamespaceSecurity.ps1
# Example: Get-WmiNamespaceSecurity root/cimv2
Param ( [parameter(Mandatory=$true,Position=0)][string] $namespace,
[string] $computer = ".",
[System.Management.Automation.PSCredential] $credential = $null)
Process {
$ErrorActionPreference = "Stop"
Function Get-PermissionFromAccessMask($accessMask) {
$WBEM_ENABLE = 1
$WBEM_METHOD_EXECUTE = 2
$WBEM_FULL_WRITE_REP = 4
$WBEM_PARTIAL_WRITE_REP = 8
$WBEM_WRITE_PROVIDER = 0x10
$WBEM_REMOTE_ACCESS = 0x20
$READ_CONTROL = 0x20000
$WRITE_DAC = 0x40000
$WBEM_RIGHTS_FLAGS = $WBEM_ENABLE,$WBEM_METHOD_EXECUTE,$WBEM_FULL_WRITE_REP,`
$WBEM_PARTIAL_WRITE_REP,$WBEM_WRITE_PROVIDER,$WBEM_REMOTE_ACCESS,`
$WBEM_RIGHT_SUBSCRIBE,$WBEM_RIGHT_PUBLISH,$READ_CONTROL,$WRITE_DAC
$WBEM_RIGHTS_STRINGS = "Enable","MethodExecute","FullWrite","PartialWrite",`
"ProviderWrite","RemoteAccess","Subscribe","Publish","ReadSecurity","WriteSecurity"
$permission = @()
for ($i = 0; $i -lt $WBEM_RIGHTS_FLAGS.Length; $i++) {
if (($accessMask -band $WBEM_RIGHTS_FLAGS[$i]) -gt 0) {
$permission += $WBEM_RIGHTS_STRINGS[$i]
}
}
$permission
}
$INHERITED_ACE_FLAG = 0x10
$invokeparams = @{Namespace=$namespace;Path="__systemsecurity=@";Name="GetSecurityDescriptor";ComputerName=$computer}
if ($credential -eq $null) {
$credparams = @{}
} else {
$credparams = @{Credential=$credential}
}
$output = Invoke-WmiMethod @invokeparams @credparams
if ($output.ReturnValue -ne 0) {
throw "GetSecurityDescriptor failed: $($output.ReturnValue)"
}
$acl = $output.Descriptor
foreach ($ace in $acl.DACL) {
$user = New-Object System.Management.Automation.PSObject
$user | Add-Member -MemberType NoteProperty -Name "Name" -Value "$($ace.Trustee.Domain)\$($ace.Trustee.Name)"
$user | Add-Member -MemberType NoteProperty -Name "Permission" -Value (Get-PermissionFromAccessMask($ace.AccessMask))
$user | Add-Member -MemberType NoteProperty -Name "Inherited" -Value (($ace.AceFlags -band $INHERITED_ACE_FLAG) -gt 0)
$user
}
}
}