2016-12-08 11 views
0

このクイックスクリプトを使用して、多数のサーバーに関する情報を取得しました。私がWindows 7(ps v2)のホスト上で走ったとき、私はすべて正しい結果を得ました。しかし、Server 2008 r2(ps v2)を実行すると、以下のすべてのクエリに対してSystem.Object []が得られます。他にもたくさんの質問がありますが、これらの問題はすべて解決しています。どうしたの?PowerShellでSystem.Object []出力を取得する

$ArrComputers = "localhost" 
$OutputLog = ".\output.csv" 
$NotRespondingLog = ".\notresponding.txt" 
$ErrorActionPreference = "Stop" 
Clear-Host 

$data = ForEach ($Computer in $ArrComputers) { 
try{ 

    $ipAdd   = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| select ipaddress 
    $MacAdd   = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| Select MacAddress 
    $DefGateway  = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| Select DefaultIPGateway 
    $DNSServ  = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName .)| Select DNSServerSearchOrder 
    $CPUname  = (Get-WmiObject –class Win32_processor -ComputerName .)| Select name 
    $processorinfo = (Get-WmiObject –class Win32_processor -ComputerName .)| Select NumberOfCores 
    $processorinfo2 = (Get-WmiObject –class Win32_processor -ComputerName .)| Select NumberOfLogicalProcessors 

$memory = Get-WMIObject -class Win32_PhysicalMemory -ComputerName $Computer | 
Measure-Object -Property capacity -Sum | 
select @{N="r"; E={[math]::round(($_.Sum/1GB),2)}} 


}catch{ 
    $Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8 
    continue  
} 

$props = @{ 


'IPAddress'  = $ipAdd 
'MacAddress'  = $MacAdd 
'DefaultIPGateway'= $DefGateway 
'DNSServerSearchOrder' = $DNSServ 
'cpuName'  = $CPUname 
'Cores'   = $processorinfo 
'logicalcores' = $processorinfo2 
' Memory'   = $memory 


} 
New-object -type PSCustomObject -Property $Props 
} 

$Data | export-csv -notypeinformation $outputlog 
+0

私は$小道具に以下の文字列を追加し、それはたIPAddress '=働い|コンピュータが1つの以上のインターフェースを持っている場合、あなたは空得るためである「」-join($ ipadd -ExpandProperty IPアドレスを選択します)文字列。私はここで答えを見つけたhttps://social.technet.microsoft.com/Forums/sharepoint/en-US/55734274-634d-465e-a655-03f7f404f349/systemstring-in-output-of-converttohtml-when-im-expecting -a-regular-string-data?forum = winserverpowershell –

答えて

0

あなたが直面している問題:Powershellは、$データをKey = Valueまたはハッシュテーブル形式としてオブジェクトとして返します。したがって、CSVと同じものを挿入すると、Objectとして返されます。つまり、データをJSON形式に変換して、それを挿入することができます。そうでなければArraylistを使用してそこにすべての値を挿入することができます。その場合、キーと値のペアのマッピングを受け入れます。

はそれが私が選択クエリからヘッダを削除し、ループ内で作成したカスタムオブジェクトと配列リストを作成し、それをサーバーからサーバーへの各詳細を追加し、配列リストに追加されますしている

+0

しかし、これは$ OS =(Get-WmiObject Win32_OperatingSystem).captionのような他のクエリでも動作します。 –

+0

スクリプトを修正し、それを投稿します。実際に多くのことが実際にデータについて気にならない場合 –

+0

ありがとうございましたRanadip、本当にありがとうございます。 –

0

を役に立てば幸い別々に。これがあなたに役立つことを願っています

$ArrComputers = "localhost" 
$OutputLog = ".\output.csv" 
$NotRespondingLog = ".\notresponding.txt" 
$ErrorActionPreference = "Stop" 
Clear-Host 
$Global:arraylist= New-Object System.Collections.ArrayList 
$data = ForEach ($Computer in $ArrComputers) { 
try{ 

    $ipAdd   = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| select ipaddress 
    $MacAdd   = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| Select MacAddress 
    $DefGateway  = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| Select DefaultIPGateway 
    $DNSServ  = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName $Computer)| Select DNSServerSearchOrder 
    $CPUname  = (Get-WmiObject –class Win32_processor -ComputerName $Computer)| Select name 
    $processorinfo = (Get-WmiObject –class Win32_processor -ComputerName $Computer)| Select NumberOfCores 
    $processorinfo2 = (Get-WmiObject –class Win32_processor -ComputerName $Computer)| Select NumberOfLogicalProcessors 

$memory = Get-WMIObject -class Win32_PhysicalMemory -ComputerName $Computer | 
Measure-Object -Property capacity -Sum | 
select @{N="r"; E={[math]::round(($_.Sum/1GB),2)}} 
$props =[PSCustomObject]@{ 


'IPAddress'  = $ipAdd.ipaddress[0] 
'MacAddress'  = $MacAdd.MacAddress 
'DefaultIPGateway'= $DefGateway.DefaultIPGateway[0] 
'DNSServerSearchOrder' = $DNSServ.DNSServerSearchOrder[0] 
'cpuName'  = $CPUname.name 
'Cores'   = $processorinfo.NumberOfCores 
'logicalcores' = $processorinfo2.NumberOfLogicalProcessors 
' Memory'   = $memory.r 


} 
$arraylist.Add($props) 
}catch{ 
    $Computer | Out-File -FilePath $NotRespondingLog -Append -Encoding UTF8 
    continue  
} 


} 

$arraylist | Export-Csv -NoTypeInformation $OutputLog -Force 
関連する問題