Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

powershell - Get WMI Data From Multiple Computers and Export to CSV

So having some good old fashion Powershell frustrations today. What I need to do is this:

  1. Get a list of computers from a file
  2. Query those computers for "CSName" and "InstallDate" from Win32_OperatingSystem
  3. Convert InstallDate into a useable date format.
  4. Export all that to a .Csv

I've tried so many different iterations of my script. I run into 2 major issues. One is that I can't export and append to .Csv even with Export-Csv -Append. It just takes the first value and does nothing with the rest. The 2nd is that I can't get the datetime converter to work when piping |.

Here's a few samples of what I've tried - none of which work.

This sample simply errors a lot. Doesn't seem to carry $_ over from the WMI query in the pipe. It looks like it is trying to use data from the first pipe, but I'm not sure.

    Get-Content -Path .Computernames.txt | Foreach-Object {
        gwmi Win32_OperatingSystem -ComputerName $_) |
            Select-Object $_.CSName, $_.ConvertToDateTime($OS.InstallDate).ToShortDateString()
    } | Export-Csv -Path Filename -Force -Append -NoTypeInformation
}

This one simply exports the first value and gives up on the rest when exporting .Csv

$Computers = Get-Content -Path .Computernames.txt
foreach ($Computer in $Computers) {
  echo $Computer
  $OS = gwmi Win32_OperatingSystem -ComputerName $Computer
  $OS | Select-Object
  $OS.CSName,$OS.ConvertToDateTime($OS.InstallDate).ToShortDateString() |
      Export-Csv -Path $Log.FullName -Append
}

This one does get the data, but when I try to select anything, I get null values, but I can echo just fine.

$OS = gwmi Win32_OperatingSystem -ComputerName $Computers
$OS | Foreach-Object {
    Select-Object $_.CSName,$_.ConvertToDateTime($OS.InstallDate).ToShortDateString() |
        Export-Csv -Path $Log.FullName -Force -Append -NoTypeInformation
}

This feels like it should be ridiculously simple. I can do this in C# with almost no effort, but I just can't get PS to do what I want. Any help would be much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here you go,

$Array = @() ## Create Array to hold the Data
$Computers = Get-Content -Path .Computernames.txt

foreach ($Computer in $Computers)
{
    $Result = "" | Select CSName,InstallDate ## Create Object to hold the data
    $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer

    $Result.CSName = $OS.CSName ## Add CSName to line1
    $Result.InstallDate = $OS.ConvertToDateTime($OS.InstallDate).ToShortDateString() ## Add InstallDate to line2
    $Array += $Result ## Add the data to the array
}

$Array = Export-Csv c:file.csv -NoTypeInformation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...