internal/functions/resolve/Resolve-Subscription.ps1
function Resolve-Subscription { [CmdletBinding()] Param ( [Parameter(Mandatory = $true)] [string] $InputReference, [System.Management.Automation.PSCmdlet] $Cmdlet = $PSCmdlet ) begin { $InputReference = Resolve-String -Text $InputReference $secureStringToken = (Get-AzAccessToken -AsSecureString -ResourceUrl $script:apiBaseUrl).Token $token = [System.Net.NetworkCredential]::new('', $secureStringToken).Password } process { try { if ($InputReference -match $script:guidRegex) { $subscriptionId = ((Invoke-RestMethod -Method GET -uri "$($script:apiBaseUrl)subscriptions?$($script:apiVersion)" -Headers @{"Authorization"="Bearer $($token)"}).value | Where-Object {$_.subscriptionId -eq $InputReference}).id } else { $subscriptionId = ((Invoke-RestMethod -Method GET -uri "$($script:apiBaseUrl)subscriptions?$($script:apiVersion)" -Headers @{"Authorization"="Bearer $($token)"}).value | Where-Object {$_.displayname -eq $InputReference}).id } if ($subscriptionId.count -ne 1) {throw "Can not find subscription $($InputReference)"} return $subscriptionId } catch { Write-PSFMessage -Level Warning -String 'AzurePIM.CannotResolveResource' -StringValues "Subscription" -Tag 'failed' -ErrorRecord $_ $Cmdlet.ThrowTerminatingError($_) } } } |