@@ -56,18 +56,18 @@ function Get-DbaPrivilege {
56
56
)
57
57
58
58
begin {
59
- $ResolveSID = @'
60
- function Convert-SIDToUserName ([string] $SID ) {
61
- try {
62
- $objSID = New-Object System.Security.Principal.SecurityIdentifier ($SID)
63
- $objUser = $objSID.Translate([System.Security.Principal.NTAccount])
64
- $objUser.Value
65
- } catch {
66
- $SID
67
- }
68
- }
69
- '@
59
+ function Convert-SIDToUserName ([string ] $SID ) {
60
+ try {
61
+ $objSID = New-Object System.Security.Principal.SecurityIdentifier ($SID )
62
+ $objUser = $objSID.Translate ([System.Security.Principal.NTAccount ])
63
+ $objUser.Value
64
+ } catch {
65
+ $SID
66
+ }
67
+ }
68
+
70
69
$ComputerName = $ComputerName.ComputerName | Select-Object - Unique
70
+
71
71
}
72
72
process {
73
73
foreach ($computer in $ComputerName ) {
@@ -78,123 +78,96 @@ function Get-DbaPrivilege {
78
78
}
79
79
80
80
try {
81
- Write-Message - Level Verbose - Message " Exporting Privileges on $computer "
82
- $null = Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ScriptBlock {
81
+ Write-Message - Level Verbose - Message " Exporting Privileges on $computer and cleaning up temporary files "
82
+ $secPol = Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ScriptBlock {
83
83
$temp = ([System.IO.Path ]::GetTempPath()).TrimEnd(" " )
84
84
secedit / export / cfg $temp \secpolByDbatools.cfg > $null
85
+ $CFG = Get-Content $temp \secpolByDbatools.cfg - Force
86
+ Remove-Item $temp \secpolByDbatools.cfg - Force
87
+ $CFG
85
88
}
86
89
87
90
Write-Message - Level Verbose - Message " Getting Batch Logon Privileges on $computer "
88
- $bl = Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ArgumentList $ResolveSID - ScriptBlock {
89
- param ($ResolveSID )
90
- . ([ScriptBlock ]::Create($ResolveSID ))
91
- $temp = ([System.IO.Path ]::GetTempPath()).TrimEnd(" " );
92
- $blEntries = (Get-Content $temp \secpolByDbatools.cfg | Where-Object {
93
- $_ -like " SeBatchLogonRight*"
94
- })
95
-
96
- if ($null -ne $blEntries ) {
97
- $blEntries.Substring (20 ).Split(" ," ) | ForEach-Object {
98
- if ($_ -match ' ^\*S-' ) {
99
- Convert-SIDToUserName - SID $_.TrimStart (' *' )
100
- } else {
101
- $_
102
- }
91
+ $blEntries = $secPol | Where-Object { $_ -like " SeBatchLogonRight*" }
92
+
93
+ $bl = if ($null -ne $blEntries ) {
94
+ $blEntries.Substring (20 ).Split(" ," ) | ForEach-Object {
95
+ if ($_ -match ' ^\*S-' ) {
96
+ Convert-SIDToUserName - SID $_.TrimStart (' *' )
97
+ } else {
98
+ $_
103
99
}
104
100
}
105
101
}
102
+
106
103
if ($bl.count -eq 0 ) {
107
104
Write-Message - Level Verbose - Message " No users with Batch Logon Rights on $computer "
108
105
}
109
106
110
107
Write-Message - Level Verbose - Message " Getting Instant File Initialization Privileges on $computer "
111
- $ifi = Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ArgumentList $ResolveSID - ScriptBlock {
112
- param ($ResolveSID )
113
- . ([ScriptBlock ]::Create($ResolveSID ))
114
- $temp = ([System.IO.Path ]::GetTempPath()).TrimEnd(" " );
115
- $ifiEntries = (Get-Content $temp \secpolByDbatools.cfg | Where-Object {
116
- $_ -like ' SeManageVolumePrivilege*'
117
- })
118
-
119
- if ($null -ne $ifiEntries ) {
120
- $ifiEntries.Substring (26 ).Split(" ," ) | ForEach-Object {
121
- if ($_ -match ' ^\*S-' ) {
122
- Convert-SIDToUserName - SID $_.TrimStart (' *' )
123
- } else {
124
- $_
125
- }
108
+ $ifiEntries = $secPol | Where-Object { $_ -like ' SeManageVolumePrivilege*' }
109
+
110
+ $ifi = if ($null -ne $ifiEntries ) {
111
+ $ifiEntries.Substring (26 ).Split(" ," ) | ForEach-Object {
112
+ if ($_ -match ' ^\*S-' ) {
113
+ Convert-SIDToUserName - SID $_.TrimStart (' *' )
114
+ } else {
115
+ $_
126
116
}
127
117
}
128
118
}
119
+
129
120
if ($ifi.count -eq 0 ) {
130
121
Write-Message - Level Verbose - Message " No users with Instant File Initialization Rights on $computer "
131
122
}
132
123
133
124
Write-Message - Level Verbose - Message " Getting Lock Pages in Memory Privileges on $computer "
134
- $lpim = Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ArgumentList $ResolveSID - ScriptBlock {
135
- param ($ResolveSID )
136
- . ([ScriptBlock ]::Create($ResolveSID ))
137
- $temp = ([System.IO.Path ]::GetTempPath()).TrimEnd(" " );
138
- $lpimEntries = (Get-Content $temp \secpolByDbatools.cfg | Where-Object {
139
- $_ -like ' SeLockMemoryPrivilege*'
140
- })
141
-
142
- if ($null -ne $lpimEntries ) {
143
- $lpimEntries.Substring (24 ).Split(" ," ) | ForEach-Object {
144
- if ($_ -match ' ^\*S-' ) {
145
- Convert-SIDToUserName - SID $_.TrimStart (' *' )
146
- } else {
147
- $_
148
- }
125
+ $lpimEntries = $secPol | Where-Object { $_ -like ' SeLockMemoryPrivilege*' }
126
+
127
+ $lpim = if ($null -ne $lpimEntries ) {
128
+ $lpimEntries.Substring (24 ).Split(" ," ) | ForEach-Object {
129
+ if ($_ -match ' ^\*S-' ) {
130
+ Convert-SIDToUserName - SID $_.TrimStart (' *' )
131
+ } else {
132
+ $_
149
133
}
150
134
}
151
135
}
136
+
152
137
if ($lpim.count -eq 0 ) {
153
138
Write-Message - Level Verbose - Message " No users with Lock Pages in Memory Rights on $computer "
154
139
}
155
140
156
141
Write-Message - Level Verbose - Message " Getting Generate Security Audits Privileges on $computer "
157
- $gsa = Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ArgumentList $ResolveSID - ScriptBlock {
158
- param ($ResolveSID )
159
- . ([ScriptBlock ]::Create($ResolveSID ))
160
- $temp = ([System.IO.Path ]::GetTempPath()).TrimEnd(" " );
161
- $gsaEntries = (Get-Content $temp \secpolByDbatools.cfg | Where-Object {
162
- $_ -like ' SeAuditPrivilege*'
163
- })
164
-
165
- if ($null -ne $gsaEntries ) {
166
- $gsaEntries.Substring (19 ).Split(" ," ) | ForEach-Object {
167
- if ($_ -match ' ^\*S-' ) {
168
- Convert-SIDToUserName - SID $_.TrimStart (' *' )
169
- } else {
170
- $_
171
- }
142
+ $gsaEntries = $secPol | Where-Object { $_ -like ' SeAuditPrivilege*' }
143
+
144
+ $gsa = if ($null -ne $gsaEntries ) {
145
+ $gsaEntries.Substring (19 ).Split(" ," ) | ForEach-Object {
146
+ if ($_ -match ' ^\*S-' ) {
147
+ Convert-SIDToUserName - SID $_.TrimStart (' *' )
148
+ } else {
149
+ $_
172
150
}
173
151
}
174
152
}
153
+
175
154
if ($gsa.count -eq 0 ) {
176
155
Write-Message - Level Verbose - Message " No users with Generate Security Audits Rights on $computer "
177
156
}
178
157
179
158
Write-Message - Level Verbose - Message " Getting Logon as a service Privileges on $computer "
180
- $los = Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ArgumentList $ResolveSID - ScriptBlock {
181
- param ($ResolveSID )
182
- . ([ScriptBlock ]::Create($ResolveSID ))
183
- $temp = ([System.IO.Path ]::GetTempPath()).TrimEnd(" " );
184
- $losEntries = (Get-Content $temp \secpolByDbatools.cfg | Where-Object {
185
- $_ -like " SeServiceLogonRight*"
186
- })
187
-
188
- if ($null -ne $losEntries ) {
189
- $losEntries.Substring (22 ).split(" ," ) | ForEach-Object {
190
- if ($_ -match ' ^\*S-' ) {
191
- Convert-SIDToUserName - SID $_.TrimStart (' *' )
192
- } else {
193
- $_
194
- }
159
+ $losEntries = $secPol | Where-Object { $_ -like " SeServiceLogonRight*" }
160
+
161
+ $los = if ($null -ne $losEntries ) {
162
+ $losEntries.Substring (22 ).split(" ," ) | ForEach-Object {
163
+ if ($_ -match ' ^\*S-' ) {
164
+ Convert-SIDToUserName - SID $_.TrimStart (' *' )
165
+ } else {
166
+ $_
195
167
}
196
168
}
197
169
}
170
+
198
171
if ($los.count -eq 0 ) {
199
172
Write-Message - Level Verbose - Message " No users with Logon as a service Rights on $computer "
200
173
}
@@ -211,11 +184,7 @@ function Get-DbaPrivilege {
211
184
LogonAsAService = $los -contains $_
212
185
}
213
186
}
214
- Write-Message - Level Verbose - Message " Removing secpol file on $computer "
215
- Invoke-Command2 - Raw - ComputerName $computer - Credential $Credential - ScriptBlock {
216
- $temp = ([System.IO.Path ]::GetTempPath()).TrimEnd(" " )
217
- Remove-Item $temp \secpolByDbatools.cfg - Force
218
- }
187
+
219
188
} catch {
220
189
Stop-Function - Continue - Message " Failure" - ErrorRecord $_ - Target $computer
221
190
}
0 commit comments