Skip to content

Commit 53ba2d1

Browse files
authored
Update AzMig_Dependencies.psm1 (Azure#320)
* Update AzMig_Dependencies.psm1 Changed TokenCache behaviour now that is depricated and removed redundant assignment of the TokenCacheItem variable. * Update AzMig_Dependencies.psm1 Changed to use token directly from Get-AzAccessToken and removed redundant code. * Update AzMig_Dependencies.psm1 Added verification for Az.Accounts module to be at least 2.2.0. * Update README.md Added requirement for Az.Accounts 2.2.0 or higher.
1 parent 0dbf224 commit 53ba2d1

File tree

2 files changed

+11
-29
lines changed

2 files changed

+11
-29
lines changed

azure-migrate/dependencies-at-scale/AzMig_Dependencies.psm1

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ function GetRequestProperties()
1717
Import-Module Az.Accounts
1818
}
1919

20+
if ((Get-Module Az.Accounts).Version -lt "2.2.0") {
21+
throw "At least Az.Accounts 2.2.0 is required, please update before continuing."
22+
}
23+
2024
$CurrentContext = Get-AzContext
2125
if (-not $CurrentContext) {
2226
throw "Not logged in. Use Connect-AzAccount to log in"
@@ -44,35 +48,12 @@ function GetRequestProperties()
4448
New-Variable -Name ResourceURL -Value "https://management.core.windows.net/" -Option Constant
4549
}
4650

47-
$TokenCache = $CurrentContext.TokenCache
48-
if (-not $TokenCache) {
49-
throw "Missing or incomaptible module version. Get the latest version of the Az.Accounts module"
50-
}
51-
52-
$TokenCacheItem = $TokenCache.ReadItems() |
53-
Where-Object -Property TenantId -eq $TenantId |
54-
Where-Object -Property DisplayableId -eq $UserId |
55-
Where-Object -Property Resource -eq $ResourceURL
56-
if (-not $TokenCacheItem) {
57-
throw "Something went wrong. Open a new PowerShell session, login with Connect-AzAccount and then try again"
58-
}
59-
60-
$authorityURL = $CurrentContext.Environment.ActiveDirectoryAuthority +$TenantId
61-
$AuthContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($authorityURL, $TokenCache)
62-
63-
if (-not $AuthContext) {
64-
throw "Couldn't get Auth Context. ADAL Library version missing or incompatible. Get the latest version of the Az.Accounts module"
65-
}
66-
67-
$Task = $AuthContext.AcquireTokenSilentAsync($ResourceURL, "1950a258-227b-4e31-a9cf-717495945fc2")
68-
while (-not $Task.AsyncWaitHandle.WaitOne(200)) {}
69-
$Result = $Task.GetAwaiter().GetResult()
70-
71-
if((-not $Result) -or (-not $Result.AccessToken)) {
72-
throw "Something went wrong while fetching access token"
51+
$Token = (Get-AzAccessToken -ResourceUrl $ResourceURL -TenantId $TenantId).Token
52+
if (-not $Token) {
53+
throw "Missing token, please make sure you are signed in."
7354
}
7455

75-
$AuthorizationHeader = "Bearer " + $Result.AccessToken
56+
$AuthorizationHeader = "Bearer " + $Token
7657
$Headers = [ordered]@{Accept = "application/json"; Authorization = $AuthorizationHeader}
7758

7859
if($Environment -eq "AzureUSGovernment") {

azure-migrate/dependencies-at-scale/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Before you get started, you need to do the following:
1919
- Ensure that VMware Tools (later than 10.2) is installed on each VM you want to analyze.
2020
- Ensure you have downloaded the AzMig_Dependencies powershell module in this folder.
2121
- Ensure you have PowerShell available. It is recommended you use the [latest version of PowerShell](https://docs.microsoft.com/powershell/scripting/install/installing-powershell).
22-
- Ensure you have the [Azure PowerShell](https://docs.microsoft.com/powershell/azure/install-az-ps) module installed.
22+
- Ensure you have the [Azure PowerShell](https://docs.microsoft.com/powershell/azure/install-az-ps) module installed.
23+
- The Az.Accounts module is required and must be at least version 2.2.0 or higher.
2324

2425
## Log in to Azure
2526

@@ -96,4 +97,4 @@ The Network Connections chart and the Source server name, Destination server nam
9697
4. Visualize the map of network connections filtering by servers and processes.
9798
> Below is an example output when the data is visualized within PowerBI
9899
99-
![PowerShell Output](images/PowerBI_example_output.jpg)
100+
![PowerShell Output](images/PowerBI_example_output.jpg)

0 commit comments

Comments
 (0)