2

我有这段代码从域中的计算机获取信息并输出到 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[]而不是显示 info 。

4

2 回答 2

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
于 2017-09-15T01:20:21.290 回答
0

您可以这样做以获得我认为您追求的结果:

$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-StringExport-CSV

于 2017-09-13T07:16:00.120 回答