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

How to do it...

You use the cmdlets within the PackageManagement module to explore the capabilities it provides.

  1. Review the cmdlets in the PackageManagement module:
      Get-Command -Module PackageManagement
  1. Review the installed providers with Get-PackageProvider:
      Get-PackageProvider | Select-Object -Property Name, Version
  1. The provider list includes msi, msu, and Programs package providers. These providers expose applications and updates installed on your computer which you can explore:
      Get-Package -ProviderName msi |
Select-Object -ExpandProperty Name
Get-Package -ProviderName msu |
Select-Object -ExpandProperty Name
Get-Package -ProviderName Programs |
Select-Object -ExpandProperty Name
  1. The NuGet source contains developer library packages. This functionality is outside the scope of this book, but worth exploring if you do Windows or web development:
      Get-PackageProvider -Name NuGet
  1. There are also other package providers you can explore:
      Find-PackageProvider |
Select-Object -Property Name,Summary |
Format-Table -Wrap -AutoSize
  1. Notice Chocolatey, which is a very useful tool for Windows administrators and power users. Those with some Linux background may think of Chocolatey as apt-get for Windows. You cannot use this provider until you install it and confirm the installation:
      Install-PackageProvider -Name Chocolatey -Verbose
  1. Verify Chocolatey is now in the list of installed providers:
      Get-PackageProvider | Select-Object Name,Version
  1. Look for available software packages from the Chocolatey package provider. Store these in a variable so you don't request the collection more than once, and explore it:
      $AvailableChocolateyPackages = `
Find-Package -ProviderName Chocolatey
# How many software packages are available at Chocolatey?
$AvailableChocolateyPackages | Measure-Object
  1. Pipe to Out-GridView to search for interesting software packages from Chocolatey:
      $AvailableChocolateyPackages |
Sort-Object Name,Version |
Select-Object Name, Version, Summary |
Out-GridView
  1. Install one or more packages. sysinternals is a good example to use. Use -Verbose to get details on the installation:
      Install-Package -ProviderName Chocolatey `
-Name sysinternals `
-Verbose
  1. Review installed Chocolatey packages, stored to C:\chocolatey\ by default, this path is stored in the $env:ChocolateyPath environment variable. Then review the executable files included with the sysinternals package:
      Get-ChildItem -Path $env:ChocolateyPath\lib |
Select-Object -Property Name
Get-ChildItem -Path `
$env:ChocolateyPath\lib\sysinternals.2016.11.18\tools `
-Filter *.exe |
Select-Object -Property Name
  1. Run any installed command included with sysinternals:
      $PSInfoCommand = `
‘C:\Chocolatey\lib\sysinternals.2016.11.18\tools\PsInfo.exe’
Invoke-Expression -Command $PSInfoCommand
  1. Installed packages are enumerated with Get-Package and updated using the same command to install them, Install-Package:
      Get-Package -ProviderName Chocolatey | 
Install-Package -Verbose