# Basic authentication header using the personal access token (PAT) $basicAuth = ("{0}:{1}" -f "",$patToken) $basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth) $basicAuth = [System.Convert]::ToBase64String($basicAuth) $headers = @{Authorization=("Basic {0}" -f $basicAuth)}
# REST API Request to get all entitlements $request_GetEntitlements = "https://vsaex.dev.azure.com/" + $orgName + "/_apis/userentitlements?top=10000&api-version=5.1-preview.2";
# Send the REST API request and initialize the members array list. $response = Invoke-RestMethod-Uri$request_GetEntitlements-headers$headers-Method Get $response.items | ForEach-Object { $members.add($_.id) | out-null }
# Iterate through all user entitlements $response.items | ForEach-Object { $name = [string]$_.user.displayName; $date = [DateTime]$_.lastAccessedDate; $expired = Get-Date; $expired = $expired.AddMonths($months); $license = [string]$_.accessLevel.AccountLicenseType; $licenseName = [string]$_.accessLevel.LicenseDisplayName; $count++;
if ( $expired-gt$date ) {
# Ignore users who have NEVER or NOT YET ACTIVATED their license if ( $date.Year -eq1 ) { Write-Host" **INACTIVE** "" Name: "$name" Last Access: "$date"License: "$licenseName } # Look for BASIC license elseif ( $licenseName-eq$basic ) { Write-Host" **INACTIVE** "" Name: "$name" Last Access: "$date"License: "$licenseName } # Look for BASIC + TEST license elseif ( $licenseName-eq$basicTest ) { Write-Host" **INACTIVE** "" Name: "$name" Last Access: "$date"License: "$licenseName } } }
当你运行脚本时,你将得到以下输出,你可以将其转发给授权团队,以重置用户许可证:
1 2 3 4 5 6 7 8 9 10
**INACTIVE** Name: Demo1 Last Access: 2019/09/0611:01:26 AM License: Basic **INACTIVE** Name: Demo2 Last Access: 2019/06/0408:53:15 AM License: Basic **INACTIVE** Name: Demo3 Last Access: 2019/09/2612:54:57 PM License: Basic **INACTIVE** Name: Demo4 Last Access: 2019/06/0712:03:18 PM License: Basic **INACTIVE** Name: Demo5 Last Access: 2019/07/1810:35:11 AM License: Basic **INACTIVE** Name: Demo6 Last Access: 2019/10/0309:21:20 AM License: Basic **INACTIVE** Name: Demo7 Last Access: 2019/10/0211:45:55 AM License: Basic **INACTIVE** Name: Demo8 Last Access: 2019/09/2001:36:29 PM License: Basic + Test Plans **INACTIVE** Name: Demo9 Last Access: 2019/08/2810:58:22 AM License: Basic