Format output from Powershell as JSON
Hi
I have a powershell script run in an automation account triggered from a Power Flow. I would like to take the output from the PS to manage the result in the Flow, but i keep getting things I dont want! The PowerShell creates and configure a new Teams:
*
Param(
[string]$teamName, #The name of the Team
[string]$teamOwner, #The email address of the owner of the Team
[string]$description, #The description of the Team
[string]$scope, #Internal or external
)
$mailNickname = $teamName.replace(" ","")
$runAs = Get-AutomationConnection -Name 'AzureRunAsConnection'
try
{
#Connect to Azure AD preview
AzureADPreview\Connect-AzureAD -TenantId $runAs.TenantId -ApplicationId $runAs.ApplicationId -CertificateThumbprint $runAs.CertificateThumbprint
#Connect to Teams
Connect-microsoftTeams -TenantId $runAs.TenantId -ApplicationId $runAs.ApplicationId -CertificateThumbprint $runAs.CertificateThumbprint
Write-Output -InputObject "{""Connection"": ""Successful"","
}
catch
{
if (!$runAs)
{
Write-Output -InputObject "{""Connection"": ""Failed - runAs.Name not found""}"
throw
}
else
{
Write-Output -InputObject "{""Connection"": ""Failed""}"
Write-Error -Message $_.Exception
throw
}
}>
<#------------SET UP TEAMS-------------------#>
try
{
$newTeam = New-Team -DisplayName $teamName -Owner $teamOwner -Visibility Private -MailNickName $mailNickname -Description $description -AllowAddRemoveApps $false -AllowCreateUpdateChannels $false -AllowCreateUpdateRemoveConnectors $false -AllowDeleteChannels $false
Write-Output -InputObject """CreateTeam"": ""Successful"","
}
catch
{
$e = $error[0].exception.message
Write-Output -InputObject """CreateTeam"": ""Failed $e""}"
throw
}
Write-Output -InputObject """TeamName"": ""$teamName"","
Write-Output -InputObject """TeamOwner"": ""$teamOwner"","
<#------------SET UP TEAMS GROUPS-------------------#>
try
{
#Getting O365 Group
$O365Group = Get-AzureADGroup -objectid $newTeam.GroupID
#$O365DisplayName = $O365Group.displayname
}
catch
{
Write-Output -InputObject """GetGroup"": "Failed","
}
Write-Output """TeamScope"": ""$scope"","
[bool]$allowGuests = $false
if ($scope -eq "External")
{
$allowGuests = $true
Write-Host """UpdateToExternalGroup"": ""True"","
}
try
{
$O365GroupSettings = Get-AzureADObjectSetting -TargetType Groups -TargetObjectId $O365Group.ObjectId
#Check if the Channel exists. This line will throw an exception if the group is not associated with a team.
#This is to ensure that we are disabling external access only for those groups which have teams associated.
if($O365GroupSettings)
{
$O365GroupSettings["AllowToAddGuests"] = $allowGuests
#Updating the Property to restrict or allow adding Guest User
Set-AzureADObjectSetting -Id $O365GroupSettings.Id -DirectorySetting $O365GroupSettings -TargetObjectId $O365Group.ObjectId -TargetType Groups
}
else
{
$template = Get-AzureADDirectorySettingTemplate | ? {$.displayname -eq "group.unified.guest"}
$settingsCopy = $template.CreateDirectorySetting()
$settingsCopy["AllowToAddGuests"]=$allowGuests
#Creating the Property and setting the value to restrict or allow adding Guest User
New-AzureADObjectSetting -TargetType Groups -TargetObjectId $O365Group.ObjectId -DirectorySetting $settingsCopy
}
Write-Output """UpdateGroupSettings"": ""Successful"","
}
catch
{
Write-Output """UpdateGroupSettings"": ""Failed"","
throw
}
if ($allowGuests)
{
try
{
Add-AzureADAdministrativeUnitMember -ObjectId "7b37ca05-38f6-4267-b510-044e6a6670ca" -RefObjectId $O365Group.ObjectId
Write-Output """AddToExternalO365GroupsAdminUnit"": ""Successful"","
}
catch
{
Write-Output """AddToExternalO365GroupsAdminUnit"": ""Failed"","
#throw
Write-Error -Message $.Exception
}
}
Write-Output "}"
*
And here is an example of the output:
*
Account : ###
Environment : AzureCloud
Tenant : ###
TenantId : ###
TenantDomain : ###.onmicrosoft.com
Account : ###
Environment : AzureCloud
Tenant : ###
TenantId : ###
TenantDomain : ###.onmicrosoft.com
{"Connection": "Successful",
"CreateTeam": "Successful",
"TeamName": "My Test Site",
"TeamOwner": "******@ourdomain.co.uk",
"TeamScope": "Internal",
Id DisplayName TemplateId Values
0024692d-###-42b8-9c05-b95a3### 08d542b9-071f-4e16-###-74abb372### {class SettingValue {...
"UpdateGroupSettings": "Successful",
}
*
As you can see from the above, there is a chunk in the middle of the formatted json that throws my script! How can i stop this happening? Is there a better way to achieve the json output i require (its only this that i need, not the opening text either!)
Thanks