-
Notifications
You must be signed in to change notification settings - Fork 8
/
Clear_Cache.ps1
96 lines (96 loc) · 2.72 KB
/
Clear_Cache.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
<#
.SYNOPSIS
Kill and clear IIS Express. Removes temporary and user specific solution files.
.NOTES
Author: Evaldas Jocys <evaldas@jocys.com>
Modified: 2021-04-14
.LINK
http://www.jocys.com
#>
# ----------------------------------------------------------------------------
# Get current command path.
[string]$current = $MyInvocation.MyCommand.Path;
# Get calling command path.
[string]$calling = @(Get-PSCallStack)[1].InvocationInfo.MyCommand.Path;
# If executed directly then...
if ($calling -ne "") {
$current = $calling;
}
$file = Get-Item $current;
# Working folder.
$wdir = $file.Directory.FullName;
# ----------------------------------------------------------------------------
Function RemoveDirectories
{
# Parameters.
param($pattern);
# Function.
$items = Get-ChildItem $wdir -Filter $pattern -Recurse -Force | Where-Object {$_ -is [System.IO.DirectoryInfo]};
foreach ($item in $items)
{
Write-Output $item.FullName;
Remove-Item -LiteralPath $item.FullName -Force -Recurse;
}
}
# ----------------------------------------------------------------------------
Function RemoveSubDirectories
{
# Parameters.
param($path);
# Function.
$dirs = Get-Item $path -ErrorAction SilentlyContinue;
foreach ($dir in $dirs)
{
Write-Output "Clear: $($dir.FullName)";
$items = Get-ChildItem -LiteralPath $dir.FullName -Force | Where-Object {$_ -is [System.IO.DirectoryInfo]};
foreach ($item in $items)
{
Write-Output " - $($item.Name)";
Remove-Item -LiteralPath $item.FullName -Force -Recurse;
}
}
}
# ----------------------------------------------------------------------------
Function RemoveFiles
{
# Parameters.
param($pattern);
# Function.
$items = Get-ChildItem $wdir -Filter $pattern -Recurse -Force | Where-Object {$_ -is [System.IO.FileInfo]};
foreach ($item in $items)
{
Write-Output $item.FullName;
Remove-Item -LiteralPath $item.FullName -Force;
}
}
# ----------------------------------------------------------------------------
Function KillProcess
{
# Parameters.
param($pattern);
# Function.
$procs = Get-Process;
foreach ($proc in $procs)
{
if ($proc.Path)
{
$item = Get-Item $proc.Path;
if ($item.Name -eq $pattern)
{
Write-Output "Stopping process: $($item.Name)";
Stop-Process $proc;
}
}
}
}
# ----------------------------------------------------------------------------
# Clear IIS Express configuration.
KillProcess "iisexpress.exe";
KillProcess "iisexpresstray.exe";
RemoveSubDirectories "$($env:USERPROFILE)\Documents\My Web Sites";
# Clear directories and files.
RemoveDirectories ".vs";
RemoveFiles "*.dbmdl";
RemoveFiles "*.user";
RemoveFiles "*.suo";
pause;