csv 远程从列表文件中获取序列号和型号,并将其导出到PowerShell中排列好的Excel中

fnatzsnv  于 2023-04-03  发布在  Shell
关注(0)|答案(2)|浏览(112)

我有很好的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

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

gwbalxhn

gwbalxhn1#

如果您的list.txt只是一个纯文本文件的计算机名称,每行一个名称,Get-Content就足够了。您可以使用$_$PSItem访问当前行(当前项)。
Export-Csv需要objects。所以你必须把你的数据转换成对象。有几种方法可以做到这一点,大多数都需要创建一个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结构,如果有多个HDD,则将所有HDD的值相加。
另外,请考虑使用CIM instead of WMI

zyfwsgd6

zyfwsgd62#

在powershell脚本中,仅HDD行无法工作
制造商:Hewlett-Packard型号:HP Pro3500系列序列号:CZC3191456 CPU:英特尔(R)酷睿(TM)i3-3220 CPU@3.30GHz方法调用失败,因为[System.Object[]]不包含名为“op_Division”的方法。第18行,字符:9

"HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 类别信息:无效操作:(运算符分部:字符串)[],运行时异常
  • 完全限定错误ID:未找到方法

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

"HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computer ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • 类别信息:无效操作:(运算符分部:字符串)[],运行时异常
  • 完全限定错误ID:未找到方法

操作系统:Windows XPMicrosoft Windows 10(32-bit)Service Pack 10

相关问题