Home > Net >  I need to export the results of this script to a .CSV but it will not work properly with Export-CSV
I need to export the results of this script to a .CSV but it will not work properly with Export-CSV

Time:06-08

'''$Computers = Get-ADComputer -Filter 'operatingsystem -like "server" -and enabled -eq "true"' -Properties Name| Sort-Object -Property Name | Select-Object -Property Name,Operatingsystem,OperatingSystemVersion,IPv4Address

foreach ($Computer in $Computers)

{

Write-Host $Computer.Name

Get-WmiObject -Class Win32_Product -ComputerName $Computer.Name | select Name, Version, Vendor, Caption | Sort-Object -Property Name,Vendor,Version,Caption

Write-Host " " }'''

CodePudding user response:

I think the root of your issue lies when you use Name as the only Property in Get-ADComputer parameters.

Further I am not sure the syntax in your Get-WmiObject is fully correct.

Try this: It will allow all the properties to return before you sort by name and subsequently select the properties you want to use.

$Computers = Get-ADComputer -Filter 'OperatingSystem like "server" -and Enabled -eq "True"' -Properties * | 
    Sort-Object -Property Name | 
    Select-Object -Property Name, OperatingSystem, OperatingSystemVersion, IPv4Address |
    ForEach-Object {
        Get-WmiObject -Class Win32_Product -ComputerName $_.Name |
        Sort-Object -Property Name,Vendor,Version,Caption | 
        Select-Object | -Property Name, Version, Vendor, Caption
    }

$Computers | Export-CSV -Path 'D:foo.csv -NoTypeInformation

CodePudding user response:

Tested on Windows 10, Server 2016 and Server 2019. The product class is correct and so are the fields. It appears to hang but the reality it's just slow. 2-3 minutes per server is typical.

The article from Microsoft that documents this WMI class can be found here:

enter image description here

  • Related