Get-WUSettings.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Function Get-WUSettings
{
    <#
 .SYNOPSIS
     Get Windows Update settings.
 
 .DESCRIPTION
     Use Get-WUSettings to get Windows Update settings.
 
 .PARAMETER WUAAPI
        Use Windows Update Agent API. Works only on local machine.
 
 .PARAMETER Registry
        Use Windows registry. Works only for GPO settings.
 
 .PARAMETER ComputerName
     Specify the name of the computer to the remote connection.
 
 .PARAMETER Debuger
     Debug mode.
 
 .EXAMPLE
        PS C:\> Get-WUSettings -Registry
   
            AcceptTrustedPublisherCerts : 1
            WUServer : https://wsus.contoso.com
            WUStatusServer : https://wsus.contoso.com
            DetectionFrequencyEnabled : 1
            DetectionFrequency : 2
            NoAutoRebootWithLoggedOnUsers : 1
            RebootRelaunchTimeoutEnabled : 1
            RebootRelaunchTimeout : 240
            IncludeRecommendedUpdates : 0
            NoAutoUpdate : 0
            AUOptions : 2 - Notify before download
            ScheduledInstallDay : 0 - Every Day
            ScheduledInstallTime : 4
            UseWUServer : 1
            ComputerName : G1
   
 .NOTES
  Author: Michal Gajda
  Blog : http://commandlinegeeks.com/
   
 .LINK
  http://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc
 #>
    

[CmdletBinding(
     SupportsShouldProcess=$True,
        ConfirmImpact="Low",
        DefaultParameterSetName="Registry"
    )]
    Param
    (
#Mode options
[Switch]$Debuger,
        [Parameter(ParameterSetName='WUAAPI')]
        [Switch]$WUAAPI,
        [Parameter(ParameterSetName='Registry')]
        [Switch]$Registry = $True,
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
            ParameterSetName='Registry')]
[String[]]$ComputerName    
    )

Begin
{
If($PSBoundParameters['Debuger'])
{
$DebugPreference = "Continue"
} #End If $PSBoundParameters['Debuger']

$User = [Security.Principal.WindowsIdentity]::GetCurrent()
$Role = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)

if(!$Role)
{
Write-Warning "To perform some operations you must run an elevated Windows PowerShell console."
} #End If !$Role
}

Process
{
        Write-Debug "Check if ComputerName in set"
If($ComputerName -eq $null)
{
Write-Debug "Set ComputerName to localhost"
[String[]]$ComputerName = $env:COMPUTERNAME
} #End If $ComputerName -eq $null

        $NotificationLevels = @{ 0="0 - Not configured"; 1="1 - Disabled"; 2="2 - Notify before download"; 3="3 - Notify before installation"; 4="4 - Scheduled installation"; 5="5 - Users configure" }
        $ScheduledInstallationDays = @{ 0="0 - Every Day"; 1="1 - Every Sunday"; 2="2 - Every Monday"; 3="3 - Every Tuesday"; 4="4 - Every Wednesday"; 5="5 - Every Thursday"; 6="6 - Every Friday"; 7="7 - EverySaturday" }

        $Results = @()
        Foreach($Computer in $ComputerName)
{        
    If(Test-Connection -ComputerName $Computer -Quiet)
{
                Write-Debug "Connect to reg HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate for $Computer"
$RegistryKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$Computer) 
                $RegistrySubKey1 = $RegistryKey.OpenSubKey("SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\") 
                $RegistrySubKey2 = $RegistryKey.OpenSubKey("SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\")                
                
                if($RegistrySubKey1) { Write-Verbose "Some settings are managed by your system administrator." }
                                
                if($WUAAPI)
                {
                    $AutoUpdateSettings = (New-Object -ComObject Microsoft.Update.AutoUpdate).Settings

                    $Result = New-Object -TypeName PSObject -Property @{
                        NotificationLevel = $NotificationLevels[$AutoUpdateSettings.NotificationLevel]
                        ScheduledInstallationDay = $ScheduledInstallationDays[$AutoUpdateSettings.ScheduledInstallationDay]
                        ScheduledInstallationTime = $AutoUpdateSettings.ScheduledInstallationTime
                        IncludeRecommendedUpdates = $AutoUpdateSettings.IncludeRecommendedUpdates
                        NonAdministratorsElevated = $AutoUpdateSettings.NonAdministratorsElevated
                        FeaturedUpdatesEnabled = $AutoUpdateSettings.FeaturedUpdatesEnabled
                    }
                } elseif($Registry)
                {
    $Result = New-Object -TypeName PSObject
                    Try
                    {
                        Foreach($RegName in $RegistrySubKey1.GetValueNames()) 
                        { 
                            $Value = $RegistrySubKey1.GetValue($RegName) 
                            $Result | Add-Member -MemberType NoteProperty -Name $RegName -Value $Value
                        }
                        Foreach($RegName in $RegistrySubKey2.GetValueNames()) 
                        { 
                            $Value = $RegistrySubKey2.GetValue($RegName) 
                            Switch($RegName)
                            {
                                'AUOptions' { $Value = $NotificationLevels[$Value] }
                                'ScheduledInstallDay' { $Value = $ScheduledInstallationDays[$Value] }
                            }
                            $Result | Add-Member -MemberType NoteProperty -Name $RegName -Value $Value
                        }
        }
                    Catch
                    {
                        Write-Error "Can't find registry subkey: HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate. Probably you don't use Group Policy for Windows Update settings. Try use -WUAAPI on local machine." -ErrorAction Stop
                    }

                    $Result | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $Computer
                } #End elseif $Registry
                $Results += $Result
            } # End If Test-Connection -ComputerName $Computer -Quiet
        } # End Foreach $Computer in $ComputerName
 
        Return $Results

} #End Process

End{}
} #In The End :)