【问题标题】:Formatting multiple result sets together in powershell在PowerShell中一起格式化多个结果集
【发布时间】:2023-04-02 20:53:01
【问题描述】:
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt") | SELECT-Object PSComputerName, @{Name="Memory (RAM in GB)";Expression={[Math]::Round($_.TotalVisibleMemorySize/1024/1024)}} | Format-Table
Get-WmiObject -Class Win32_logicaldisk -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DriveType, DeviceID, VolumeName,  @{Name="Size";Expression={[math]::ceiling($_.Size /1GB)}} ,  @{Name="FreeSpace";Expression={[math]::ceiling($_.FreeSpace /1GB)}}, Compressed | where DriveType -eq 3 | Format-Table
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt")| Select-Object PSComputerName, BuildNumber,    BuildType,  Caption,    CodeSet,    OSArchitecture, SystemDrive,    TotalVisibleMemorySize, Version | Format-Table
Get-WmiObject -Class win32_product -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object Name, Version, Vendor,  InstallDate  | Format-Table
Get-WmiObject -Class Win32_Service -ComputerName (Get-Content "C:\Temp\Servers.txt") |  Select-Object  PSComputerName, DisplayName, StartName, PathName, StartMode| where DisplayName -Like "*xyz*" |Format-Table

到目前为止,我已经设法将以上内容拼凑在一起,以便从服务器上获取我需要的信息,但是现在我想对其进行格式化,以便我可以以我可以显示的格式整理每个服务器的信息

例如。

Server : ABC

RAM : 64 GB

Number of Processors : 8

Disk :
Table of disk Sizes Etc 

任何指针将不胜感激

【问题讨论】:

    标签: powershell powershell-remoting


    【解决方案1】:

    使用所有这些属性,您将获得一个嵌套对象数组,这可能最容易以 JSON 格式查看。

    我已将所有Get-WmiObject 更改为以下更新更快的Get-CimInstance cmdlet

     $result = Get-Content "C:\Temp\Servers.txt" | ForEach-Object {
        # create an ordered hashtable to store the results for each server
        $pcinfo = [ordered]@{}
    
        # System info
        $data = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $_
        $pcinfo['Computer']           = $data.PSComputerName
        $pcinfo['Memory (RAM in GB)'] = '{0:N2}' -f ($data.TotalPhysicalMemory / 1GB)
    
        # OS info
        $data = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $_
        $pcinfo['BuildNumber']            = $data.BuildNumber
        $pcinfo['BuildType']              = $data.BuildType
        $pcinfo['Caption']                = $data.Caption
        $pcinfo['CodeSet']                = $data.CodeSet
        $pcinfo['OSArchitecture']         = $data.OSArchitecture
        $pcinfo['SystemDrive']            = $data.SystemDrive
        $pcinfo['TotalVisibleMemorySize'] = $data.TotalVisibleMemorySize
        $pcinfo['Version']                = $data.Version
    
        # Product info (array of objects)
        $pcinfo['Products'] = Get-CimInstance -ClassName Win32_Product -ComputerName $_ |
                              Select-Object Name, Version, Vendor, InstallDate
    
        # Local fixed disk info (array of objects)
        $pcinfo['FixedDrives'] = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $_ -Filter 'DriveType=3' | 
                                 Sort-Object DeviceID | 
                                 Select-Object DriveType, DeviceID, VolumeName, 
                                               @{Name="Size";Expression={"{0:N2} GB" -f ($_.Size / 1GB)}}, 
                                               @{Name="FreeSpace";Expression={"{0:N2} GB" -f ($_.FreeSpace / 1GB)}}, 
                                               Compressed
    
        # Services info (array of objects)
        $pcinfo['Services'] = Get-CimInstance -ClassName Win32_Service -ComputerName $_ | 
                              Where-Object { $_.DisplayName -like '*Adobe*' } | 
                              Select-Object DisplayName, StartName, PathName, StartMode
    
        # convert the hashtable to PSObject and output 
        [PsCustomObject]$pcinfo
    }
    
    # output the whole structure as JSON for easier reading and optionally save it to file
    $result | ConvertTo-Json -Depth 3 # | Set-Content -Path 'Path\To\Output.json' -Force
    

    【讨论】:

    • 谢谢你,这让我可以继续下去。虽然 Json 可能无法提供我正在寻找的那种输出,但我正在寻找基于 txt 或 html 的输出,我可以将其复制粘贴到文档中。我了解您的代码背后的基本思想,并且应该能够将其转换为我需要的输出格式。
    • 捕获为 JSON,输出为 CSV、XML、HTML 等...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 2014-03-23
    • 2012-03-09
    • 1970-01-01
    • 2020-01-14
    相关资源
    最近更新 更多