-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathInvoke-Thanos.ps1
152 lines (133 loc) · 5.56 KB
/
Invoke-Thanos.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
<#
.SYNOPSIS
THANOS! Randomly removes about half of your objects.
.DESCRIPTION
Thanos enumerates the objects in the literal path specified by Path, and removes each file with probability 1/2.
Thanos supports FileSystem, Registry, Certificate, Variable, Function, Alias and Environment PowerShell providers. The only built-in provider that it does not recognise is WSMan. When an unrecognised provider is encountered, it performs some random default action.
This advanced function takes special care for WhatIf and Confirm parameters. Specifically:
- If both WhatIf and Confirm are specified ($True or $False), they are left intact.
- If none is specified, both default to $True.
- If WhatIf is specified while Confirm is not, Confirm defaults to $True.
- If Confirm is specified while WhatIf is not, WhatIf defaults to $WhatIfPreference.
Contributed by Gee Law. See examples (Get-Help .\Invoke-Thanos.ps1 -Examples) for examples.s
.PARAMETER Path
Specifies the literal path. Defaults to ".".
.EXAMPLE
.\Invoke-Thanos.ps1 C:\
Prints the effect of removing about half of files in drive C.
.EXAMPLE
.\Invoke-Thanos.ps1 . -Confirm:$False
Removes about half of the files in the current directory, WITHOUT confirmation.
.EXAMPLE
.\Invoke-Thanos.ps1 HKCU:\ -WhatIf:$False
Removes about half of the registry values in HKEY_CURRENT_USER, with confirmation for each value to be removed.
#>
[CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = 'High')]
Param
(
[string]$Path = '.'
)
Begin
{
$local:hadDisabled = $PSDefaultParameterValues.ContainsKey('Disabled')
$local:disabledValue = $PSDefaultParameterValues['Disabled']
$PSDefaultParameterValues['Disabled'] = $True
$local:hasWhatIf = $PSBoundParameters.ContainsKey('WhatIf')
$local:hasConfirm = $PSBoundParameters.ContainsKey('Confirm')
# Resolve WhatIf and Confirm.
If (-not $local:hasWhatIf -and -not $local:hasConfirm)
{
$WhatIf = $True
$local:WhatIfPreference = $True
$Confirm = $True
$local:ConfirmPreference = 'Low'
Write-Verbose 'WhatIf and Confirm are not specified. Both default to $True.'
}
If ($local:hasWhatIf -and -not $local:hasConfirm)
{
$WhatIf = $PSBoundParameters['WhatIf']
$Confirm = $True
$local:ConfirmPreference = 'Low'
Write-Verbose 'WhatIf is specified while Confirm is not. Confirm defaults to $True.'
}
If (-not $local:hasWhatIf -and $local:hasConfirm)
{
$WhatIf = $WhatIfPreference
$Confirm = $PSBoundParameters['Confirm']
Write-Verbose 'Confirm is specified while WhatIf is not. WhatIf defaults to $WhatIfPreference.'
}
If ($local:hasWhatIf -and $local:hasConfirm)
{
$WhatIf = $PSBoundParameters['WhatIf']
$Confirm = $PSBoundParameters['Confirm']
Write-Verbose 'Both WhatIf and Confirm are specified.'
}
}
Process
{
$local:resolvedPath = Resolve-Path -LiteralPath $Path
$local:randomSource = [System.Random]::new()
If ($local:resolvedPath -eq $null)
{
Write-Error 'Thanos failed to come to the site.' -RecommendedAction 'Do not invoke Thanos.'
Return
}
Switch -Regex -CaseSensitive ($local:resolvedPath.Provider.Name)
{
'FileSystem' {
$local:resolvedPath | Get-ChildItem -Recurse -Force -File |
Where-Object { $randomSource.NextDouble() -lt 0.5 } |
Remove-Item -Force -WhatIf:$WhatIf -Confirm:$Confirm
}
'Registry' {
$local:resolvedPath | Get-ChildItem -Recurse -Force -PipelineVariable regKey |
ForEach-Object { $regKey.Property |
Where-Object { $randomSource.NextDouble() -lt 0.5 } |
ForEach-Object { $regKey |
Remove-ItemProperty -Name $_ -WhatIf:$WhatIf -Confirm:$Confirm }
}
}
'Certificate' {
$local:resolvedPath |
Get-ChildItem -Recurse -Force |
Where-Object { $_ -is [X509Certificate] } |
Where-Object { $randomSource.NextDouble() -lt 0.5 } |
Remove-Item -Force -WhatIf:$WhatIf -Confirm:$Confirm
}
'Variable|Function|Alias' {
$local:resolvedPath | Get-ChildItem -Force |
Where-Object { $randomSource.NextDouble() -lt 0.5 } |
Remove-Item -Force -WhatIf:$WhatIf -Confirm:$Confirm
}
'Environment' {
$local:resolvedPath | Get-ChildItem -Force |
Where-Object { $randomSource.NextDouble() -lt 0.5 } |
ForEach-Object {
If ($PSCmdlet.ShouldProcess($_.Name, 'Remove environment variable from Machine, User and Process'))
{
[System.Environment]::SetEnvironmentVariable($_.Name, $null, 'Machine')
[System.Environment]::SetEnvironmentVariable($_.Name, $null, 'User')
[System.Environment]::SetEnvironmentVariable($_.Name, $null, 'Process')
}
}
}
Default
{
$local:resolvedPath | Get-ChildItem -Recurse -Force |
Where-Object { $_ | Test-Path -PathType Leaf } |
Where-Object { $randomSource.NextDouble() -lt 0.5 } |
Remove-Item -Force -WhatIf:$WhatIf -Confirm:$Confirm
}
}
}
End
{
If ($local:hadDisabled)
{
$PSDefaultParameterValues['Disabled'] = $local:disabledValue
}
Else
{
$PSDefaultParameterValues.Remove('Disabled')
}
}