Enviando o Service Health do Office 365 por e-mail através de um script Powershell

Olá a todos!
Nesse post irei demonstrar como utilizar um script em PowerShell para envio do Service Health do Office 365 por e-mail.
Para isso, iremos precisar do módulo O365ServiceCommunications, utilizado para recuperar dados da API do Office 365 Service Comunications.


Mãos a massa


O primeiro passo é instalar o módulo O365ServiceCommunications.
Fazemos isso através do comando:

PS C:\> Install-Module -Name O365ServiceCommunications
PS C:\>

Após instalado já podemos fazer alguns testes.
Lembrando que o usuário precisa ser administrador global para executar os comandos abaixo.
Conecte ao Tenant.

PS C:\Import-Module MSOnline 
PS C:\>$O365Cred = Get-Credential 
PS C:\>$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred -Authentication Basic -AllowRedirection 
PS C:\>Import-PSSession $O365Session -AllowClobber 
PS C:\>Connect-MsolService –Credential $O365Cred
PS C:\>

No exemplo abaixo utilizamos o a função Get-SCServiceInfo, que nos retorna informações sobre os serviços disponíveis no Tenant.

PS C:\> $session = New-SCSession -Credential $Credential
PS C:\> Get-SCServiceInfo -ScSession $Session

ServiceName                             FeatureNames
-----------                             ------------
Exchange Online                         {Sign-in, E-Mail and calendar access, E-Mail timely delivery, Management and...
Office Subscription                     {Licensing and Renewal, Network Availability, Office Professional Plus Downl...
Identity Service                        {Sign-In, Administration}
Office 365 Portal                       {Portal, Administration, Purchase and Billing}
Skype for Business                      {Audio and Video, Federation, Management and Provisioning, Sign-In...}
SharePoint Online                       {Provisioning, SharePoint Features, Tenant Admin, Search and Delve...}
Dynamics 365                            {Sign In, Sign up and administration, Organization access, Organization perf...
Azure Information Protection            {Azure RMS Available}
Yammer Enterprise                       {Yammer Components}
Mobile Device Management for Office 365 {Mobile Device Management}
Social Engagement                       {Sign in, Sign up and Administration, Solution Access, Solution Performance...}
Planner                                 {Planner}
Sway                                    {Sway}
Power BI                                {PowerBI.com}
Microsoft Intune                        {Microsoft Intune}
OneDrive for Business                   {OneDrive for Business}
Microsoft Teams                         {Teams Components}
Microsoft StaffHub                      {Service and web access issues, Web client issues, Android client issues, iO...

PS C:\>

No próximo exemplo iremos exibir os eventos do Office 365 ocorridos no Tenant.
Os tipos de eventos são: Incident, Maintenance, and Message.

PS C:\> Get-SCEvent -SCSession $Session -EventTypes Incident -PastDays 7
20/02/2018 23:30:00 22/02/2018 18:22:00 IT129640 Incident  Microsoft Intune   Service restored
21/02/2018 23:00:00 22/02/2018 17:50:00 MO129664 Incident  Office 365 Portal  Service restored
22/02/2018 01:00:00 27/02/2018 00:46:00 LY129669 Incident  Skype for Business Service restored
16/02/2018 16:00:00                     PB129675 Incident  Power BI           Investigating
22/02/2018 08:00:00 23/02/2018 04:33:02 YA129706 Incident  Yammer Enterprise  Service restored
22/02/2018 20:00:00 26/02/2018 21:16:43 EX129742 Incident  Exchange Online    Service restored
23/02/2018 18:01:00 23/02/2018 21:05:00 TM129753 Incident  Microsoft Teams    Service restored
23/02/2018 23:06:47                     SP129758 Incident  SharePoint Online  Service degradation
24/02/2018 05:02:00 25/02/2018 04:00:00 PB129790 Incident  Power BI           Service restored
26/02/2018 18:14:00 27/02/2018 08:49:00 IT131023 Incident  Microsoft Intune   Service restored
23/02/2018 03:00:00 26/02/2018 19:26:17 LY131025 Incident  Skype for Business Service restored
27/02/2018 03:09:00 27/02/2018 03:40:00 TM131058 Incident  Microsoft Teams    Service restored
27/02/2018 13:10:00 27/02/2018 13:36:00 SP131064 Incident  SharePoint Online  Service restored
27/02/2018 10:00:00 27/02/2018 14:55:00 PB131067 Incident  Power BI           False positive
26/02/2018 21:30:00                     IT131084 Incident  Microsoft Intune   Investigating
27/02/2018 15:00:00                     TM131085 Incident  Microsoft Teams    Service degradation
27/02/2018 18:00:00 27/02/2018 21:05:00 YA131086 Incident  Yammer Enterprise  Service restored
27/02/2018 19:55:00 27/02/2018 20:20:00 YA131097 Incident  Yammer Enterprise  Service restored
27/02/2018 20:59:40                     IT131100 Incident  Microsoft Intune   Service degradation
PS C:\>

 

Enviando por e-mail

 

Abaixo temos um script em PowerShell onde podemos por exemplo criar uma tarefa agendada que execute diariamente e envie os incidentes ocorridos nos últimos 7 dias.

O primeiro passo é gerar um arquivo com as credenciais, que será utilizado no script para realizar a conexão.

PS C:\>Get-Credential | Export-CliXml -Path c:\scripts\cred.xml 

Abaixo o Script.
Note que no script temos a descrição do Evento.

Import-Module O365ServiceCommunications
$Credential = Import-Clixml -Path "$PSScriptRoot\cred.xml"
$MySession = New-SCSession -Credential $Credential
$Events = Get-SCEvent -EventTypes Incident -PastDays 7 -SCSession $MySession |
Select-Object Id, Status, StartTime, EndTime,
@{n='ServiceName'; e={$_.AffectedServiceHealthStatus.servicename}},
@{n='Message';e={$_.messages[0].messagetext}}
if ($Events)
{
$Tables = foreach ($Event in $Events)
{
@"
<table style="width:100%" align="left">
<tr bgcolor="#BDB76B">
<th align="left">Id</th>
<th align="left">ServiceName</th>
<th align="left">Status</th>
<th align="left">StartTime</th>
<th align="left">EndTime</th>
</tr>
<tr>
<td>$($Event.Id)</td>
<td>$($Event.ServiceName)</td>
<td>$($Event.Status)</td>
<td>$($Event.StartTime)</td>
<td>$($Event.EndTime)</td>
</tr>
</table>
<table>
<tr>
<td>$($Event.Message)</td>
</tr>
</table>
<br>
<br>
<br>
"@
}
$Html = @"
<!DOCTYPE HTML>
<html>
<Head>
</head>
<body>
<table width="841" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<th width="455" scope="row"><img src="http://www.uptocloud.com/images/logo_uptocloud_ext.png" width="455" height="178"></th>
<td></td>
<td width="386"><font face="verdana" font size="6" color="red">Office 365 Service Health Alerts</font></td>
</tr>
</tbody>
</table>
<br>
$Tables
</body>
</html>
"@
$Splat = @{
SmtpServer = 'smtp.office365.com'
usessl = $true
Port = '587'
Credential = $Credential
Body = $Html
BodyAsHtml = $true
To = 'breno@uptocloud.com'
From = 'breno@uptocloud.com'
Subject = 'Office 365 Service Health Alerts'
Priority = 'High'
}
Send-MailMessage @Splat
}


Salve em um arquivo .ps1 e crie uma tarefa agendada.

Mais informações, acesse a documentação do O365ServiceCommunications no site do PowerShell Gallery: https://www.powershellgallery.com/packages/O365ServiceCommunications/1.4


Fico por aqui.

Grande abraço,

Breno Padovan.

Nenhum comentário: