I’m trying to export the result of a WMI query to CSV. It results in Sytem.Object or blank when I’ve tried to apply different solutions from the web.

$ComputerName = Get-Content c:\Scripts\eCW\Computers.txt
$PCData = foreach ($PC in $ComputerName) {
If (Test-Connection -ComputerName $PC -Count 2 -Quiet) {
Write-Verbose “Checking software on $PC” -Verbose
$App = Get-WmiObject -Class Win32_Product | where { $_.Name -match “Citrix”}
$Props = @{
ComputerName = $PC
Status = “Online”
CitrixInstalled = $App
}
New-Object -TypeName PSObject -Property $Props
}
Else {
Write-Verbose “$PC Offline” -Verbose
$Props = @{
ComputerName = $PC
Status = “No Response”
CitrixInstalled = ‘’
}
New-Object -TypeName PSObject -Property $Props
}
}
Write-Verbose “Creating Report” -Verbose
Write-Output $PCData | select ComputerName, Status, CitrixInstalled | Export-CSV c:\Scripts\eCW\ComputerRpt1.csv -NoTypeInformation
Write-Verbose “Report Complete” -Verbose

The insert Code wasn’t working

2 Spice ups

What is the code you use to export?

If you post code, please use the ‘Insert Code’ button. Please and thank you!

get-wmiobject <#query#> |
export-csv "c:\file.csv" -notypeinformation

Post code button wasn’t working for me. I edited the post and added the code.

why wmi , do not use wmi to query programs installed, its slow and re-configures all apps.

better use
Edited: I missed csv in export csv

https://gallery.technet.microsoft.com/scriptcenter/Get-RemoteProgram-Get-list-de9fd2b4

gc C:\computers.txt |Foreach{Get-RemoteProgram -ComputerName $_ |where{$_.ProgramName -like '*Citrix*' |export-csv c:\citrix.csv -notype}}
1 Spice up

I do not know of what you speak. Reconfigure?!?! What am I doing to my computers? I’ll have to look into that script on Tuesday when I get back into the office. Looks promising.

Querying win32_product can cause problems on systems. More here (2nd para): Why Win32_Product is Bad News! - SDM Software

That script Jiten linked to is probably a better option.

2 Spice ups

The Win32_product class us well known for being problematic. Sadly, Msft has not offered a reasonable alternative

So I’ve integrated the Get-RemoteProgram module into my script. (got a little schooling there in Powershell modules in the process…Thanks!) However, I still have the same issue. Something about using a variable for the command and trying to output that to the $Props object all in the $PCData object, somewhere in there it is only getting System.Object. That is still coming out on the CSV. Oh, and it now starts on the 22 row ; )

Here is the replacement for the line above (I tried with and without -excludesimlar with same result):

$App = Get-RemoteProgram -Computername $PC -ExcludeSimilar | Where{$_.ProgramName -Like ‘Citrix*’}

That issue sounds like you are trying to write the entire objet, not individual properties.

What does:

Write-Output $PCData | select ComputerName, Status, CitrixInstalled

Show?

In general - you need to look ‘back’ - what is the data you are sending to the CSV. I suspect you need to remove some properties to get them into the CSV. This is where simple PowerShell Debugging comes into play.

1 Spice up

Good call tfl. I isolated that and Get-RemoteProgram outputs a table. I have tried to break it down to just the one program, but it still outputs the table:

I’m working on extracting the text from the string or figuring out where to edit the Get-RemoteProgram to only send the one item

PS> Get-RemoteProgram -ExcludeSimilar -SimilarWord 1 | Where{$_.ProgramName -Like ‘Citrix*’}

PS> Get-RemoteProgram -ExcludeSimilar -SimilarWord 1 | Where{$_.ProgramName -Like ‘Citrix*’}
ProgramName ComputerName


Citrix Receiver 4.9 MyHostname

That is correct. You are retrieving an object that has two properties.

Any suggestion on extracting just the one property?

(BTW I’m having a heck of a time with entering code into my post and the formatting in my posts. I tried to find a tutorial, but…)

so if you have a variable, $foo that contains an object, just do

$foo.property

To get a aproperty from an expression

(Get-ChildItem c:\ -file | select -last 1).fullname

I am assuming you have made changes to the original post - and I can’t work out what you are trying and, specifically, what output you get.

And PLEASE use the </> tool in the editor tool bar enter code and results - that way it’s easier to read (and you are more likely to get better answers if we can read the code easily)/

1 Spice up

I should have known that. I’ve used that in other scripts and didn’t make the connection. Thanks for the heads up. Been learning PowerShell over the last year by hacking together scripts I find on the internet.

Here is the final code I came up with for this. Thanks to everyone who contributed.

still couldn’t get the </> button to work for me (comes up blank when I enter code and submit the post)

<#
.Synopsis
Generates a list of computers and exports to CSV with version of program installed

.Notes
Author : Mike Wilson
Date Created : 2/21/18
#>

empty the Computername variable for running in same window

$ComputerName = $Null
#Download from https://gallery.technet.microsoft.com/scriptcenter/Get-RemoteProgram-Get-list-de9fd2b4#content rename to .psm1
Import-Module -Name “C:\Scripts\eCW\Get-RemoteProgram.psm1”
#Get list of computers
$ComputerName = Get-Content c:\Scripts\eCW\Computers.txt
#Set counter to 1
$Count = 1
$PCData = foreach ($PC in $ComputerName) {
Write-Progress -Id 0 -Activity “Checking Computers” -Status “$Count of $($ComputerName.Count)” -PercentComplete (($Count / $ComputerName.Count) * 100)
If (Test-Connection -ComputerName $PC -Count 2 -Quiet) {
Write-Verbose “Checking software on $PC” -Verbose
#Get-RemoteProgram requires remote registry
CMD /C SC.exe \$PC start RemoteRegistry | Out-Null
$App = Get-RemoteProgram -Computername $PC -ExcludeSimilar -Property DisplayVersion | Where{$_.ProgramName -Like ‘Citrix Receiver Inside*’}
$Props = @{
ComputerName = $PC
Status = “Online”
Citrix_Version = $App.DisplayVersion
eCW_Installed = $ECW
}
CMD /C SC.exe \$PC stop RemoteRegistry | Out-Null
New-Object -TypeName PSObject -Property $Props
}
Else {
Write-Verbose “$PC Offline” -Verbose
Write-Output “$PC offline” >> c:\Scripts\eCW\NewOut.txt
$Props = @{
ComputerName = $PC
Status = “No Response”
Citrix_Installed = ‘’
eCW_Installed = ‘’
}
New-Object -TypeName PSObject -Property $Props
}
$Count ++
}
Write-Progress -Id 0 -Activity " " -Status " " -Completed
Write-Verbose “Creating Report” -Verbose
Write-Output $PCData | select ComputerName, Status, Citrix_Installed, eCW_Installed | Sort Computername | Export-CSV c:\Scripts\eCW\ComputerRptfinal.csv -NoTypeInformation
Write-Verbose “Report Complete” -Verbose