-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDaily-SMAT.ps1
More file actions
90 lines (77 loc) · 2.64 KB
/
Daily-SMAT.ps1
File metadata and controls
90 lines (77 loc) · 2.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
<#
.SYNOPSIS
Run SMAT (SharePoint Migration Assesment) daily and output to folder. Download SMAT from https://www.microsoft.com/en-us/download/details.aspx?id=53598
.NOTES
File Name : Daily-SMAT.ps1
Author : Jeff Jones - @spjeff
Version : 0.11
Last Modified : 06-29-2017
.PARAMETER install
Typing "Daily-SMAT.ps1 -install" will create a local Task Scheduler job under credentials of the current user. Job runs once daily at 3AM to produce a new current report.
.LINK
http://www.github.com/spjeff/spadmin/Daily-SMAT.ps1
.EXAMPLE
.\Daily-SMAT.ps1 -i
.\Daily-SMAT.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-SMAT" /f
Write-Host " [OK]" -Fore Green
# Create task
Write-Output "SCHTASKS CREATE"
SCHTASKS /create /tn "Daily-SMAT" /tr "$cmd" /ru $user /rp $pass /rl highest /sc Daily /st 21:00
Write-Host " [OK]" -Fore Green
}
# Main
$cmdpath = $MyInvocation.MyCommand.Path
$cmdfolder = Split-Path $cmdpath
if ($install) {
Installer
}
else {
Start-Transcript
# Remove folders older than 30 days
md "$cmdfolder\REPORT" -ErrorAction SilentlyContinue | Out-Null
$threshold = (Get-Date).AddDays(-30)
$folders = Get-ChildItem "$cmdfolder\REPORT"
foreach ($f in $folders) {
if ($f.LastWriteTime -lt $threshold) {
Write-Host "Deleting folder $($f.Name)"
$f | Remove-ChildItem -Confirm:$false
}
}
# Execute SMAT report and output to folder with today's date
$date = (Get-Date).AddDays(1).ToString("yyyy-MM-dd")
" Output: $cmdfolder\$date\"
Invoke-Expression "$cmdfolder\SMAT\SMAT.exe -o ""$cmdfolder\REPORT\$date\"" -t 8 -q"
Write-Host "DONE" -Fore Green
Stop-Transcript
}