从列表文件中远程获取序列号和型号并将其导出到 PowerShell 中的 arranged excel

Remotely get serial number and model from list file and export it to arranged excel in PowerShell

我有不错的 PowerShell 代码,可以从列表中远程获取序列号和型号(以及其他详细信息)并将其导出到 PowerShell 中的漂亮视图。

但是,我想将它导出到一个安排好的 csv 文件,所以不是这样:

我要那个:

这是原始代码:

$ArrComputers =  "yakovcomputer"
#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
        write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
        "-------------------------------------------------------"
        "Manufacturer: " + $computerSystem.Manufacturer
        "Model: " + $computerSystem.Model
        "Serial Number: " + $computerBIOS.SerialNumber
        "CPU: " + $computerCPU.Name
        "HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
        "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
        "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
        "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
        "User logged In: " + $computerSystem.UserName
        "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        ""
        "-------------------------------------------------------"
}

所以我尝试在开头添加 Import-Csv C:list.txt | ForEach-Object(其中包含 yakovcomputer 名称)

(未决定是否使用 $computers = Get-Content c:\list.txt),

并在末尾添加export-csv c:\temp\list.csv命令。

所以现在看起来像那样(编辑代码):

Import-Csv C:list.csv | ForEach-Object
    {
        $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
    }
export-csv c:\temp\list.csv

但是一直报错

我知道我在这里写错了一些东西(代码),但是,实现它的正确方法是什么?

如果您的 list.txt 只是计算机名称的纯文本文件,每行一个名称,Get-Content 就足够了。您可以使用 $_$PSItem.

访问当前行(当前项目)

Export-Csv 需要 个对象。因此,您必须将数据转换为对象。有几种方法可以做到这一点,其中大多数都需要创建一个 HashTable。这是我的首选版本:

Get-Content c:\list.txt | foreach
{
    $computerSystem = Get-WmiObject Win32_ComputerSystem -Computer $_
    $computerBIOS = Get-WmiObject Win32_BIOS -Computer $_
    $computerOS = Get-WmiObject Win32_OperatingSystem -Computer $_
    $computerCPU = Get-WmiObject Win32_Processor -Computer $_
    $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $_ -Filter drivetype=3
    New-Object PSObject -Property @{
        "Manufacturer" = $computerSystem.Manufacturer
        "Model" = $computerSystem.Model
        "Serial Number" = $computerBIOS.SerialNumber
        "CPU" = $computerCPU.Name
        "HDD Capacity"  = ("{0:N2}" -f ($($computerHDD|measure Size -sum).Sum/1GB) + "GB")
        "HDD Space" = ("{0:P2}" -f ($($computerHDD|measure FreeSpace -sum).Sum/$($computerHDD|measure Size -sum).Sum) + " Free (" + "{0:N2}" -f ($($computerHDD|measure FreeSpace -sum).Sum/1GB) + "GB)")
        "RAM" = ("{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB")
        "Operating System" = ($computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion)
        "User logged In" = $computerSystem.UserName
        "Last Reboot" = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
    }
} | Export-Csv c:\temp\list.csv -NoTypeInformation

注意 $($computerHDD|measure Size -sum).Sum 结构,如果有多个硬盘,则将所有硬盘的值相加。

此外,请考虑改用 CIM instead of WMI

在 powershell 脚本中,仅 HDD 行不工作

制造商:Hewlett-Packard 型号:HP Pro3500 系列 序列号:CZC3191456 CPU: Intel(R) Core(TM) i3-3220 CPU @ 3.30GHz 方法调用失败,因为 [System.Object[]] 不包含名为 'op_Division' 的方法。 在 line:18 char:9

  •     "HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB ...
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (op_Division:String) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

方法调用失败,因为 [System.Object[]] 不包含名为 'op_Division' 的方法。 在 line:19 char:9

  •     "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computer ...
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (op_Division:String) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

内存:7.89GB 操作系统:Microsoft Windows 10 Pro,Service Pack:0