AjKundnani / Gen1-TrustedLaunch

https://aka.ms/Gen1ToTLUpgrade
MIT License
4 stars 0 forks source link

Validate Pre-Requisites Section fails if $env:UserProfile not set #3

Closed cocallaw closed 1 month ago

cocallaw commented 1 month ago

When running the script if the user's environment does not have the environment variable $env:UserProfile set, the script will fail with an error message that does not helo the user understand the issue that they are experiencing.

PS /Users/USER/Documents/Source/Github/Gen1-TrustedLaunch> ./Upgrade-Gen1ToTL.ps1 -subscriptionId $subscriptionId -tenantDomain $tenantDomain -csvLocation "/Users/USER/Documents/Source/Github/Gen1-TrustedLaunch/vmList.csv"
PowerShell version is greater than 7.2
Setting up working dir \Gen1-TrustedLaunch-Upgrade
Error Exception Occurred
Cannot bind argument to parameter 'Path' because it is an empty string.
Error Caused By: New-Item -ItemType Directory -Path (Split-Path $workingDirectory -Parent) -Name (Split-Path $workingDirectory -Leaf) -ErrorAction 'Stop' | Out-Null

Adding if statement to check if the value of $env:UserProfile is not null or empty provides option to either log error and exit or set value to something else

if ([string]::IsNullOrEmpty($env:UserProfile)) { }

Example:

    if ($useCloudshell) {
        $workingDirectory = [system.string]::concat((Get-Location).Path, "/Gen1-TrustedLaunch-Upgrade")
    } else {
        if ([string]::IsNullOrEmpty($env:UserProfile)) {
            $messageTxt = "User Profile is not set. Please set the environment variable UserProfile and re-run the script."
            Write-Output $messageTxt
            exit
        }
        $workingDirectory = "$env:UserProfile\Gen1-TrustedLaunch-Upgrade"}
    if ((Test-Path $workingDirectory) -eq $true) {
        $messageTxt = "Working Directory Already Setup $workingDirectory"
        Write-Output $messageTxt
    }
    else {
        $messageTxt = "Setting up working dir $workingDirectory"
        Write-Output $messageTxt
        New-Item -ItemType Directory -Path (Split-Path $workingDirectory -Parent) -Name (Split-Path $workingDirectory -Leaf) -ErrorAction 'Stop' | Out-Null
    }

Output with if statement

PS /Users/USER/Documents/Source/Github/Gen1-TrustedLaunch> ./Upgrade-Gen1ToTL.ps1 -subscriptionId $subscriptionId -tenantDomain $tenantDomain -csvLocation "/Users/USER/Documents/Source/Github/Gen1-TrustedLaunch/vmList.csv"
PowerShell version is greater than 7.2
User Profile is not set. Please set the environment variable UserProfile and re-run the script.
AjKundnani commented 1 month ago

@cocallaw - Kindly validate the script version available in branch - issue/3

cocallaw commented 1 month ago

@AjKundnani I tested using the issue/3 branch and received the following output when the script failed, sensitive information removed

PS /Users/coreycallaway/Documents/Source/Github/Gen1-TrustedLaunch> ./Upgrade-Gen1ToTL.ps1 -subscriptionId $subscriptionId -tenantDomain $tenantDomain -csvLocation ./vmList.csv
PowerShell version is greater than 7.2
Test-Path: /Users/USER/Documents/Source/Github/Gen1-TrustedLaunch/Upgrade-Gen1ToTL.ps1:110
Line |
 110 |          if ((Test-Path $env:UserProfile)) {
     |               ~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Value cannot be null. (Parameter 'The provided Path argument was null or an empty collection.')
User profile directory not found. Defaulting to script execution location.
Setting up working dir /Users/USER/Documents/Source/Github/Gen1-TrustedLaunch\Gen1-TrustedLaunch-Upgrade
Module Az.Accounts with minimum version 2.8.0 is available.
Module Az.Compute with minimum version 6.0.0 is available.
WARNING: Module Az.Storage  is available, but its version is lower than the minimum version 5.8.0. Upgrading module on local machine.
Connecting to Subscription <AZURE SUB GUID> under <TENANT NAME>.onmicrosoft.com
WARNING: 11:09:25 AM - You may need to login again after updating "EnableLoginByWam".

Key              Value Applies To Scope       Help Message
---              ----- ---------- -----       ------------
EnableLoginByWam False Az         CurrentUser [Preview] When enabled, Web Account Manager (WAM) will be the default interactive login experience. It will fall back to using the browser if the platform does n…

Environments : {[AzureChinaCloud, AzureChinaCloud], [AzureUSGovernment, AzureUSGovernment], [AzureCloud, AzureCloud]}
Context      : Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext

Feature Gen1ToTLMigrationPreview is already registered.
Script Version: 2.1.1
Error Exception Occurred
Write-LogEntry()
Could not find a part of the path '/Gen1-TrustedLaunch-Upgrade/gen1avd-1-Gen1-TL-Upgrade-24.09.16-11.10.13.log'.
-1
Enable Secure Boot = True
Output Storage Account Name =
VM name = gen1avd-1
Use Signed Script =
Tenant Domain = <TENANT NAME>onmicrosoft.com
Subscription ID = <AZURE SUB GUID>
Resource group name = rg-avd-gen1-neu
Use Cloud Shell =
Error Exception Occurred
Write-LogEntry()
Could not find a part of the path '/Gen1-TrustedLaunch-Upgrade/gen1avd-1-Gen1-TL-Upgrade-24.09.16-11.10.13.log'.
-1
Processing VM gen1avd-1 under resource group rg-avd-gen1-neu with Secure boot TRUE
Error Exception Occurred
Write-LogEntry()
Could not find a part of the path '/Gen1-TrustedLaunch-Upgrade/gen1avd-1-Gen1-TL-Upgrade-24.09.16-11.10.13.log'.
-1
AjKundnani commented 1 month ago

@cocallaw - Added fix. Kindly check.

AjKundnani commented 1 month ago

Closing as fix is added for Windows machines without user profile. For Macintosh machines, request to leverage CloudShell to execute the orchestration.