Skip to content

Instantly share code, notes, and snippets.

@w3bward
Created April 21, 2022 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3bward/0364bce98bd8a8d0123539694d6ff907 to your computer and use it in GitHub Desktop.
Save w3bward/0364bce98bd8a8d0123539694d6ff907 to your computer and use it in GitHub Desktop.
A PowerShell class to implement the interface Microsoft.SqlServer.Management.Common.IRenewableToken
$ResourceUrl = 'https://database.windows.net'
class SmoRenewableToken : Microsoft.SqlServer.Management.Common.IRenewableToken {
[string]$Resource
[string]$Tenant
[DateTimeOffset]$TokenExpiry
[string]$UserId
[string] hidden $Token
SmoRenewableToken(
[System.Uri]$ResourceUrl
) {
$AzAccessToken = Get-AzAccessToken -ResourceUrl $ResourceUrl
$this.Tenant = $AzAccessToken.TenantId
$this.Resource = $ResourceUrl
$this.TokenExpiry = $AzAccessToken.ExpiresOn
$this.UserId = $AzAccessToken.UserId
$this.Token = $AzAccessToken.Token
}
[string]GetAccessToken() {
if ($this.TokenExpiry.AddMinutes(-10) -gt (Get-Date)) {
return $this.Token
}
$AzAccessToken = Get-AzAccessToken -ResourceUrl $this.Resource
$this.TokenExpiry = $AzAccessToken.ExpiresOn
$this.Tenant = $AzAccessToken.TenantId
$this.UserId = $AzAccessToken.UserId
return $AzAccessToken.Token
}
<#
Despite some documentation stating otherwise, it appears that PowerShell v5.1 does not automatically implement accessors (get/set) that the interface
recognizes. When the following methods were omitted, this function threw errors along the lines of "get_TokenExpiry... does not have an implementation"
At some point, this issue was fixed in PowerShell Core, and testing in 7.2.1 revealed that these lines could be safely omitted, but their presence does
not appear to cause any problems either. For the sake of compatibility, they stay.
#>
[DateTimeOffset]get_TokenExpiry() {
return $this.TokenExpiry
}
[string]get_Resource() {
return $this.Resource
}
[string]get_Tenant() {
return $this.Tenant
}
[string]get_UserId() {
return $this.UserId
}
}
New-Object SmoRenewableToken -ArgumentList $ResourceUrl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment