-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSend-AdminRemote.ps1
More file actions
97 lines (88 loc) · 4.03 KB
/
Send-AdminRemote.ps1
File metadata and controls
97 lines (88 loc) · 4.03 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
function Send-AdminReport {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$ReportName,
[Parameter(Mandatory=$true)]
[ValidateSet("CSV","JSON","HTML")]
[string]$Format,
[string]$Recipient,
[string]$fSender = "$($env:USERNAME)@localhost",
[string]$SmtpServer,
[switch]$IncludeUsers,
[switch]$IncludeServices,
[switch]$InclideUpdates
)
begin {
$files = @()
$timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$fileName = "$ReportName-$timestamp.$Format"
$filePath = Join-Path -Path "C:\Temp" -ChildPath $fileName
if (-not (Test-Path "C:\Temp")) {
Write-AdminLog -Message "Directory was created at path C:\Temp" -Level DEBUG -FunctionName 'Send-AdminReport'
New-Item -Path "C:\Temp" -ItemType Directory -Force
}
}
process {
if ($PSBoundParameters.ContainsKey("CSV")) {
if ($IncludeUsers) {
$users = Get-LocalUser | Select-Object Name,Enabled
$users | Export-Csv $filePath -NoTypeInformation -Force
$files += $filePath
}
if ($IncludeServices) {
$services = Get-Service -ErrorAction SilentlyContinue | Select-Object Name,Status,StartType
$services | Export-Csv $filePath -NoTypeInformation -Force
$files += $filePath
}
if ($InclideUpdates) {
$updates = Get-HotFix | Select-Object HotFixID,Description,InstalledOn
$updates | Export-Csv $filePath -NoTypeInformation -Force
$files += $filePath
}
Write-AdminLog -Message "CSV Report was created at path '$filePath'" -Level DEBUG -FunctionName 'Send-AdminReport'
} elseif ($PSBoundParameters.ContainsKey("HTML")) {
$files = "<h1>$ReportName Report</h1>"
if ($IncludeUsers) {
$users = Get-LocalUser | Select-Object Name,Enabled
$files += "<h2>Users</h2>" + ($users | ConvertTo-Html -Fragment)
}
if ($IncludeServices) {
$services = Get-Service -ErrorAction SilentlyContinue | Select-Object Name,Status,StartType
$files += "<h2>Services</h2>" + ($services | ConvertTo-Html -Fragment)
}
if ($InclideUpdates) {
$updates = Get-HotFix | Select-Object HotFixID,Description,InstalledOn
$files += "<h2>Updates</h2>" + ($updates | ConvertTo-Html -Fragment)
}
Write-AdminLog -Message "HTML Report was created at path '$filePath'" -Level DEBUG -FunctionName 'Send-AdminReport'
$files | Out-File $filePath
} else {
$files = @{}
if ($IncludeUsers) {
$users = Get-LocalUser | Select-Object Name,Enabled
$files.Users += $users
}
if ($IncludeServices) {
$services = Get-Service -ErrorAction SilentlyContinue | Select-Object Name,Status,StartType
$files.Service += $services
}
if ($InclideUpdates) {
$updates = Get-HotFix | Select-Object HotFixID,Description,InstalledOn
$files.Updates += $updates
}
Write-AdminLog -Message "JSON Report was created at path '$filePath'" -Level DEBUG -FunctionName 'Send-AdminReport'
$files| ConvertTo-Json | Out-File $filePath -Force
}
}
end {
Write-Output "Report Generated in $filePath"
if ($Recipient) {
Send-MailMessage -To $Recipient -From $fSender -Subject "Admin Report: $ReportName" `
-Body "Report generated at $(Get-Date)" -SmtpServer $SmtpServer `
-Attachments $files
Write-AdminLog -Message "Report sent to '$Recipient': '$filePath'" -Level INFO -FunctionName 'Send-AdminReport'
Write-Output "Report sent to '$Recipient': '$filePath'"
}
}
}