github.com/GoogleCloudPlatform/compute-image-tools/cli_tools@v0.0.0-20240516224744-de2dabc4ed1b/gce_windows_upgrade/upgrade_script.ps1 (about)

     1  #  Copyright 2020 Google Inc. All Rights Reserved.
     2  #
     3  #  Licensed under the Apache License, Version 2.0 (the "License");
     4  #  you may not use this file except in compliance with the License.
     5  #  You may obtain a copy of the License at
     6  #
     7  #      http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  #  Unless required by applicable law or agreed to in writing, software
    10  #  distributed under the License is distributed on an "AS IS" BASIS,
    11  #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  #  See the License for the specific language governing permissions and
    13  #  limitations under the License.
    14  
    15  function Get-MetadataValue {
    16    param (
    17      [parameter(Mandatory=$true)]
    18      [string]$key
    19    )
    20  
    21    # Returns the provided metadata value for a given key.
    22    $url = "http://metadata.google.internal/computeMetadata/v1/instance/attributes/$key"
    23    $max_attemps = 5
    24    for ($i=0; $i -le $max_attemps; $i++) {
    25      try {
    26        $client = New-Object Net.WebClient
    27        $client.Headers.Add('Metadata-Flavor', 'Google')
    28        $value = ($client.DownloadString($url)).Trim()
    29        Write-Host "Retrieved metadata for key $key with value $value."
    30        return $value
    31      }
    32      catch [System.Net.WebException] {
    33        # Sleep after each failure with no default value to give the network adapters time to become functional.
    34        Start-Sleep -s 1
    35      }
    36    }
    37    throw "Failed $max_attemps times to retrieve value from metadata for $key."
    38  }
    39  
    40  $script:install_media_drive = ''
    41  
    42  try {
    43    Write-Host 'Beginning upgrade startup script.'
    44  
    45    $script:expected_current_version = Get-MetadataValue -key 'expected-current-version'
    46    $script:expected_new_version = Get-MetadataValue -key 'expected-new-version'
    47    $script:install_folder = Get-MetadataValue -key 'install-folder'
    48  
    49    # Cleanup garbage files left by the previous failed upgrade to unblock a new upgrade.
    50    Remove-Item 'C:\$WINDOWS.~BT' -Recurse -ErrorAction SilentlyContinue
    51  
    52    $ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ProductName
    53    if ($ver -ne $script:expected_current_version) {
    54      if ($ver -eq $script:expected_new_version) {
    55        Write-Host "The instance is already running $script:expected_new_version!"
    56        Write-Host "Rerunning upgrade.ps1 as the post-upgrade step."
    57      } else {
    58        throw "The instance is not running $script:expected_current_version. It is '$ver'."
    59      }
    60    }
    61  
    62    # Bring all disks online to ensure install media is accessible.
    63    $Disks = Get-WmiObject Win32_DiskDrive
    64    foreach ($Disk in $Disks)
    65    {
    66      $DiskID = $Disk.index
    67      $DiskPartScript = @"
    68  select disk $DiskID
    69  online disk noerr
    70  "@
    71      $DiskPartScript | diskpart
    72    }
    73  
    74    # Find the drive which contains install media.
    75    $Drives = Get-WmiObject Win32_LogicalDisk
    76    ForEach ($Drive in $Drives) {
    77      if (Test-Path "$($Drive.DeviceID)\$script:install_folder") {
    78        $script:install_media_drive = "$($Drive.DeviceID)"
    79      }
    80    }
    81    if (!$script:install_media_drive) {
    82      throw "No install media found."
    83    }
    84    Write-Host "Detected install media folder drive letter: $script:install_media_drive"
    85  
    86    # Run upgrade script from the install media.
    87    Set-ExecutionPolicy Unrestricted
    88    Set-Location "$($script:install_media_drive)/$script:install_folder"
    89    ./upgrade.ps1
    90    $new_ver=(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ProductName
    91    if ($new_ver -eq $script:expected_new_version)
    92    {
    93      Write-Host "post-upgrade step is done."
    94    }
    95    Write-Host "windows_upgrade_current_version='$new_ver'"
    96  }
    97  catch {
    98    Write-Host "UpgradeFailed: $($_.Exception.Message)"
    99    exit 1
   100  }