【问题标题】:How to fix this Powershell script using Get-WmiObject?如何使用 Get-WmiObject 修复此 Powershell 脚本?
【发布时间】:2017-09-13 07:10:57
【问题描述】:

我有这段代码从域中的计算机获取信息并输出到 csv 文件。我尝试添加一行新代码来获取计算机的磁盘信息,但我无法使其按预期工作。

# Get the list of all computer names and export to CSV file
Get-ADComputer -Filter * | select Name | Export-Csv -Path 'C:\temp\computers.csv' -NoTypeInformation

# Import the computer names from CSV file and get the system information
$computers = Import-Csv “C:\Temp\computers.csv” | ForEach {

    $computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
    $computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
    $computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
    $computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
    $computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId

    [PSCustomObject]@{
        'PCName' = $computerSystem.Name    
        'Model' = $computerSystem.Model   
        'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)    
        'CPU' = $computerCPU.Name    
        'OS' = $computerOS.caption   
        'SN' = $computerSN.SerialNumber
        'User' = $computerSystem.UserName 
        'Disk' = $computerDisk.DeviceId | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
    }

} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation

这是磁盘的代码行。

$computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId

还有……

'Disk' = $computerDisk.DeviceId | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

其他参数有效,但只有磁盘信息部分无效。输出为:System.Object[],而不是显示信息。

【问题讨论】:

  • win32_logicaldisk 返回驱动器集合,因为计算机可以有多个驱动器。所以你需要一个 foreach 循环遍历每个驱动器
  • 您希望 CSV 中的 Disk 行看起来像什么?
  • 在磁盘列下,我需要磁盘号(D:、E: 等)、容量和可用大小(GB),可能需要 3 列,但我不知道如何实现。

标签: powershell get-wmiobject


【解决方案1】:

这对我来说有点用,但它只获取第一个驱动器的信息。此外,可用空间大于磁盘大小,这很奇怪。

$Computers = Import-Csv 'C:\Temp\computers.csv'

$Computers | ForEach {

    $computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
    $computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
    $computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
    $computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
    $computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId, Size, FreeSpace

    [PSCustomObject]@{
        'PCName' = $computerSystem.Name    
        'Model' = $computerSystem.Model   
        'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)    
        'CPU' = $computerCPU.Name    
        'OS' = $computerOS.caption   
        'SN' = $computerSN.SerialNumber
        'User' = $computerSystem.UserName 
        'Disk' = $computerDisk.DeviceId  | Format-Table | Out-String
        'Size' = $computerDisk.Size  | Format-Table | Out-String
        'Free Space' = $computerDisk.FreeSpace  | Format-Table | Out-String
    }

} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation

【讨论】:

    【解决方案2】:

    你可以这样做以获得我认为你想要的结果:

    $Computers = Import-Csv 'C:\Temp\computers.csv'
    
    $Computers | ForEach {
    
        $computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
        $computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
        $computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
        $computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
        $computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
    
        [PSCustomObject]@{
            'PCName' = $computerSystem.Name    
            'Model' = $computerSystem.Model   
            'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)    
            'CPU' = $computerCPU.Name    
            'OS' = $computerOS.caption   
            'SN' = $computerSN.SerialNumber
            'User' = $computerSystem.UserName 
            'Disk' = $computerDisk | Format-Table | Out-String
        }
    
    } | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation
    

    第一个问题是,在 $computerDisk = 行中,您使用 Select-Object 仅返回 DeviceID 属性,但后来尝试使用其他属性。

    第二个问题是你需要在输出时将Format-Table 传递到Out-String 以将其转换为字符串格式,这样Export-CSV 就不会将其视为对象。

    【讨论】:

    • 磁盘栏为空白。不知道为什么。
    • 让我们让它变得更简单。我只需要 DeviceId、容量和可用空间。不需要媒体类型。我认为由于每台计算机都有多个驱动器,因此磁盘输出为空。由于磁盘输出试图仅将信息添加到 1 列中。我说的对吗?
    • 您是在 Excel 中打开它进行检查吗?因为第一行是空白的,所以起初它看起来是空的,但它是多行的,所以只需展开该行,它就应该在那里。
    • 是的,完全是空白的。
    • 什么版本的windows?
    猜你喜欢
    • 2021-08-15
    • 2015-11-23
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 2012-08-11
    • 1970-01-01
    相关资源
    最近更新 更多