2016-06-21 7 views
1

だから私は、下のスクリプトブロックを持っている:輸出CSV - (PowerShellの)

Import-csv C:\file_location.csv | foreach-object { 
Get-WmiObject -computername $_.computername -class Win32_ComputerSystem | select username} | ` 
Select-Object computername, username | Export-CSV C:\file_location.csv -notypeinformation 

エクスポートされたCSVは、コンピュータ名のヘッダを示すが、実際のコンピュータとユーザー名ヘッダがうまくありません。何がどこから欠けていますか?

ありがとうございました!

+0

[Powershell Export-CSV issues]の重複している可能性があります(https://stackoverflow.com/questions/42233416/powershell-export-csv-issues) –

答えて

0

selectは(Select-Objectの別名である)は、指定のみプロパティを持つオブジェクトを返します。だからあなたが最初に行ったときにはselect usernameという名前のオブジェクトしか持っていません。他のすべてのプロパティは破棄されたので、2番目のSelect-Objectコールが実行されると、返される番号はcomputernameになりません。

最初のselectは完全に不要です。ただそれを取り出して、私はすべてが期待どおりに動作すると思う。

編集:今すぐ参照してくださいcomputernameは、返されたWMIオブジェクトのプロパティではありません。それはCSVから来ました。 ForEach-ObjectはWMIオブジェクトのみを返すので、CSV行オブジェクトは破棄されます。あなたがする必要がどのような

あなたはSelect-Object(計算列を持つ)またはAdd-Memberで行うことができますWMIオブジェクトにCSVからcomputernameを追加です:

Import-csv C:\file_location.csv | 
    ForEach-Object { 
     Get-WmiObject -computername $_.computername -class Win32_ComputerSystem | 
     Select-Object @{Name='ComputerName';Expression={$_.computername}},username 
    } | 
    Export-CSV C:\file_location.csv -notypeinformation 

または:

Import-csv C:\file_location.csv | 
    ForEach-Object { 
     Get-WmiObject -computername $_.computername -class Win32_ComputerSystem | 
     Add-Member -NotePropertyName computername -NotePropertyValue $_.computername -Force -PassThru 
    } | 
    Select-Object computername, username 
    Export-CSV C:\file_location.csv -notypeinformation 
+0

ありがとうございます!私は両方の方法を試みましたが、それでも同じです。 selectオブジェクトメソッドは、同じ結果をComputerNameヘッダーに渡し、列にデータはありません。 Add-Memberは、空のCSVをエクスポートします。 –

+0

スクラッチそれ!私は間違いを見つけました。追加メンバーは完全に働いた。 @briantistありがとうございます! –