-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-serviceV3.ps1
181 lines (141 loc) · 7.24 KB
/
get-serviceV3.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
## Script check in list of servers a list of services
## Post json messages in Teams Group
## Post or consult incidents in API Tickets
## Post number of ticket in Teams Group in case of opem
## Opem new ticket in case not opened
## Notify Group Teams for number ticket
## Execution new task scheduler with powershell -command "& 'Path_of_script' 'Path_of_config_file'"
##XML Parameters
param(
[string]$configPath
)
##Change path
Set-Location $configPath
##Import Function
. .\import-xml.ps1
$Config = Import-XMLConfig -ConfigPath $configPath\config.xml
Write-Output $Config
##Start Log
$logFile = $Config.log + (Get-Date).ToString('yyyyMMddHHmm') + ".txt"
Start-Transcript -Path $logFile
##URL Teams group
$webHook = $Config.url
##Variable to platform
$plataforma = $Config.plat
$filepath = ".\im.txt"
##list servers
foreach($servers in $Config.server){
Write-Output $servers
##List services
foreach($service in $Config.service){
Write-Output $service
##Get incident status
$check = Get-Service -ComputerName $servers -DisplayName $service | Select-Object Status
Write-Output $check
$serv = $check.status
##Condition is Running ?
if($serv -ne "Running") {
##Fail message in Teams Group
$alert = ConvertTo-Json -Depth 1 @{
text = "`nServico: $service Status: $serv Servidor: $servers"
title = "Servico do $plataforma"
}
##Post Teams
$response = Invoke-RestMethod -ContentType "application/json" -Method Post -body $alert -Uri $webHook
Write-Output $response
##Get number of incidente
$im = Get-Content -Path $filepath
##Variable to acces API of incidents
$username = 'svcacc_tiim'
$password = 'mudar$123'
$credPair = "$($username):$($password)"
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credPair))
$headers = @{ Authorization = "Basic $encodedCredentials" }
##Condition of no incident exist
if($im -ne $null){
$consult = $Config.sm + "/" + $im
##Get im status
$getstatus = Invoke-RestMethod -Method Get -Uri $consult -Headers $headers -UseBasicParsing
$st = $getstatus.Incident | Select-Object status
$sts = $st.status
##Incidente is opem or Work
if($st -eq "Opem" -or $st -eq "Work in progress"){
##Message in Teams group to incident opem
$alert2 = ConvertTo-Json -Depth 1 @{
text = "`nService: $service Status: $serv Server: $servers Incident $im"
title = "Service $plataforma"
}
##Post message in teams
$response2 = Invoke-RestMethod -ContentType "application/json" -Method Post -body $alert2 -Uri $webHook
Write-Outpute $response2
}
##Incident is close this opem new incident
elseif ($sts -eq "Closed") {
##Json to parameters to API incident
$alert3 = ConvertTo-Json -Depth 1 @{
Incident = @{
category = $Config.category
company = $Config.company
alternatecontact = $Config.alternatecontact
assignmentgroup = $Config.assignmentgroup
impact = $Config.impact
configurationitem = $Config.configurationitem
producttype = $Config.producttype
symptom = $Config.symptom
subcategory = $Config.subcategory
briefdesc = "$servers $service"
urgency = $Config.urgency
description = "Alarm of $service status $serv server $servers"
}
}
##Post json in API to incidents
$response3 = Invoke-RestMethod -ContentType "application/json" -Method Post -body $alert3 -Uri $Config.sm -Headers $headers -UseBasicParsing
Write-Output $response3
}
##Get number to incident
$im = $response3.Incident | Select-Object number
$number = $im.number
##Create a texte file
$number | Out-File -FilePath $filepath
}
##Condition of opem incident with no exist other
Else{
##Json parameters of API incident
$alert4 = ConvertTo-Json -Depth 1 @{
Incident = @{
category = $Config.category
company = $Config.company
alternatecontact = $Config.alternatecontact
assignmentgroup = $Config.assignmentgroup
impact = $Config.impact
configurationitem = $Config.configurationitem
producttype = $Config.producttype
symptom = $Config.symptom
subcategory = $Config.subcategory
briefdesc = "$servers $service"
urgency = $Config.urgency
description = "Alarm of $service status $serv server $servers"
}
}
##Post incidente in Teams group
$response4 = Invoke-RestMethod -ContentType "application/json" -Method Post -body $alert4 -Uri $Config.sm -Headers $headers -UseBasicParsing
Write-Output $response4
##Select incident number
$im = $response4.Incident | Select-Object number
$number = $im.number
##Record number in texte file
$number | Out-File -FilePath $filepath
}
}
##Condition service is running
elseif ($serv -eq "Running") {
##Path of logs and history incidente opened
$dest = $Config.log + "\im" + (Get-Date).ToString('yyyyMMddHHmm') + ".txt"
##Copy incident opened in logs pah
Copy-Item -Path $filepath -Destination $dest
##Remove incidente existing
Get-ChildItem -Path $filepath | Remove-Item -Force
Write-Output "Remove incident"
}
}
}