在powershell中获取System.Object []输出

时间:2016-12-08 14:44:32

标签: powershell

我编写了这个快速脚本来检索有关一堆服务器的信息。当我在我的Windows 7(ps v2)主机上运行时,我得到了所有正确的结果。但是,当我在Server 2008 r2(ps v2)上运行时,我得到以下所有查询的 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 

2 个答案:

答案 0 :(得分:0)

所以你面临的问题是:Powershell将$ data作为Key = Value或hashtable格式返回,而是作为一个对象。因此,当您插入与CSV相同的内容时,它会将其作为对象返回。所以你可以做的是你可以将数据转换为JSON格式,你可以插入相同的。否则,您可以使用Arraylist并在那里插入所有值。在这种情况下,它将接受键值对映射。

希望有所帮助

答案 1 :(得分:0)

我从select查询中删除了标题,并创建了一个数组列表,其中包含在循环中创建的自定义对象,它将从服务器添加每个细节到服务器,并将分别附加到数组列表中。我希望这可以帮助你。

$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