powershell查询硬件信息返回json

Posted 阿拉的梦想

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell查询硬件信息返回json相关的知识,希望对你有一定的参考价值。

powershell查询硬件信息返回json

#cpu信息
function getCpu($cpuInfo) 
    $info_list = '['
    get-wmiobject win32_processor | ForEach-Object 
        $info_list = "$info_list`"brand`":`"$($_.Manufacturer)`",`"model`":`"$($_.Name)`",`"threadSize`":`"$THREAD_SIZE`",`"coreSize`":`"$($_.NumberOfLogicalProcessors)`",`"processorSpeed`":`"$($_.CurrentClockSpeed)`",`"boostProcessorSpeed`":`"$($_.MaxClockSpeed)`",`"l3Cache`":`"$($_.L3CacheSize)`",`"voltage`":`"$($_.CurrentVoltage)`",`"name`":`"$($_.DeviceID)`","
    
    $info_list = $info_list.SubString(0, $info_list.Length - 1)
    return $info_list + "]"


#内存
function getMemory($memoryInfo) 
    $info_list = '['
    get-wmiobject Win32_PhysicalMemory | ForEach-Object 
        $info_list = "$info_list`"capacity`":`"$([int]($_.Capacity/1024/1024/1024))G`",`"brand`":`"$($_.Manufacturer)`",`"memoryType`":`"$($_.MemoryType)`",`"frequency`":`"$MEMORY_FREQUENCY`",`"sn`":`"$($_.SerialNumber)`",`"name`":`"$($_.Name)`","

    
    $info_list = $info_list.SubString(0, $info_list.Length - 1)
    return $info_list + "]"


#网卡信息
function getNetworkAdapter($netInfo) 
    $info_list = '['
    Get-NetAdapter  | ForEach-Object 
        $net = Get-NetIPAddress  -InterfaceAlias  $_.Name -AddressFamily IPv4
        $info_list = "$info_list`"ip`":`"$($net.IPAddress)`",`"macAddress`":`"$($_.MacAddress)`",`"rate`":`"$($_.LinkSpeed)`",`"name`":`"$($_.Name)`",`"useType`":`"$NET_USE_TYPE`","
    
    $info_list = $info_list.SubString(0, $info_list.Length - 1)
    return $info_list + "]"


#硬盘信息
function getDisk 
    $info_list = '['
    Get-WMIObject Win32_DiskDrive  | ForEach-Object 

        $info_list = "$info_list`"name`":`"$($_.Name)`",`"capacity`":`"$([int]($_.Size/1024/1024/1024))G`",`"brand`":`"$($_.Model)`",`"diskType`":`"$($_.InterfaceType)`",`"sn`":`"$($_.SerialNumber)`","
    
    $info_list = $info_list.SubString(0, $info_list.Length - 1)
    return $info_list + "]"
    

#获取IP
function getIp 
    
    Get-NetIPAddress -AddressFamily IPv4 | ForEach-Object 
        If ($_.IPAddress -ne '127.0.0.1') 
            $NET_IP = $_.IPAddress
        
    

    $res = "`"ip`":`"$NET_IP`""
    return $res






#系统信息
function getDeviceInfo 
    #内部方法 获取系统信息
    $header = 'Hostname','OSName','OSVersion','OSManufacturer','OSConfiguration','Obuildtype','RegisteredOwner','RegisteredOrganization','Product ID','Original Install Date','System Boot Time','System Manufacturer','System Model','System Type','Processor(s)','BiosVersion','Windows Directory','System Directory','Boot Device','System Locale','Input Locale','Time Zone','Total Physical Memory','Available Physical Memory','Virtual Memory: Max Size','Virtual Memory: Available','Virtual Memory: In Use','Page File Location(s)','Domain','Logon Server','Hotfix(s)','Network Card(s)'
    $systemInfo1 = systeminfo.exe /FO CSV |Select-Object -Skip 1 |ConvertFrom-CSV -Header $header

    #osquery方式查询系统信息
    #Set-Location -Path "C:\\home\\finance\\App\\osquery\\"
    $systemInfo2 = osqueryi.exe --json -A system_info | ConvertFrom-Json

    $res= "`"biosVersion`":`"$($systemInfo1.BiosVersion)`""
    $res="$res,`"name`":`"$($systemInfo1.Hostname)`""
    $res="$res,`"sn`":`"$($systemInfo2.uuid)`""
    $res="$res,`"res_host_os`":`"version`":`"$($systemInfo1.OSVersion)`",`"osBit`":`"$($systemInfo2.cpu_type)`",`"osType`":`"windows`",`"name`":`"$($systemInfo1.OSName)`""
    return $res;





function reportInfo 
   
    # 将数据构造成数组
    $CPU = getCpu
    $MEMORY = getMemory
    $NETWORK_ADAPTER = getNetworkAdapter
    $DISK = getDisk
    $IP = getIp
    $DEVICE = getDeviceInfo
    $INFO = "$IP,$DEVICE,`"res_host_cpu`":$CPU,`"res_host_memory`":$MEMORY,`"res_host_network`":$NETWORK_ADAPTER,`"res_host_disk`":$DISK"
    Write-Output $INFO
    


reportInfo


以上是关于powershell查询硬件信息返回json的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell:ConvertFrom-Json 将多个对象导出到 csv

使用 Powershell 解析从 Web 请求返回的 JSON 字节流而不写入文件

powershell Invoke-SQL查询返回System.data.datatables

JS字符串转成数学表达式

Powershell 查询信息中如何将内容过多的省略号(....)显示完整

Powershell 查询信息中如何将内容过多的省略号(....)显示完整