Get-WUAPIVersion.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
Function Get-WUAPIVersion
{
    <#
 .SYNOPSIS
     Show Windows Update Agent version.
 
 .DESCRIPTION
     Use Get-WUAPIVersion to get Windows Update Agent version.
 
 .PARAMETER ComputerName
     Specify the name of the computer to the remote connection.
 
 .PARAMETER Debuger
     Debug mode.
 
 .EXAMPLE
        PS C:\> Get-WUAPIVersion
   
        WuapiDll ApiVersion ComputerName
        -------- ---------- ------------
        10.0.14393.953 8.0 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"
    )]
    Param
    (
#Mode options
[Switch]$Debuger,
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[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

        $Results = @()
        Foreach($Computer in $ComputerName)
{        
If(Test-Connection -ComputerName $Computer -Quiet)
{
If($Computer -eq $env:COMPUTERNAME)
{
Write-Debug "Create Microsoft.Update.AgentInfo object"
$AgentInfo = New-Object -ComObject "Microsoft.Update.AgentInfo" #Support local instance only
} #End If $Computer -eq $env:COMPUTERNAME
Else
{
Write-Debug "Create Microsoft.Update.AgentInfo object for $Computer"
$AgentInfo =  [activator]::CreateInstance([type]::GetTypeFromProgID("Microsoft.Update.AgentInfo",$Computer))
} #End Else $Computer -eq $env:COMPUTERNAME

                $ApiMajorVersion = $AgentInfo.GetInfo("ApiMajorVersion")
                $ApiMinorVersion = $AgentInfo.GetInfo("ApiMinorVersion")
                $WuapiDllVersion = $AgentInfo.GetInfo("ProductVersionString")

                $Result = New-Object -TypeName PSObject -Property @{
                    ApiVersion = "$($ApiMajorVersion).$($ApiMinorVersion)"
                    WuapiDll = $WuapiDllVersion
                    ComputerName = $Computer
                }

                $Results += $Result
            } # End If Test-Connection -ComputerName $Computer -Quiet
        } # End Foreach $Computer in $ComputerName
        Return $Results

} #End Process

End{}
} #In The End :)