Reputation: 2061
i wanted to compare the version of a software installed on multiple servers, the list of servers are part of a DAG cluster and as you can see below.
PS C:\Windows\system32\drivers> Get-DatabaseAvailabilityGroup | select -ExpandProperty servers | select name
Name
----
VMAPPSNODE3
VMAPPSNODE2
VMAPPSNODE1
Now the number of servers which are a part of DAG can be between 1--16, i wanted a way to compare the software version of a particular software in thsi servers.
I could do some thing like below but as you can see the No of DAG servers is not fixed and its a dynamic variable,
$VMAPPSNODE3 = get-wmiobject Win32_product |?{.....
$VMAPPSNODE2 = get-wmiobject Win32_product |?{.....
.
.
.
.
.
i tried compare-object but it only supports comparison of two objects, how could i attain comparison of the same software version of a product across multiple servers and let me know a "True" or "False" statement indicating if the software version is same across all servers or not.
Upvotes: 1
Views: 447
Reputation: 54881
You could use a foreach-loop. Try this:
Get-DatabaseAvailabilityGroup | % {
$name = $_.Name
Get-WmiObject win32_product -Filter "name like 'Powershell%'" -ComputerName $name | % {
New-Object psobject -Property {
ComputerName = $name
Version = $_.Version
}
}
}
Sample-output:
ComputerName Version
------------ -------
VMAPPSNODE3 3.0.4.0
VMAPPSNODE2 3.0.4.0
....
I've used and recommend -Filter
in Get-WmiObject
instead of where-object (?)
. Where-Object
queries all products and then filter using where
, while the -Filter
parameter does it during the search, which is alot faster. Both ways work, it's just a timesaver (but requires a slightly different filter-style then where-object
)
You can expand this however you like, ex. True/false: There's only one version in the DAG
@(Get-DatabaseAvailabilityGroup | % {
$name = $_.Name
Get-WmiObject win32_product -Filter "name like 'Powershell%'" -ComputerName $name | % {
New-Object psobject -Property {
ComputerName = $name
Version = $_.Version
}
}
} | Select-Object Version -Unique).Count -eq 1
True
Upvotes: 2