0

I have issues with this PowerShell script, if i execute the line without the foreach works great but with him don't show up nothing, can someone tell me why?

The foreach works fantastic by himselft he read all the data from the CSV but for some reason don't work, the CSV contain PC names like PC1, Pc-RRHH, JosePc from my network, like i say without the foreach is fine.

    $computers = Import-Csv “C:\PowerShell\Pc.csv”

$array = @()

foreach($pc in $computers)
    {
    
    Get-WmiObject -Namespace ROOT\CIMV2 -Class Win32_Product -Computer $pc.computername 
    
    Select-Object Name, Version, PSComputerName 
    
    Where-Object -FilterScript {$_.Name -like “Adobe*”}
    
    }

Best regards

RetiredGeek
  • 2,980
  • 1
  • 7
  • 21
Aismel
  • 3
  • 2
  • You're missing a few pipes and you're not adding to your array. With your current code you should see some output along with the syntax errors. Also, it's good practice to filter before using `select-object` – Abraham Zinala Mar 14 '22 at 12:43
  • It doesn't show anything if that was the case I would mention it. – Aismel Mar 14 '22 at 21:37
  • As an FYI, you should **never use `Win32_Product`**, instead [check the registry directly](https://stackoverflow.com/questions/71575378/powershell-for-software-inventory/71576041#71576041) for the Windows software inventory. – codewario Mar 24 '22 at 15:12

1 Answers1

1

Assuming your CSV file indeed has a header with column 'computername' in it, you can change your code to:

$computers = Import-Csv "C:\PowerShell\Pc.csv"

# let PowerShell collect the data for you in an array
$array = foreach($pc in $computers) {
    # because NameSpace 'root/CIMV2' is the default, you do not have to specify that
    Get-CimInstance -ClassName Win32_Product -ComputerName $pc.computername |
    # or use the old Get-WmiObject
    # Get-WmiObject -Class Win32_Product -Computer $pc.computername |
    Where-Object { $_.Name -like "Adobe*" } |
    Select-Object Name, Version, PSComputerName 
}

Instead of piping to a Where-Object {..} clause, you can also use the -Filter parameter.
Beware though that this filter requires WQL syntax, which is different from PowerShell syntax.

$computers = Import-Csv "C:\PowerShell\Pc.csv"

# let PowerShell collect the data for you in an array
$array = foreach($pc in $computers) {
    # because NameSpace 'root/CIMV2' is the default, you do not have to specify that
    Get-CimInstance -ClassName Win32_Product -ComputerName $pc.computername -Filter "Name like 'Adobe%'" |
    Select-Object Name, Version, PSComputerName 
}

Or use the -Query parameter:

$computers = Import-Csv "C:\PowerShell\Pc.csv"
# the query string to filter on. Also WQL syntax
$query = "Select * from Win32_Product where Name LIKE 'Adobe%'"

# let PowerShell collect the data for you in an array
$array = foreach($pc in $computers) {
    Get-CimInstance Get-CimInstance -Query $query -ComputerName $pc.computername |
    Select-Object Name, Version, PSComputerName 
}

From your comment, I gather that your CSV file isn't a CSV at all, but just a text file with pc names each on a separate line and that there is not header 'computername'.

In that case, change $computers = Import-Csv "C:\PowerShell\Pc.csv" to $computers = Get-Content -Path "C:\PowerShell\Pc.csv" and in the loop use -ComputerName $pc instead if -ComputerName $pc.computername

Theo
  • 57,719
  • 8
  • 24
  • 41
  • 1
    Definitely use the `-Filter` instead of piping to `Where-Object` - it reduces the amount of data brought across the network. "Filter left, format right" – Jeff Zeitlin Mar 14 '22 at 13:15
  • I'll try and let you asap... Thx any way for the reply.. – Aismel Mar 14 '22 at 18:43
  • Well, the issue seems to be realted to the array from CSV function.... I move the content to a TXT and works smoothly.... – Aismel Mar 15 '22 at 10:39
  • @Aismel That is why I started my answer with _Assuming your CSV file indeed has a header with column 'computername' in it_. I have edited my answer. – Theo Mar 15 '22 at 10:44
  • i know you did, and everything is OK with that, so i don't know what's the problem, i just move the content to a txt file and works great. Your anserw is correct! – Aismel Mar 17 '22 at 18:45