格式化我的输出结果来自PowerShell中的哈希表

时间:2018-04-14 12:02:53

标签: powershell output hashtable export-to-csv

所以这是我的问题,我确定你在用PowerShell和hashtables格式化输出时已经看过这一千次了。所以我编写了一个脚本,它将收集远程服务器上的所有必需品,如HD,MEM,CPU等。首先,我想说脚本工作正常,据说,我有输出到csv格式的问题。我正在寻找的是一些建议如何帮助我输出将每个HD捕获到有自己的列或分隔符,用于具有多个驱动器以及其他所有内容的系统。

所以这是我的脚本功能齐全:

         $OutFile = "$psscriptroot\sys_info.csv"
         $Status = @()
          $HDCAP = @()
        $ArrComputers =  "."  
        #Specify the list of PC names in the line above. "." means local system

        #Clear-Host
          foreach ($Computer in $ArrComputers) 
        {
            $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer 
            $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer  
            $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer 
            $computerCPU = get-wmiobject Win32_Processor -Computer $Computer 
            $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3 


              $exp =       @{Expression={$_.Server };Label="Computer_Name"},
                           @{Expression={$_.Manufacturer };Label="Manufacturer/Model"},
                           @{Expression={$_.Serial };Label="Serial #"},                   
                           @{Expression={$_.Processor };Label="Processor"},
                           @{Expression={$_.Harddrive};Label="Harddrive"},
                           @{Expression={$_.TotalMemorySize};Label="TotalMemorySize"}, 
                           @{Expression={$_.LastRebootTime};Label="LastRebootTime"} 

        foreach($sys in $computerSystem)
          {
          $mf = $sys | select Manufacturer,Model
          }
         foreach($cs in $computerSystem)
          {

          $mem = $cs | select -Property @{Name = "MemorySize GB" ;   Expression = { $_.TotalPhysicalMemory/1GB –as [int] } }
          }

         foreach($CB in  $computerBIOS)
        {
          $ser = $cb | select SerialNumber
        }
         foreach($CPU in  $computerCPU)
        {
          $PRCR = $CPU | select name
        }
        foreach($HD in $computerHDD)
        {
          $HDCAP += $HD| select -Property DeviceID,
                        @{Name = "SizeGB" ;   Expression = { $_.Size/1GB –as [int] } },
                        @{Name=  "FreeGB";    Expression={[math]::Round($_.Freespace/1GB,2)}}
        }
        foreach($OS in  $computerOS)
        {
          $CPOS = $os | select -Property  @{Name = "LastBootUpTime " ; Expression = {$OS.ConvertToDateTime($OS.LastBootUpTime)}}

        }
        $Status += (@{

         Server          =  $Computer
         Manufacturer    =  $Mf
         Serial          =  $ser      
         Processor       =  $PRCR
         Harddrive       =  $HDCAP 
         TotalMemorySize =  $mem     
         LastRebootTime  =  $CPOS     


                 })

        }




         $Status | select-object $exp 

以下是我收到的输出副本:

Computer_Name      : .
Manufacturer/Model : @{Manufacturer=Micro-Star International Co., Ltd; Model=MS-0000}
Serial #           : @{SerialNumber=To be filled by O.E.M.}
Processor          : @{name=AMD Ryzen 5 1600 Six-Core Processor            }
Harddrive          : {@{DeviceID=C:; SizeGB=447; FreeGB=158.99}, @{DeviceID=D:; SizeGB=931; FreeGB=473.66}, @{DeviceID=F:; SizeGB=0; FreeGB=0.11}}
TotalMemorySize    : @{MemorySize GB=32}
LastRebootTime     : @{LastBootUpTime =4/10/2018 9:54:16 AM}

期望的外观如下:

Computer_Name,Manufacturer/Model,Serial # ,Processor,Harddrive1, Harddrive 2, Harddrive 3 ,TotalMemorySize ,LastRebootTime

加上没有所有@符号。

请让我知道你的想法

2 个答案:

答案 0 :(得分:0)

尝试Flatten-Object,它不会提供确切的必需标题名称但非常接近,您可以随时考虑重命名它们:

PS C:\> $Status | Flatten | ft

Manufacturer.Manufacturer Manufacturer.Model       Server LastRebootTime.LastBootUpTime  Harddrive.1.DeviceID Harddrive.1.SizeGB Harddrive.1.FreeGB Serial.SerialNumber Processor.name
------------------------- ------------------       ------ ------------------------------ -------------------- ------------------ ------------------ ------------------- --------------
Hewlett-Packard           HP EliteBook Folio 9470m .      2018-04-11 6:11:28 PM          C:                                  221              59.42 PC1234567          Intel(R) Core...

答案 1 :(得分:0)

感谢大家对我提出的问题的支持。 有了这个,这是我决定使用的格式,我喜欢我收到的输出: 以下是使用PScustomObjects的片段:

    {   $DiskResults += [pscustomobject][ordered] @{
            Computer = $Computer
            Manufacturer =$mf.Manufacturer
            Model = $mf.Model
            Serial =$ser.SerialNumber
            Memory ="{0:N2}" -f ($mem.TotalPhysicalMemory / 1gb)
            Processor =$PRCR.name
            LastRebootInDays = (($now - 
           ($CPOS.ConverttoDateTime($CPOS.lastbootuptime))).days  )
            Drive = $Disk.DeviceID
            CapacityGB = "{0:N2}" -f ($Disk.Size / 1gb)
            UsedGB = "{0:N2}" -f (($Disk.Size - $Disk.FreeSpace) / 1gb)
            UsedPercent = "{0:P0}" -f (($Disk.Size - $Disk.FreeSpace) / 
            $Disk.Size)
            FreeGB = "{0:N2}" -f ($Disk.FreeSpace / 1gb)
            FreePercent = "{0:P0}" -f ($Disk.FreeSpace / $Disk.Size)
            VolumeName = $Disk.VolumeName
        }

这是我的结果图片:

enter image description here