forked from scriptrunner/ActionPacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SysMLibrary.ps1
210 lines (177 loc) · 8.32 KB
/
SysMLibrary.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
#Requires -Version 5.0
function RemoveComputerProfile(){
<#
.SYNOPSIS
Removes profile on 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
.LINK
.Parameter CimInstance
Cim user profile instance
.Parameter Action
Action to be performed before remove the profile on computer
.Parameter ComputerName
Specifies the computer from which the profile are removed
.Parameter AccessAccount
Specifies a user account that has permission to perform this action. If Credential is not specified, the current user account is used
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[object]$CimInstance,
[ValidateSet('None','ZipProfile','Rename')]
[string]$Action,
[string]$ComputerName,
[pscredential]$AccessAccount
)
try{
[string]$profilePath = $CimInstance.LocalPath
if($Action -eq 'ZipProfile'){
$archiveName = $profilePath + ".zip"
if($null -eq $AccessAccount){
Invoke-Command -ComputerName $ComputerName -ScriptBlock{
Compress-Archive -LiteralPath $Using:profilePath -CompressionLevel Optimal -DestinationPath $Using:archiveName -Update -ErrorAction Stop
} -ErrorAction Stop
}
else{
Invoke-Command -ComputerName $ComputerName -Credential $AccessAccount -ScriptBlock{
Compress-Archive -LiteralPath $Using:profilePath -CompressionLevel Optimal -DestinationPath $Using:archiveName -Update -ErrorAction Stop
} -ErrorAction Stop
}
}
elseif($Action -eq 'Rename'){
$newName = $profilePath + ".old"
if($null -eq $AccessAccount){
Invoke-Command -ComputerName $ComputerName -ScriptBlock{
Rename-Item -Path $Using:profilePath -NewName $Using:newName -Force -Confirm:$false -ErrorAction Stop
} -ErrorAction Stop
}
else{
Invoke-Command -ComputerName $ComputerName -Credential $AccessAccount -ScriptBlock{
Rename-Item -Path $Using:profilePath -NewName $Using:newName -Force -Confirm:$false -ErrorAction Stop
} -ErrorAction Stop
}
}
$null = Remove-CimInstance -CimSession $Script:Cim -InputObject $usrProfile -Confirm:$false -ErrorAction Stop
}
catch{
throw
}
finally{
}
}
function RemoveServerProfile(){
<#
.SYNOPSIS
Removes profile on server
.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
.LINK
.Parameter ADUser
Active Directory user instance
.Parameter Action
Action to be performed before remove the profile on server
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[object]$ADUser,
[ValidateSet('None','ZipProfile','Rename')]
[string]$Action
)
try{
if(([System.String]::IsNullOrWhiteSpace($ADUser.ProfilePath) -eq $false) -and ($Action -ne 'None')){
[string]$srvProfile = $ADUser.ProfilePath
if($Action -eq 'ZipProfile'){
$null = Compress-Archive -LiteralPath $srvProfile -CompressionLevel Optimal -DestinationPath ($srvProfile + '.zip') -Update -ErrorAction Stop
}
elseif($Action -eq 'Rename'){
$null = Rename-Item -Path $srvProfile -NewName ($srvProfile + '.old') -Force -Confirm:$false -ErrorAction Stop
}
$null = Remove-Item -Path $srvProfile -Force -Confirm:$false -Recurse -ErrorAction Stop
}
}
catch{
throw
}
finally{
}
}
function RemoveOldProfiles(){
<#
.SYNOPSIS
Removes old profiles on 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
.LINK
.Parameter CimSession
Cim instance
.Parameter DaysAgo
Specifies the days the user has not logged in
.Parameter ComputerAction
Action to be performed before remove the profile on computer
.Parameter ServerAction
Action to be performed before remove the profile on server
.Parameter ComputerName
Specifies the computer from which the profile are removed
.Parameter AccessAccount
Specifies a user account that has permission to perform this action. If Credential is not specified, the current user account is used
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[object]$CimSession,
[int]$DaysAgo = 180,
[ValidateSet('None','ZipProfile','Rename')]
[string]$ComputerAction,
[ValidateSet('None','ZipProfile','Rename')]
[string]$ServerAction,
[string]$ComputerName,
[pscredential]$AccessAccount
)
try{
$usrProfiles = Get-CimInstance -CimSession $CimSession -ClassName Win32_UserProfile -ErrorAction Stop | Where-Object{$_.Special -eq $false}
if($null -eq $Global:output){
$Global:output = @()
}
foreach($prof in $usrProfiles){
if($prof.LastUseTime.Date.ToFileTimeUtc() -lt [System.DateTime]::Today.AddDays(-$DaysAgo).ToFileTimeUtc()){
$sid = $prof.SID
$Script:usr = Get-ADUser -Filter{SID -eq $sid} -Properties Name
if([System.String]::IsNullOrWhiteSpace($usr.Name) -eq $false){
# Server action
RemoveServerProfile -ADUser $Script:usr -Action $ServerAction
# Computer actions
RemoveComputerProfile -AccessAccount $AccessAccount -ComputerName $ComputerName -CimInstance $prof -Action $ComputerAction
$Global:output += "Profile $($prof.LocalPath) removed"
}
}
}
}
catch{
throw
}
finally{
}
}