-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathRemove-PrtMPrinterPermissions.ps1
241 lines (218 loc) · 10.3 KB
/
Remove-PrtMPrinterPermissions.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#Requires -Version 5.0
#Requires -Modules PrintManagement
<#
.SYNOPSIS
Removes the permissions from the printer from the specified computer
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires Module PrintManagement
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/WinPrintManagement/Printers
.Parameter PrinterName
[sr-en] Name of the printer for which to remove the permissions
.Parameter ComputerName
[sr-en] Name of the computer on which the printer is installed
.Parameter AccessAccount
[sr-en] User account that has permission to perform this action. If Credential is not specified, the executing account is used.
.Parameter ADMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the permission from the specified printer. Use the comma to separate the members
.Parameter Permission
[sr-en] Permission for the specified printer
.Parameter ADMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the permission from the specified printer. Use the comma to separate the members
.Parameter PrintPermissionMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the print permission from the specified printer. Use the comma to separate the members
.Parameter ManagePrinterPermissionMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the manage printer permission from the specified printer. Use the comma to separate the members
.Parameter ManageDocumentsPermissionMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the manage documents permission from the specified printer. Use the comma to separate the members
.Parameter ReadPermissionMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the read permissions permission from the specified printer. Use the comma to separate the members
.Parameter ChangePermissionMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the change permissions permission from the specified printer. Use the comma to separate the members
.Parameter TakeownershipPermissionMembers
[sr-en] SamAccountName or user principal name (UPN) of the users and groups to remove the takeownership permission from the specified printer. Use the comma to separate the members
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$true,ParameterSetName='Single permission')]
[Parameter(Mandatory=$true,ParameterSetName='Multiple permissions')]
[string]$PrinterName,
[Parameter(ParameterSetName='Single permission')]
[Parameter(ParameterSetName='Multiple permissions')]
[string]$ComputerName,
[Parameter(ParameterSetName='Single permission')]
[Parameter(ParameterSetName='Multiple permissions')]
[PSCredential]$AccessAccount,
[Parameter(Mandatory=$true,ParameterSetName='Single permission')]
[string]$ADMembers,
[Parameter(Mandatory=$true,ParameterSetName='Single permission')]
[ValidateSet('Print','ManagePrinter','ManageDocuments','ReadPermissions','ChangePermissions', 'Takeownership')]
[string]$Permission='Print',
[Parameter(ParameterSetName='Multiple permissions')]
[string]$PrintPermissionMembers,
[Parameter(ParameterSetName='Multiple permissions')]
[string]$ManagePrinterPermissionMembers,
[Parameter(ParameterSetName='Multiple permissions')]
[string]$ManageDocumentsPermissionMembers,
[Parameter(ParameterSetName='Multiple permissions')]
[string]$ReadPermissionMembers,
[Parameter(ParameterSetName='Multiple permissions')]
[string]$ChangePermissionMembers,
[Parameter(ParameterSetName='Multiple permissions')]
[string]$TakeownershipPermissionMembers
)
Import-Module PrintManagement
$Script:Cim = $null
$Script:output = @()
try{
function SetAceID(){
$Script:AceValue = 0
if($Permission -eq "Takeownership" ){
$Script:AceValue = 524288
}
elseif($Permission -eq "ReadPermissions"){
$Script:AceValue =131072
}
elseif($Permission -eq "ChangePermissions" ){
$Script:AceValue =262144
}
elseif($Permission -eq "ManagePrinter" ){
$Script:AceValue = 851972
}
elseif($Permission -eq "ManageDocuments"){
$Script:AceValue = 983088
}
elseif($Permission -eq "Print"){
$Script:AceValue =131080
}
<# elseif($Permission -eq "FullControl"){
$Script:AceValue =268435456
} #>
}
function RemoveAccess([string]$Member){
try{
$acc = New-Object Security.Principal.NTAccount($Member)
$mbr =$acc.Translate([Security.Principal.SecurityIdentifier]).Value
if($null -ne $mbr -and $Script:AceValue -gt 0){
if($Script:AceValue -eq 983088){
$Script:secDesc.DiscretionaryAcl.RemoveAccess([System.Security.AccessControl.AccessControlType]::Allow,
$mbr,983088,
[System.Security.AccessControl.InheritanceFlags]::ObjectInherit,[System.Security.AccessControl.PropagationFlags]::InheritOnly) | Out-Null
}
else{
$Script:secDesc.DiscretionaryAcl.RemoveAccess([System.Security.AccessControl.AccessControlType]::Allow,
$mbr,$Script:AceValue,
[System.Security.AccessControl.InheritanceFlags]::None, [System.Security.AccessControl.PropagationFlags]::None) | Out-Null
}
}
$Script:output += "Permission $($Permission) for Member:$($Member) removed"
}
catch{
$Script:output += "Error: Remove Permission $($Permission) for Member:$($Member) - $($_.Exception.Message)"
}
}
if([System.string]::IsNullOrWhiteSpace($ComputerName)){
$ComputerName = [System.Net.DNS]::GetHostByName('').HostName
}
if($null -eq $AccessAccount){
$Script:Cim = New-CimSession -ComputerName $ComputerName -ErrorAction Stop
}
else {
$Script:Cim = New-CimSession -ComputerName $ComputerName -Credential $AccessAccount -ErrorAction Stop
}
$Script:Printer = Get-Printer -Name $PrinterName -ComputerName $ComputerName -CimSession $Script:Cim -Full -ErrorAction Stop
if($null -ne $Script:Printer){
$Script:secDesc = New-Object -TypeName Security.AccessControl.CommonSecurityDescriptor ($true, $false, $Script:Printer.PermissionSDDL)
if($PSCmdlet.ParameterSetName -eq "Single permission"){
SetAceID
foreach($item in $ADMembers.Split(',') ){
RemoveAccess $item.Trim()
}
}
else{
if($null -ne $PrintPermissionMembers -and $PrintPermissionMembers.Length -gt 0){
$Permission ='Print'
SetAceID
foreach($item in $PrintPermissionMembers.Split(',') ){
RemoveAccess $item.Trim()
}
}
if($null -ne $ManagePrinterPermissionMembers -and $ManagePrinterPermissionMembers.Length -gt 0){
$Permission ='ManagePrinter'
SetAceID
foreach($item in $ManagePrinterPermissionMembers.Split(',') ){
RemoveAccess $item.Trim()
}
}
if($null -ne $ManageDocumentsPermissionMembers -and $ManageDocumentsPermissionMembers.Length -gt 0){
$Permission ='ManageDocuments'
SetAceID
foreach($item in $ManageDocumentsPermissionMembers.Split(',') ){
RemoveAccess $item.Trim()
}
}
if($null -ne $ReadPermissionMembers -and $ReadPermissionMembers.Length -gt 0){
$Permission ='ReadPermissions'
SetAceID
foreach($item in $ReadPermissionMembers.Split(',') ){
RemoveAccess $item.Trim()
}
}
if($null -ne $ChangePermissionMembers -and $ChangePermissionMembers.Length -gt 0){
$Permission ='ChangePermissions'
SetAceID
foreach($item in $ChangePermissionMembers.Split(',') ){
RemoveAccess $item.Trim()
}
}
if($null -ne $TakeownershipPermissionMembers -and $TakeownershipPermissionMembers.Length -gt 0){
$Permission ='Takeownership'
SetAceID
foreach($item in $TakeownershipPermissionMembers.Split(',') ){
RemoveAccess $item.Trim()
}
}
}
$Script:done = $false
$perms = $Script:secDesc.GetSddlForm('All')
try{
$null = Set-Printer -CimSession $Script:Cim -Name $PrinterName -ComputerName $ComputerName -PermissionSDDL $perms -ErrorAction Stop
$Script:done = $true
}
catch{
# Problems with print server W2k16
}
if($Script:done -eq $false){
$prn = $PrinterName
$cmd = { Set-Printer -Name $Using:prn -PermissionSDDL $Using:perms }
if($null -eq $AccessAccount){
Invoke-Command -ComputerName $ComputerName -validateset $cmd -ErrorAction Stop
}
else{
Invoke-Command -ComputerName $ComputerName -Credential $AccessAccount -ScriptBlock $cmd -ErrorAction Stop
}
}
}
if($SRXEnv) {
$SRXEnv.ResultMessage = $Script:output
}
else{
Write-Output $Script:output
}
}
catch{
throw
}
finally{
if($null -ne $Script:Cim){
Remove-CimSession $Script:Cim
}
}