1

I'm trying to get a list of installed programs off a group of remote servers. I'm able to get the program name but not return the system name. Below is my script.

$computerfile = get-content "D:\Users\Admin\Docs\PrimaryServers.txt"
ForEach ($computer in $computerfile) {
Get-WmiObject Win32_Product -ComputerName $computer |
  Select-Object SystemName,Name,Version,PackageName,Installdate,Vendor |
  Format-Table -AutoSize
  }

Below is my output

enter image description here

Slyons
  • 117
  • 4
  • 20
  • Did you write this yourself based on the PowerShell documentation, or did you copy it from somewhere else? Because step one would be to check the API docs to make sure you're doing something that can be done, and those tell you that there is no such thing as `Systemname` is the Win32_Product class: https://msdn.microsoft.com/en-us/library/aa394378(v=vs.85).aspx – Mike 'Pomax' Kamermans Sep 27 '17 at 17:23

2 Answers2

0

First, -ComputerName can take an array of names so by looping you are going to increase the time because the loop will be in serial where utilizing the array for computername will be in parallel.

Second, it's best practice to use the CIM cmdlets in place of the WMI cmdlets. They operate over WSMAN by default and are easier to work with.

Third, Win32_Product forces a consistency check so reading the Uninstall registry keys is usually superior.

Lastly, SystemName isn't a property name that is returned by Get-WMIObject. PSComputerName is the property you are looking for and you can make a Calculated Property from it.

$computerfile = get-content "D:\Users\Admin\Docs\PrimaryServers.txt"
Get-CimInstance Win32_Product -ComputerName $Computerfile |
    Select-Object @{n=SystemName;e={$_.PSComputerName}},Name,Version,PackageName,Installdate,Vendor |
    Format-Table -AutoSize
BenH
  • 9,766
  • 1
  • 22
  • 35
0

Another approach is to use the Invoke-Command where it automatically extends the result with the PSComputerName column

First build up the script blocks to use as query

Block 1 is my approach for both x32 and x64 implementations. Consider this as an alternative because I has some issues with the proposed implementation.

$block1={
    # Query x64 bit applications
    $queryPath="HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
    Get-ItemProperty $queryPath | Select-Object -Property DisplayName, Publisher, InstallDate, DisplayVersion

    # Query x32 bit applications
    $queryPath="HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
    Get-ItemProperty $queryPath | Select-Object -Property DisplayName, Publisher, InstallDate, DisplayVersion
}

Block 2 is the proposed approach on this question

$block2={
    Get-CimInstance Win32_Product | Select-Object Name,Version,PackageName,Installdate,Vendor
}

For either $block1 or $block2 execute remotely on a server list

Invoke-Command -ComputerName $computernames -ScriptBlock $block1

And one record looks like this

DisplayName    : Microsoft Visual C++ 2013 x86 Additional Runtime - 12.0.21005
Publisher      : Microsoft Corporation
InstallDate    : 20161202
DisplayVersion : 12.0.21005
PSComputerName : MECDEVAPP01
RunspaceId     : 4b8cc747-da25-4c6e-b108-0ca3138c0335
Alex Sarafian
  • 634
  • 6
  • 17