Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...