Share via


Listing IP Addresses from Azure Subscription

Following examples shows how to use Azure REST API to confirm assigned/active IP Addresses:

  1. Create SP and allow rights into your Azure Subscription (IAM)
  2. Update the below script with the following variables (tenant, client id, clientsecret, subscriptionid guid)

IP Address Output for Azure RM VMs, Azure WebSites using REST API lookup.

image2

Reference:
https://docs.microsoft.com/en-us/rest/api/appservice/webapps
https://docs.microsoft.com/en-us/rest/api/network/public-ip-addresses

  1 2 3 4 5 6 7 8 9101112131415161718192021222324252627282930313233343536373839404142434445
 # Vars $Tenant="[tenantname].onmicrosoft.com"$ClientID="Azure AD Client APP"$ClientSecret="Azure AD Client Secret/key"$Subscriptionid="[azure subscription guid]"# Generate Token that will be used for API access$Token = Invoke-RestMethod -Uri https://login.microsoftonline.com/$tenant/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = $ClientID; "client_secret" = $ClientSecret}# API Endpoint we are accessing $APIURL="https://management.azure.com/subscriptions/$SubscriptionID/providers/Microsoft.Network/publicIPAddresses?api-version=2017-03-01"$IPRequest = Invoke-RestMethod -Header @{'authorization'="Bearer $($Token.access_token)"} -URI $APIURL -UserAgent 'application/json'foreach ($vm in $IPrequest.value){    Write-host "Arm based VMs" -ForegroundColor Yellow    Write-host $vm.name `0 $VM.properties.ipaddress `0 $vm.properties.publicIPAllocationMethod    Write-host ""    } $APIURL="https://management.azure.com/subscriptions/$SubscriptionID/providers/Microsoft.Web/sites?api-version=2016-08-01"$WebApprequest = Invoke-RestMethod -Header @{'authorization'="Bearer $($Token.access_token)"} -URI $APIURL -UserAgent 'application/json'foreach ($Website in $WebApprequest){    Write-host "Websites" -ForegroundColor Yellow    Write-host $Website.value.properties.hostnames `0 $Website.value.properties.outboundIpAddresses    }<##classic $VMhttps://management.azure.com/subscriptions/$subscriptionid/resourceGroups/vm3343321/providers/Microsoft.ClassicCompute/virtualMachines?api-version=2016-11-01$iprequest.value.properties.domainname.name$iprequest.value.properties.instanceview.publicipaddresses#>

image1