Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

How to do it...

The steps for the recipe are as follows:

  1. Install the Windows Update feature and tools, with -Verbose for additional feedback:
      Install-WindowsFeature -Name 'UpdateServices' `
-IncludeManagementTools -Verbose
  1. Review the features that are installed on your server, noting that not only has Windows Software Update Services been installed, but Web Server (IIS), ASP.Net 4.6, and Windows Internal Database have as well:
      Get-WindowsFeature |
Where-Object -FilterScript {($psitem.Installed)}
  1. Create a folder for WSUS update content:
      $WSUSContentDir = 'C:\WSUS'
New-Item -Path $WSUSContentDir -ItemType Directory
  1. Perform post-installation configuration using WsusUtil.exe:
      & "$env:ProgramFiles\Update Services\Tools\WsusUtil.exe" ` 
postinstall
CONTENT_DIR=$WSUSContentDir
  1. Once configuration completes, the output includes a line stating Log file is located at, followed by a path to a .tmp file in the user's temp directory. Review this log file to see what was done in the configuration (adjust the file name as necessary):
      Get-Content -Path "$env:TEMP\1tmp234.tmp"
  1. View some websites on this machine, noting the WSUS website:
      Get-Website
  1. View the cmdlets in the UpdateServices module:
      Get-Command -Module UpdateServices
  1. Inspect the TypeName and properties of the object created with Get-WsusServer:
       $WSUSServer = Get-WsusServer
$WSUSServer.GetType().Fullname
$WSUSServer | Select-Object -Property *
  1. The object is of type UpdateServer in the Microsoft.UpdateServices.Internal.BaseApi namespace, and is the main object you interact with to manage WSUS from PowerShell. Inspect the methods of the object:
      $WSUSServer | Get-Member -MemberType Method
  1. Inspect some of the configuration values of the UpdateServer object:
      $WSUSServer.GetConfiguration() |
Select-Object -Property SyncFromMicrosoftUpdate,LogFilePath
  1. Product categories are the various operating systems and programs for which updates are available. See what product categories are included by WSUS after the initial install:
      $WSUSProducts = Get-WsusProduct -UpdateServer $WSUSServer
$WSUSProducts.Count
$WSUSProducts
  1. Your $WSUSServer object contains a subscription object with properties and methods useful for managing the synchronization of updates. Access the Subscription object in the $WSUSServer object and inspect it, noting that it is also in the Microsoft.UpdateServices.Internal.BaseApi namespace:
      $WSUSSubscription = $WSUSServer.GetSubscription()
$WSUSSubscription.GetType().Fullname
$WSUSSubscription | Select-Object -Property *
$WSUSSubscription | Get-Member -MemberType Method
  1. Before you choose which product updates you want, you need to know what product categories are available. Get the latest categories of products available from Microsoft Update servers, and use a while loop to wait for completion:
      $WSUSSubscription.StartSynchronizationForCategoryOnly()

Do {
Write-Output $WSUSSubscription.GetSynchronizationProgress()
Start-Sleep -Seconds 5
}
While ($WSUSSubscription.GetSynchronizationStatus() -ne `
'NotProcessing')
  1. Once synchronization is complete, check the results of the synchronization:
      $WSUSSubscription.GetLastSynchronizationInfo()
  1. Again, review the categories of the products available:
      $WSUSProducts = Get-WsusProduct -UpdateServer $WSUSServer
$WSUSProducts.Count
$WSUSProducts