github.com/hernad/nomad@v1.6.112/e2e/terraform/packer/windows-2016-amd64/disable-windows-updates.ps1 (about)

     1  # Copyright (c) HashiCorp, Inc.
     2  # SPDX-License-Identifier: MPL-2.0
     3  
     4  $RunningAsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
     5  if (!$RunningAsAdmin) {
     6    Write-Error "Must be executed in Administrator level shell."
     7    exit 1
     8  }
     9  
    10  $service = Get-WmiObject Win32_Service -Filter 'Name="wuauserv"'
    11  
    12  if (!$service) {
    13    Write-Error "Failed to retrieve the wauserv service"
    14    exit 1
    15  }
    16  
    17  if ($service.StartMode -ne "Disabled") {
    18    $result = $service.ChangeStartMode("Disabled").ReturnValue
    19    if($result) {
    20      Write-Error "Failed to disable the 'wuauserv' service. The return value was $result."
    21      exit 1
    22    }
    23  }
    24  
    25  if ($service.State -eq "Running") {
    26    $result = $service.StopService().ReturnValue
    27    if ($result) {
    28      Write-Error "Failed to stop the 'wuauserv' service. The return value was $result."
    29      exit 1
    30    }
    31  }
    32  
    33  Write-Output "Automatic Windows Updates disabled."