Powershell Script to Create API Connections - O365

Copper Contributor

Hi,

 

We are using the powershell script to create O365 API connection. We are creating Multiple Connections using CSV file to get the inputs like API Name,RG Name,Location. Now we need to achieve the Authentication through the script and not manually. We are trying the below script to pass the creds in the script. but it is not working. We need to pass the creds as well through csv file.

 

Script:

 

$CSVPath = "C:\Temp\API\API.csv"
#Import File
$VMCreateFile = Import-Csv -Path $CSVPath
 
$password=ConvertTo-SecureString -String "password" -AsPlainText -Force
$credential=[System.Management.Automation.PSCredential]::new("UserID",$password)
 
#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
 
 
Foreach ($NewVM in $VMCreateFile)
{
 

$ResourceGroupName = $null
$ResourceLocation = $null
$ConnectionName = $null
$api= $null
$subscriptionId = $null
 
$ResourceGroupName = $NewVm.ResourceGroupName
$ResourceLocation = $NewVm.ResourceLocation
$ConnectionName = $NewVm.ConnectionName
$api= $NewVM.api
$subscriptionId = $NewVM.subscriptionId
 
#select the subscription
 
$subscription = Select-AzureRmSubscription -SubscriptionId $subscriptionId
$Existing_Resource=$null
$Existing_Resource=Get-AzureRmResource -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -ResourceName $ConnectionName -ErrorAction SilentlyContinue
#if the connection wasn't alrady created via a deployment
if($Existing_Resource -eq $null)
{
$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"
}
}
 
Connect-AzureRmAccount -Credential $credential
 
#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]
Disconnect-AzureRmAccount
}
0 Replies