首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Get-WmiObject修复此Powershell脚本?

如何使用Get-WmiObject修复此Powershell脚本?
EN

Stack Overflow用户
提问于 2017-09-13 15:10:57
回答 2查看 1.6K关注 0票数 2

我有一段代码,它从域中的计算机获取信息并输出到csv文件。我试图添加一行新代码来获取计算机的磁盘信息,但我无法让它像预期的那样工作。

代码语言:javascript
复制
# 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

这是磁盘的代码行。

代码语言:javascript
复制
$computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId

还有..。

代码语言:javascript
复制
'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[],而不是显示信息。

EN

回答 2

Stack Overflow用户

发布于 2017-09-15 09:20:21

这在某种程度上对我或我来说是有效的,但它只捕获了第一个驱动器的信息。而且,可用空间大于磁盘大小,这很奇怪。

代码语言:javascript
复制
$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
票数 1
EN

Stack Overflow用户

发布于 2017-09-13 15:16:00

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

代码语言:javascript
复制
$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就不会将其视为对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46191081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档