-
Notifications
You must be signed in to change notification settings - Fork 1
/
Insert-RestartEvents.ps1
103 lines (64 loc) · 3.16 KB
/
Insert-RestartEvents.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
<#
.Synopsis
Inserts selected scom events to custom database
.DESCRIPTION
Inserts selected scom events to custom database
.EXAMPLE
.\Insert-RestartEvents -SQLServer 'opwscomdb1' -Instance 'default,1977' -Database 'SCOMDashboard' -TableName 'RestartEvents' -Verbose
VERBOSE: [27.04.2020 16:24:51] Script Started.
WARNING: Using provider context. Server = opwscomdb1\default,1977, Database = [SCOMDashboard].
VERBOSE: [27.04.2020 16:25:21] Inserted 1691 number of CPU Samples in total
VERBOSE: [27.04.2020 16:25:21] Script ended.Script dutation is 29.8307143
#>
#requires -version 5.1 -Modules OperationsManager
[CmdletBinding()]
Param(
[Parameter(Mandatory= $true)]
[string]$SQLServer,
[Parameter(Mandatory= $true)]
[string]$Instance,
[Parameter(Mandatory= $true)]
[string]$Database,
[Parameter(Mandatory= $true)]
[string]$TableName,
[string]$RuleDisplayName = 'Collection rule for Service or Driver Failed to Start events',
[string]$ManagementServer = 'OvwScomMng1.kfs.local'
)
$ScriptStart = Get-date
$SelectTableName = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_CATALOG='$database' and TABLE_NAME = '$TableName'"
Write-Verbose "[$(Get-Date -Format G)] Script Started."
Import-module OperationsManager -Verbose:$false
Import-Module SqlServer -Verbose:$false
# Connect to Management Server
try {
New-SCOMManagementGroupConnection -ComputerName $ManagementServer -ErrorAction Stop
} Catch {
throw "Could not connect to $ManagementServer. Error. $($_.Exception.Message)"
}
# Collect Events
$event = Get-SCOMEvent
$Rules = Get-SCOMManagementPack | where {$_.Name -match 'Microsoft\.Windows\.Server\.\d{4}\.Monitoring'} | Get-SCOMRule |where {$_.Name -match '(restart)|(shutdown)'}
$Events = get-scomevent -Rule $Rules | Select-Object -Property MonitoringObjectName,MonitoringObjectDisplayName,MonitoringObjectPath,MonitoringObjectFullName,MonitoringRuleDisplayName,MonitoringRuleDescription,PublisherName,Number,Description,CategoryId,Category,User,LoggingComputer,TimeGenerated,TimeAdded
# Prepare SQL Server connections
Try {
if((Invoke-Sqlcmd -ServerInstance "$SQLServer\$Instance" -Database $Database -Query $SelectTableName -ErrorAction stop)) {
Write-Verbose "[$(Get-date -Format G)]Found $TableName table dropping."
Invoke-Sqlcmd -ServerInstance "$SQLServer\$Instance" -Database $Database -Query "DROP TABLE [dbo].[$TableName]" -ErrorAction Stop
}
} catch {
Throw "[$(Get-Date -Format G)] Select or delete TableName`nError: $($_.Exception.Message)"
}
try {
New-PSDrive -Name SCOMDashboard -PSProvider 'SQLServer' -root "SQLSERVER:\SQL\$SQLServer\$Instance\Databases\$Database" -ErrorAction stop | Out-Null
cd 'SCOMDashboard:\Tables'
Write-SqlTableData -TableName $TableName -InputData $Events -Force -SchemaName dbo -ErrorAction Stop
Write-Verbose "[$(Get-Date -Format G)] Inserted $($ConvertedEvents.Count) number of Rows in total"
}
Catch {
Throw "[$(Get-Date -Format G)] Couldnt Insert to SQL.`nError: $($_.Exception.Message)"
}
Finally {
cd c:\
Remove-PSDrive SCOMDashboard
}
Write-verbose "[$(Get-date -Format G)] Script ended. Script dutation is $(((Get-date) - $ScriptStart).TotalSeconds)"