-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-MSSQLJobActivities.ps1
119 lines (109 loc) · 3.14 KB
/
Get-MSSQLJobActivities.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Import-Module SQLPS -WarningAction SilentlyContinue
$ErrorActionPreference= 'silentlycontinue'
$path = Get-Location
function Get-IniFile {
param(
[parameter(Mandatory = $true)] [string] $filePath
)
$anonymous = "NoSection"
$ini = @{}
switch -regex -file $filePath
{
"^\[(.+)\]$" # Section
{
$section = $matches[1]
$ini[$section] = @{}
$CommentCount = 0
}
"^(;.*)$" # Comment
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$value = $matches[1]
$CommentCount = $CommentCount + 1
$name = "Comment" + $CommentCount
$ini[$section][$name] = $value
}
"(.+?)\s*=\s*(.*)" # Key
{
if (!($section))
{
$section = $anonymous
$ini[$section] = @{}
}
$name,$value = $matches[1..2]
$ini[$section][$name] = $value
}
}
return $ini
}
function Get-UserConfig {
param(
[parameter(Mandatory = $false)] [string] $instance
)
$ini = ""
$mkconf=Get-Item -Path $ENV:MK_CONFDIR
if($instance.Length -gt 0)
{
$conf="$($mkconf.FullName)mssql_$instance.ini"
if(Get-Item -Path "$conf")
{
$ini = Get-IniFile -filePath "$conf"
}
}
else
{
$conf="$($mkconf.FullName)mssql.ini"
if(Get-Item -Path "$conf")
{
$ini = Get-IniFile -filePath "$conf"
}
}
return $ini
}
function Invoke-SqlcmdWithConfig{
param(
$instance,
$sql
)
$iniGlobal=Get-UserConfig
cd $instance.PSPath
$ini = Get-UserConfig -instance $instance.PSChildName
if($ini -like "")
{
$ini=$iniGlobal
}
$data=""
if($ini -like "")
{
$data = Invoke-Sqlcmd -Query "$sql" -WarningAction SilentlyContinue
}
else
{
$data = Invoke-Sqlcmd -Query "$sql" -Username $ini.auth.name -Password $ini.auth.password -WarningAction SilentlyContinue
}
return $data
}
cd SQLSERVER:\SQL
foreach($machine in Get-ChildItem){
cd $machine.PSPath
foreach($instance in Get-ChildItem)
{
$data = Invoke-SqlcmdWithConfig -instance $instance -sql "EXEC msdb.dbo.sp_help_jobactivity"
"<<<mssql_job_activity>>>"
if($data -ne "")
{
foreach($dataset in $data)
{
if($dataset.next_scheduled_run_date.toString().trim() -notlike "")
{
"$($dataset.job_name.Trim().Replace(" ", "_")) $($dataset.next_scheduled_run_date.ToShortDateString().Trim()) $($dataset.next_scheduled_run_date.ToLongTimeString().Trim()) $($dataset.run_status)$($dataset.run_status.Trim()) $($dataset.message.Trim().Replace(" ", "_").Replace(".__", " "))"
}
}
}
}
}
cd $path