-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathTakeOwnership.ps1
More file actions
134 lines (114 loc) · 3.64 KB
/
TakeOwnership.ps1
File metadata and controls
134 lines (114 loc) · 3.64 KB
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
<#
.SYNOPSIS
A brief description of the TakeOwnership.ps1 file.
.DESCRIPTION
This script will grant ownership of files to the credentials this script is being executed under.
.PARAMETER FilesFolders
Files and folders to change permissions on.
.EXAMPLE
powershell.exe -executionpolicy bypass -file TakeOwnership.ps1 -FilesFolders "c:\Users\Mick\AppData\Roaming\Microsoft\Windows"
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.127
Created on: 9/2/2016 9:49 AM
Created by: Mick Pletcher
Organization:
Filename: TakeOwnership.ps1
===========================================================================
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][string]
$FilesFolders
)
function Grant-FolderOwnership {
<#
.SYNOPSIS
Take FileFolder Ownership
.DESCRIPTION
Take ownership of the FileFolder
.PARAMETER FileFolder
File or FileFolder to take ownership of
.PARAMETER Recurse
Take ownership of all subfolders
.EXAMPLE
PS C:\> Grant-FolderOwnership -FileFolder 'Value1'
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][string]
$FileFolder,
[switch]
$Recurse
)
$Errors = $false
If ((Test-Path $FileFolder) -eq $true) {
$Output = "Taking ownership of " + $FileFolder + "....."
If ($Recurse.IsPresent) {
#Take ownership of the top folder
$Items = takeown.exe /F $FileFolder
#Take ownership of all child folders and files
$Items = Get-ChildItem $FileFolder -Recurse | ForEach-Object { takeown.exe /F $_.FullName }
} else {
#Take ownership of the individual folder
$Executable = takeown.exe /F $FileFolder
}
}
#Get the current user this script is being executed under
[string]$CurrentUser = [Environment]::UserDomainName + "\" + [Environment]::UserName
If ($Recurse.IsPresent) {
#Test if files are owned by the current user this script is being executed under
$Item = Get-Item $FileFolder | where-object { (get-acl $_.FullName).owner -ne $CurrentUser }
$Items = Get-ChildItem $FileFolder -Recurse | where-object { (get-acl $_.FullName).owner -ne $CurrentUser }
#If no files/folders were added to $Items, then it is a success
If ((($Item -ne "") -and ($Item -ne $null)) -and (($Items -ne "") -and ($Items -ne $null))) {
$Output += "Failed"
} else {
$Output += "Success"
}
} else {
[string]$FolderOwner = (get-acl $FileFolder).owner
If ($CurrentUser -ne $FolderOwner) {
$Output += "Failed"
$Errors = $true
} else {
$Output += "Success"
}
}
Write-ToDisplay -Output $Output
If ($Errors -eq $true) {
#Error 5 is an arbitrary number I chose to flag if this fails
Exit 5
}
}
function Write-ToDisplay {
<#
.SYNOPSIS
Output Success/Failure to Display
.DESCRIPTION
Write the output to the Display color coded yellow for success and red for failure
.PARAMETER Output
Data to display to the screen
.EXAMPLE
PS C:\> Write-ToDisplay -Output 'Value1'
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]$Output
)
$OutputSplit = (($Output.Replace(".", " ")).Replace(" ", ".")).Split(".")
Write-Host $OutputSplit[0]"....." -NoNewline
If ($OutputSplit[1] -like "*Success*") {
Write-Host $OutputSplit[1] -ForegroundColor Yellow
} elseif ($OutputSplit[1] -like "*Fail*") {
Write-Host $OutputSplit[1] -ForegroundColor Red
}
}
Grant-FolderOwnership -FileFolder $FilesFolders