Purview-API-PowerShell.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 5fd81e92-c46d-45ea-b591-86524bf24498 .AUTHOR abandyop (arindamba@microsoft.com) .COMPANYNAME Microsoft .COPYRIGHT Microsoft .TAGS .LICENSEURI https://opensource.org/licenses/MIT .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES Release Version 1.0 .PRIVATEDATA .DESCRIPTION PowerShell 7.x Interface To Execute Azure Purview APIs - Control and Data Plane APIs. Supported APIs List : https://github.com/Azure/Azure-Purview-API-PowerShell/blob/main/Purview_API_Reference.csv #> ### Purview API via PowerShell ### ## Arindam Bandyopadhyay MSFT (arindamba@microsoft.com) ## param ( [Parameter(Mandatory=$false)] [string]$TenantId, [Parameter(Mandatory=$false)] [string]$SubscriptionId, [Parameter(Mandatory=$true)] [string]$PurviewAccountName, [Parameter(Mandatory=$false)] [switch]$ServicePrincipal, [Parameter(Mandatory=$false)] [string]$ApplicationID ) ############ Functions -------------------------------------------------------------------------------------------------------------------------------------------------------------------- function ReplaceRESTParametersInURI ([string] $URI) { $APIRefCSVParamsFirstColumn = $APIRefCSVParams | select -Unique -ExpandProperty API_PARAMETER foreach ($MatchParam IN $APIRefCSVParamsFirstColumn) { if ($URI -match $MatchParam) { if ($MatchParam -eq "{subscriptionId}") { $ParamValue = $SubscriptionId } elseif ($MatchParam -eq "{accountName}") { $ParamValue = $PurviewAccountName } else { $ParamValue = $(Write-Host "$MatchParam=" -ForegroundColor DarkCyan -NoNewLine; Read-Host) } $URI = $URI -replace $MatchParam,$ParamValue } } Write-Host "API Request :-> " -NoNewLine Write-Host $PurviewAPIEndpointURL -ForegroundColor DarkGreen WHILE ($URI -match '{.*}') { Write-Host "Enter Any Additional Parameters In name=value Format :-> " $OptionalParams = Read-Host $URI = $URI -replace $MatchParam,$ParamValue } if ($URI -contains "?" ) { $URI = $URI + "&api-version=2021-07-01" } else { $URI = $URI + "?&api-version=2021-07-01" } RETURN $URI } ############ Checking Login Methods -------------------------------------------------------------------------------------------------------------------------------------------------------------------- if ($PurviewAccountName) {} else { $PurviewAccountName = Read-Host "Please Enter Your Purview Account/Catalog Name " } if ($TenantId -and $SubscriptionId) { Set-AzContext -Tenant $TenantId -Subscription $SubscriptionId } elseif ($TenantId) { Set-AzContext -Tenant $TenantId } elseif ($SubscriptionId) { Set-AzContext -Subscription $SubscriptionId } $TenantId = (Get-AzContext).Tenant.Id $SubscriptionId = (Get-AzContext).Subscription.Id if (-Not($TenantId) -OR -Not($SubscriptionId)) { Connect-AzAccount $TenantId = (Get-AzContext).Tenant.Id $SubscriptionId = (Get-AzContext).Subscription.Id } Save-AzProfile -Path .\MyAzureProfile.JSON -Force ############ Login Completed + Get Access Token ------------------------------------------------------------------------------------------------------------------------------------------------------------------ $AccessTokenDataMgmt = (Get-AzAccessToken -ResourceUrl "https://management.azure.com") $AccessTokenData = (Get-AzAccessToken -ResourceUrl "https://purview.azure.net") Write-Output $AccessTokenData $APIRefCSV = Import-Csv -Path Purview_API_Reference.csv $APIRefCSVParams = Import-Csv -Path Purview_API_Reference_Parameters.csv ############ Repeat Section Below To Run APIs -------------------------------------------------------------------------------------------------------------------------------------------------------------------- DO { DO { $Mode = $(Write-Host "Enter : [G] GUI - Interactive Assistance On Help, Usage & Syntax of Purview APIs [T] Text Mode - Enter API Command(s) Manually [Q] Quit :: " -ForegroundColor Cyan -NoNewLine; Read-Host) } WHILE ($Mode -notin ("q","Q","g","G","t","T")) if ($Mode.SubString(0,1) -in ("q","Q")) { Exit 1 } if ($Mode.SubString(0,1) -in ("g","G")) { $Mode = "G" } else { $Mode = "T" } if ($Mode -eq "G") { $SelectedAPI = $APIRefCSV | Out-GridView -Title "Execute Purview API" -OutputMode Single $APICategory = $SelectedAPI | Select-Object -ExpandProperty Category $APICommand = $SelectedAPI | Select-Object -ExpandProperty Command } else { $ValidAPICategories = $APIRefCSV | select -Unique -ExpandProperty Category DO { $APICategory = $(Write-Host "Enter API Category : Valid Values ( $ValidAPICategories ) : " -ForegroundColor Yellow -NoNewLine; Read-Host) } WHILE ($APICategory -notin $ValidAPICategories) $ValidAPICommands = $APIRefCSV | Where-Object {$_.Category -eq $APICategory} | select -ExpandProperty Command DO { $APICommand = $(Write-Host "Enter API Command : Valid Values ( $ValidAPICommands ) : " -ForegroundColor DarkYellow -NoNewLine; Read-Host) } WHILE ($APICommand -notin $ValidAPICommands) } $APISelectedRow = $APIRefCSV | Where-Object {$_.Category -eq $APICategory -and $_.Command -eq $APICommand} $APIDomain = $APISelectedRow | select -ExpandProperty APIURIDomain; if ($APICategory -ne "management") { $APIDomain = $PurviewAccountName + $APIDomain } IF ($APIDomain -contains "management.azure") {$AccessTokenData = $AccessTokenDataMgmt} $APIURIPath = $APISelectedRow | select -ExpandProperty APIURIPath $HTTPMethod = $APISelectedRow | select -ExpandProperty Method $PurviewAPIEndpointURL = ReplaceRESTParametersInURI ($URI="https://"+$APIDomain+$APIURIPath) Write-Host "Domain : " -NoNewLine Write-Host $APIDomain -ForegroundColor DarkGreen Write-Host "Ready To Execute ::::::: API Request :-> $HTTPMethod" -NoNewLine Write-Host $PurviewAPIEndpointURL -ForegroundColor DarkGreen IF ($HTTPMethod -in ("PUT","POST")){ Write-Host "HTTP Body (Payload) : Sending File (purview-api-body-payload.json) as HTTP API Body..." -ForegroundColor DarkYellow Try { $HTTPBody = Get-Content purview-api-body-payload.json -ErrorAction Stop } Catch { Write-Host "Could Not Locate File (purview-api-body-payload.json) IN Curent Directory OR The File Contains Invalid JSON. Sending Empty Body." -ForegroundColor DarkYellow $HTTPBody = @{} }} $Headers = @{} $Headers.Add("Accept","*/*") $Headers.Add("User-Agent","Windows PowerShell 7.x Purview API PS") $Headers.Add("Authorization","Bearer $($AccessTokenData.Token)") $Headers.Add("Content-Type","application/json") $timer = [System.Diagnostics.Stopwatch]::StartNew() Write-Host "Invoking API : Sending Request ... " -ForegroundColor Green Try { $PurviewAPIResponse = Invoke-RestMethod -Method $HTTPMethod -Uri $PurviewAPIEndpointURL -Headers $Headers -Body $HTTPBody } Catch { Write-Host $_ :-> $_.Exception } $timer.Stop() Write-Host "API Response Received :-> " -ForegroundColor Green Write-Output $PurviewAPIResponse | ConvertTo-JSON -Depth 100 Write-Host "API Latency / Time Elapsed :-> " -ForegroundColor Green Write-Output $timer.Elapsed $PurviewAPIResponse = "" } WHILE (1) ############## END -------------------------------------------------------------------------------------------------------------------------------------------------------------------- # Rudimentary way of fetching Access Token #$PostBody = @{grant_type='client_credentials'; client_id='servicePrincipalId'; client_secret='servicePrincipalKey'; resource="https://purview.azure.net"} | ConvertTo-Json #$AccessTokenData = Invoke-WebRequest -UseBasicParsing https://login.windows.net/$TenantId/oauth2/token -ContentType "application/json" -Method POST -Body $PostBody # # Future Support for Service Principal # If -ServicePricipal = TRUE, which is when user wants to login via Service Principal, user must Enter "ApplicationID" for the username and "ServicePrincipalSecret" as the password. # Otherwise script will prompt for service principal credentials and stores them for the session. Enter your application ID for the username and service principal secret as the password when prompted. # The ServicePrincipal switch parameter indicates that the account authenticates as a service principal. |