-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIISFlush.ps1
More file actions
94 lines (79 loc) · 2.34 KB
/
IISFlush.ps1
File metadata and controls
94 lines (79 loc) · 2.34 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
<#
.SYNOPSIS
Restart IIS completely each night.
.NOTES
File Name : IISFlush.ps1
Author : Jeff Jones - @spjeff
Version : 0.10
Last Modified : 11-16-2017
.PARAMETER install
Typing "IISFlush.ps1 -install" will create a local Task Scheduler job under credentials of the current user. Job runs once daily at 3AM to stop and start the Excel Service Instance.
.LINK
http://www.github.com/spjeff/spadmin/IISFlush.ps1
.EXAMPLE
.\IISFlush.ps1 -i
.\IISFlush.ps1 -install
#>
param (
[Alias("i")]
[switch]$install
)
Function Installer() {
# Add to Task Scheduler
Write-Output " Installing to Task Scheduler..."
if (!$user) {
$user = $ENV:USERDOMAIN + "\" + $ENV:USERNAME
}
Write-Output " User for Task Scheduler job: $user"
# Attempt to detect password from IIS Pool (if current user is local admin and farm account)
$appPools = Get-WMIObject -Namespace "root/MicrosoftIISv2" -Class "IIsApplicationPoolSetting" | Select-Object WAMUserName, WAMUserPass
foreach ($pool in $appPools) {
if ($pool.WAMUserName -like $user) {
$pass = $pool.WAMUserPass
if ($pass) {
break
}
}
}
# Manual input if auto detect failed
if (!$pass) {
$pass = Read-Host "Enter password for $user "
}
# Task Scheduler command
$cmd = "powershell.exe -ExecutionPolicy Bypass ""$cmdpath"""
# Delete task
Write-Output "SCHTASKS DELETE"
SCHTASKS /delete /tn "Daily-IISFlush" /f
Write-Host " [OK]" -Fore Green
# Create task
Write-Output "SCHTASKS CREATE"
SCHTASKS /create /tn "Daily-IISFlush" /tr "$cmd" /ru $user /rp $pass /rl highest /sc Daily /st 21:00
Write-Host " [OK]" -Fore Green
}
function IISFlush() {
# Reset IIS and verify 100% started. Twice to be sure.
Import-Module WebAdministration -ErrorAction SilentlyContinue -WarningAction SilentlyContinue | Out-Null
function IISGo {
NET START W3SVC
Get-ChildItem "IIS:\AppPools" | % {$n = $_.Name; Start-WebAppPool $n}
Get-WebSite | Start-WebSite
}
IISRESET
Start-Sleep 5
IISGo
Start-Sleep 5
IISGo
}
# Main
$cmdpath = $MyInvocation.MyCommand.Path
if ($install) {
Installer
}
else {
Start-Transcript
Get-Date
IISFlush
Write-Host "DONE" -Fore Green
Get-Date
Stop-Transcript
}