Hello guys,
I’m trying to fetch the health status report from office 365 portal.
I am able to retrieve all the required parameters but when I try to add “$Message” (which contains a detailed description of multiple lines) to a single cell in output csv file, I am not able to. As it is adding each line in a separate cell.
In short, each line in the “$Message” parameter is being added to a new cell & not on the same cell.
I developed the below script.
Can someone suggest me how I can get the details in a single cell?
Thanks in advance.
Let me know if any further explanation is required.
$path=Split-Path -parent $MyInvocation.MyCommand.Definition
$MyCred = Get-Credential
$jsonPayload = (@{userName=$MyCred.username;password=$MyCred.GetNetworkCredential().password;} | convertto-json).tostring()
$cookie = (invoke-restmethod -contenttype “application/json” -method Post -uri “https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register” -body $jsonPayload).RegistrationCookie
$jsonPayload = (@{lastCookie=$cookie;locale=“en-US”;preferredEventTypes=@(0,1)} | convertto-json).tostring()
$events = (invoke-restmethod -contenttype “application/json” -method Post -uri “https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents” -body $jsonPayload)
$Issues = $events.Events
$outputFile =$path+“\O365_Health_status.csv”
#Check if output file is already present on the directory.
#Remove if it is exists
if (Test-Path $outputFile)
{
Remove-Item $outputFile
}
#Defining the output file headers
$output=“Title, ServiceName, status, starttime, EndTime, LastUpdatedTime, id, AffectedTenantCount, Message `n”
foreach($Issue in $Issues)
{
$detail = $Issue
$ServiceName = ($detail.AffectedServiceHealthStatus).ServiceName
$Title = $detail.Title
$status = $detail.Status
$starttime = $detail.StartTime
$Message =String.MessageText
$AffectedTenantCount = $detail.AffectedTenantCount
$EndTime =$detail.EndTime
$id = $detail.Id
$LastUpdatedTime = $detail.LastUpdatedTime
$output = $output+$Title+“,”+$ServiceName+“,”+$status+“,”+$starttime+“,”+$EndTime+“,”+$LastUpdatedTime+“,”+$id+“,”+$AffectedTenantCount+“,”+$Message+“`n”
Clear-Variable Title, ServiceName, status, starttime, EndTime, LastUpdatedTime, id, AffectedTenantCount, Message
}
Add-Content $outputFile $output
Write-Host "Script execution completed. The output file is saved at " $path -ForegroundColor Green