O365 API Connection With Authorization through Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-2174257%22%20slang%3D%22en-US%22%3EO365%20API%20Connection%20With%20Authorization%20through%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2174257%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20using%20the%20below%20PowerShell%20Script%20to%20create%20O365%20API%20Connection%20with%20Authorization.%20It%20is%20working%20as%20expected%20for%201%20API%20Connections.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%20I%20need%20to%20Create%20Multiple%20O365%20connections%20with%20authorization.%20I%20need%20to%20pass%20all%20the%20inputs%20through(api%2Capiname%2Cresourcegroup%2Ccreds%20for%20authentication)%20through%20CSV%20and%20execute%20the%20script.%20Now%20all%20the%20inputs%20are%20passed%20in%20parameter%20as%20below.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EParam(%3CBR%20%2F%3E%5Bstring%5D%20%24ResourceGroupName%20%3D%20'Test'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24ResourceLocation%20%3D%20'eastus'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24api%20%3D%20'office365'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24ConnectionName%20%3D%20'Test'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24subscriptionId%20%3D%20''%2C%3CBR%20%2F%3E%5Bbool%5D%20%24createConnection%20%3D%20%24true%3CBR%20%2F%3E)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20to%20pass%20all%20this%20values%20in%20CSV.%20Please%20help%20me%20on%20this.%3C%2FP%3E%3CP%3EAttaching%20script%20for%20the%20reference%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EScript%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EParam(%3CBR%20%2F%3E%5Bstring%5D%20%24ResourceGroupName%20%3D%20'Test'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24ResourceLocation%20%3D%20'eastus'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24api%20%3D%20'office365'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24ConnectionName%20%3D%20'Test'%2C%3CBR%20%2F%3E%5Bstring%5D%20%24subscriptionId%20%3D%20''%2C%3CBR%20%2F%3E%5Bbool%5D%20%24createConnection%20%3D%20%24true%3CBR%20%2F%3E)%3CBR%20%2F%3E%23region%20mini%20window%2C%20made%20by%20Scripting%20Guy%20Blog%3CBR%20%2F%3EFunction%20Show-OAuthWindow%20%7B%3CBR%20%2F%3EAdd-Type%20-AssemblyName%20System.Windows.Forms%3CBR%20%2F%3E%3CBR%20%2F%3E%24form%20%3D%20New-Object%20-TypeName%20System.Windows.Forms.Form%20-Property%20%40%7BWidth%3D600%3BHeight%3D800%7D%3CBR%20%2F%3E%24web%20%3D%20New-Object%20-TypeName%20System.Windows.Forms.WebBrowser%20-Property%20%40%7BWidth%3D580%3BHeight%3D780%3BUrl%3D(%24url%20-f%20(%24Scope%20-join%20%22%2520%22))%20%7D%3CBR%20%2F%3E%24DocComp%20%3D%20%7B%3CBR%20%2F%3E%24Global%3Auri%20%3D%20%24web.Url.AbsoluteUri%3CBR%20%2F%3Eif%20(%24Global%3AUri%20-match%20%22error%3D%5B%5E%26amp%3B%5D*%7Ccode%3D%5B%5E%26amp%3B%5D*%22)%20%7B%24form.Close()%20%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24web.ScriptErrorsSuppressed%20%3D%20%24true%3CBR%20%2F%3E%24web.Add_DocumentCompleted(%24DocComp)%3CBR%20%2F%3E%24form.Controls.Add(%24web)%3CBR%20%2F%3E%24form.Add_Shown(%7B%24form.Activate()%7D)%3CBR%20%2F%3E%24form.ShowDialog()%20%7C%20Out-Null%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%23endregion%3C%2FP%3E%3CP%3E%23login%20to%20get%20an%20access%20code%3C%2FP%3E%3CP%3ELogin-AzureRmAccount%3C%2FP%3E%3CP%3E%23select%20the%20subscription%3C%2FP%3E%3CP%3E%24subscription%20%3D%20Select-AzureRmSubscription%20-SubscriptionId%20%24subscriptionId%3C%2FP%3E%3CP%3E%23if%20the%20connection%20wasn't%20alrady%20created%20via%20a%20deployment%3CBR%20%2F%3Eif(%24createConnection)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%24connection%20%3D%20New-AzureRmResource%20-Properties%20%40%7B%22api%22%20%3D%20%40%7B%22id%22%20%3D%20%22subscriptions%2F%22%20%2B%20%24subscriptionId%20%2B%20%22%2Fproviders%2FMicrosoft.Web%2Flocations%2F%22%20%2B%20%24ResourceLocation%20%2B%20%22%2FmanagedApis%2F%22%20%2B%20%24api%7D%3B%20%22displayName%22%20%3D%20%24ConnectionName%3B%20%7D%20-ResourceName%20%24ConnectionName%20-ResourceType%20%22Microsoft.Web%2Fconnections%22%20-ResourceGroupName%20%24ResourceGroupName%20-Location%20%24ResourceLocation%20-Force%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%23else%20(meaning%20the%20conneciton%20was%20created%20via%20a%20deployment)%20-%20get%20the%20connection%3CBR%20%2F%3Eelse%7B%3CBR%20%2F%3E%24connection%20%3D%20Get-AzureRmResource%20-ResourceType%20%22Microsoft.Web%2Fconnections%22%20-ResourceGroupName%20%24ResourceGroupName%20-ResourceName%20%24ConnectionName%3CBR%20%2F%3E%7D%3CBR%20%2F%3EWrite-Host%20%22connection%20status%3A%20%22%20%24connection.Properties.Statuses%5B0%5D%3C%2FP%3E%3CP%3E%24parameters%20%3D%20%40%7B%3CBR%20%2F%3E%22parameters%22%20%3D%20%2C%40%7B%3CBR%20%2F%3E%22parameterName%22%3D%20%22token%22%3B%3CBR%20%2F%3E%22redirectUrl%22%3D%20%22%3CA%20href%3D%22https%3A%2F%2Fema1.exp.azure.com%2Fema%2Fdefault%2Fauthredirect%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fema1.exp.azure.com%2Fema%2Fdefault%2Fauthredirect%3C%2FA%3E%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%23get%20the%20links%20needed%20for%20consent%3CBR%20%2F%3E%24consentResponse%20%3D%20Invoke-AzureRmResourceAction%20-Action%20%22listConsentLinks%22%20-ResourceId%20%24connection.ResourceId%20-Parameters%20%24parameters%20-Force%3C%2FP%3E%3CP%3E%24url%20%3D%20%24consentResponse.Value.Link%3C%2FP%3E%3CP%3E%23prompt%20user%20to%20login%20and%20grab%20the%20code%20after%20auth%3CBR%20%2F%3EShow-OAuthWindow%20-URL%20%24url%3C%2FP%3E%3CP%3E%24regex%20%3D%20'(code%3D)(.*)%24'%3CBR%20%2F%3E%24code%20%3D%20(%24uri%20%7C%20Select-string%20-pattern%20%24regex).Matches%5B0%5D.Groups%5B2%5D.Value%3CBR%20%2F%3EWrite-output%20%22Received%20an%20accessCode%3A%20%24code%22%3C%2FP%3E%3CP%3Eif%20(-Not%20%5Bstring%5D%3A%3AIsNullOrEmpty(%24code))%20%7B%3CBR%20%2F%3E%24parameters%20%3D%20%40%7B%20%7D%3CBR%20%2F%3E%24parameters.Add(%22code%22%2C%20%24code)%3CBR%20%2F%3E%23%20NOTE%3A%20errors%20ignored%20as%20this%20appears%20to%20error%20due%20to%20a%20null%20response%3C%2FP%3E%3CP%3E%23confirm%20the%20consent%20code%3CBR%20%2F%3EInvoke-AzureRmResourceAction%20-Action%20%22confirmConsentCode%22%20-ResourceId%20%24connection.ResourceId%20-Parameters%20%24parameters%20-Force%20-ErrorAction%20Ignore%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%23retrieve%20the%20connection%3CBR%20%2F%3E%24connection%20%3D%20Get-AzureRmResource%20-ResourceType%20%22Microsoft.Web%2Fconnections%22%20-ResourceGroupName%20%24ResourceGroupName%20-ResourceName%20%24ConnectionName%3CBR%20%2F%3EWrite-Host%20%22connection%20status%20now%3A%20%22%20%24connection.Properties.Statuses%5B0%5D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2176717%22%20slang%3D%22en-US%22%3ERe%3A%20O365%20API%20Connection%20With%20Authorization%20through%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2176717%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F300973%22%20target%3D%22_blank%22%3E%40StefanIvemo%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EHi%20Stephen%2C%3CBR%20%2F%3E%3CBR%20%2F%3ECan%20you%20please%20help%20us%20on%20this.%3C%2FLINGO-BODY%3E
Contributor

Hi All,

 

I am using the below PowerShell Script to create O365 API Connection with Authorization. It is working as expected for 1 API Connections.

 

Now I need to Create Multiple O365 connections with authorization. I need to pass all the inputs through(api,apiname,resourcegroup,creds for authentication) through CSV and execute the script. Now all the inputs are passed in parameter as below.

 

Param(
[string] $ResourceGroupName = 'Test',
[string] $ResourceLocation = 'eastus',
[string] $api = 'office365',
[string] $ConnectionName = 'Test',
[string] $subscriptionId = '',
[bool] $createConnection = $true
)

 

I need to pass all this values in CSV. Please help me on this.

Attaching script for the reference

 

Script:

 

Param(
[string] $ResourceGroupName = 'Test',
[string] $ResourceLocation = 'eastus',
[string] $api = 'office365',
[string] $ConnectionName = 'Test',
[string] $subscriptionId = '',
[bool] $createConnection = $true
)
#region mini window, made by Scripting Guy Blog
Function Show-OAuthWindow {
Add-Type -AssemblyName System.Windows.Forms

$form = New-Object -TypeName System.Windows.Forms.Form -Property @{Width=600;Height=800}
$web = New-Object -TypeName System.Windows.Forms.WebBrowser -Property @{Width=580;Height=780;Url=($url -f ($Scope -join "%20")) }
$DocComp = {
$Global:uri = $web.Url.AbsoluteUri
if ($Global:Uri -match "error=[^&]*|code=[^&]*") {$form.Close() }
}
$web.ScriptErrorsSuppressed = $true
$web.Add_DocumentCompleted($DocComp)
$form.Controls.Add($web)
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() | Out-Null
}
#endregion

#login to get an access code

Login-AzureRmAccount

#select the subscription

$subscription = Select-AzureRmSubscription -SubscriptionId $subscriptionId

#if the connection wasn't alrady created via a deployment
if($createConnection)
{
$connection = New-AzureRmResource -Properties @{"api" = @{"id" = "subscriptions/" + $subscriptionId + "/providers/Microsoft.Web/locations/" + $ResourceLocation + "/managedApis/" + $api}; "displayName" = $ConnectionName; } -ResourceName $ConnectionName -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -Location $ResourceLocation -Force
}
#else (meaning the conneciton was created via a deployment) - get the connection
else{
$connection = Get-AzureRmResource -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -ResourceName $ConnectionName
}
Write-Host "connection status: " $connection.Properties.Statuses[0]

$parameters = @{
"parameters" = ,@{
"parameterName"= "token";
"redirectUrl"= "https://ema1.exp.azure.com/ema/default/authredirect"
}
}

#get the links needed for consent
$consentResponse = Invoke-AzureRmResourceAction -Action "listConsentLinks" -ResourceId $connection.ResourceId -Parameters $parameters -Force

$url = $consentResponse.Value.Link

#prompt user to login and grab the code after auth
Show-OAuthWindow -URL $url

$regex = '(code=)(.*)$'
$code = ($uri | Select-string -pattern $regex).Matches[0].Groups[2].Value
Write-output "Received an accessCode: $code"

if (-Not [string]::IsNullOrEmpty($code)) {
$parameters = @{ }
$parameters.Add("code", $code)
# NOTE: errors ignored as this appears to error due to a null response

#confirm the consent code
Invoke-AzureRmResourceAction -Action "confirmConsentCode" -ResourceId $connection.ResourceId -Parameters $parameters -Force -ErrorAction Ignore
}

#retrieve the connection
$connection = Get-AzureRmResource -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -ResourceName $ConnectionName
Write-Host "connection status now: " $connection.Properties.Statuses[0]

 

1 Reply
@StefanIvemo

Hi Stephen,

Can you please help us on this.