Get-WindowsUpdatePolicy.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<#
.Synopsis
   Get the Windows Update policy on local or remote computers via the registry.
.DESCRIPTION
   Get the Windows Update policy on local or remote computers via the registry.
   A Windows system can be configured to communicate with a managed update
   environment such as WSUS, SCCM, or Intune. Get-WindowsUpdatePolicy will
   query the registry keys that store the current Windows Update policy for a
   system. The function will also display if no policy is configured.
.NOTES
    Created by: Jason Wasser @wasserja
    Modified: 2/7/2017 01:40:26 PM
.PARAMETER ComputerName
    Enter one or more computer names.
.PARAMETER Key
    The Windows Update policy registry key path is already specified.
.PARAMETER Credential
    Enter alternate credentials for accessing remote computers.
.EXAMPLE
   Get-WindowsUpdatePolicy
 
   Shows the current Windows Update policy of the local computer if it is configured.
.EXAMPLE
   Get-WindowsUpdatePolicy -ComputerName SERVER01,CLIENT02
 
   Shows the current Windows Update policy of server01 and client02 if it is configured.
.LINK
    https://gallery.technet.microsoft.com/scriptcenter/Get-WindowsUpdatePolicy-317c83d3
#>

function Get-WindowsUpdatePolicy
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([Microsoft.Win32.RegistryKey])]
    Param
    (
        [Parameter(Mandatory=$false,
                   ValueFromPipeline=$true,
                   Position=0)]
        [string[]]$ComputerName=$env:COMPUTERNAME,

        # Windows Update policy registry key path
        [string]$Key='HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate',
        $Credential = [System.Management.Automation.PSCredential]::Empty
    )

    Begin
    {
        # Helper function to get the registry keys and values
        function Get-RegistryKey ($Key, $Computer) {
            Write-Verbose "$Computer"
            if (Test-Path $Key) {
                # Get the WindowsUpdate policy information
                Get-ItemProperty $Key
                
                # Get the WindowsUpdate AU sub key values
                if (Test-Path $Key\AU) {
                    Get-ItemProperty $Key\AU
                    }
                
                }
            else {
                Write-Host "No Windows Update policy set for $Computer."
                }
            }

    }
    Process
    {
        foreach ($Computer in $ComputerName) {
            if ($Computer -eq $env:COMPUTERNAME) {
                Write-Verbose "Getting Windows Update policy registry settings from $Computer."
                Get-RegistryKey -Key $Key
                }
            else {
                Write-Verbose "Getting remote Windows Update policy registry settings from $Computer."
                Invoke-Command -ScriptBlock ${function:Get-RegistryKey} -ComputerName $Computer -ArgumentList $Key,$Computer -Credential $Credential
                }
            }
    }
    End
    {
    }
}