【问题标题】:How to export information about the monitor to a csv file using PowerShell?如何使用 PowerShell 将有关监视器的信息导出到 csv 文件?
【发布时间】:2020-06-05 06:43:31
【问题描述】:

我是 PowerShell 新手,正在努力编写一个脚本来导出三个监视器的 UserFriendlyName(参见下面的代码)。这是我目前所拥有的:

$monitors = Get-WmiObject -Namespace root\wmi -Class wmiMonitorID

Get-CimInstance -Namespace root\wmi -ClassName wmimonitorid -ComputerName $ComputerName |

foreach {
    $Object = New-Object PSObject -Property @{
        MonitorName = ($monitors.UserFriendlyName -notmatch '^0$' | foreach {[char]$_}) -join ""
        MonitorSerial = ($monitors.serialnumberid -notmatch '^0$' | foreach {[char]$_}) -join ""
    } 
}

$Object | Select MonitorName,MonitorSerial
$Object | Export-Csv -append -force /Computer.csv -NoTypeInformation

我得到的结果:

MonitorName                MonitorSerial                        
-----------                -------------                        
27B1DELL P2717HDELL P2717H GUHJBHA018695YKNFG6CQAGLLYKNFG71KAPTL

我希望每个监视器名称和序列号在它们自己的列下(Monitor 1Monitor 2Monitor 3 和序列号相同)但值是在一起的。非常感谢任何帮助。

我希望将上述内容与此合并:

$computerSystem = Get-CimInstance CIM_ComputerSystem
$computerBIOS = Get-CimInstance CIM_BIOSElement
$computerOS = Get-CimInstance CIM_OperatingSystem
$computerCPU = Get-CimInstance CIM_Processor
$computerHDD = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID = 'C:'"

Get-CimInstance -Namespace root\wmi -ClassName wmimonitorid -ComputerName $ComputerName | 

foreach {


$Object = New-Object PSObject -Property @{
    "Computer Name" = $computerSystem.Name
    "Operating System" = $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
    "Manufacturer" = $computerSystem.Manufacturer
    "Model" = $computerSystem.Model
    "Serial Number" = $computerBIOS.SerialNumber
    "CPU" = $computerCPU.Name
    "HDD Capacity" = "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
    "RAM" = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
    "User logged In" = $computerSystem.UserName
    } 
}

$Object | Export-Csv -append -force /Computer.csv -NoTypeInformation

【问题讨论】:

  • 您可以遍历监视器列表并使用道具名称中的索引号构建每个属性。 $SysInfoProps.Add("Vid_Monitor_${Index}_Name" 之类的东西,然后使用哈希表构建 PSCustomObject。

标签: powershell csv export-to-csv inventory get-wmiobject


【解决方案1】:

您的代码似乎有问题。您应该将每个监视器添加到数组或列表中。

$monitors = Get-WmiObject -Namespace root\wmi -Class wmiMonitorID

$allMonitors = @()

Get-CimInstance -Namespace root\wmi -ClassName wmimonitorid -ComputerName $ComputerName |

foreach {
    $Object = [PSCustomObject]@{
        MonitorName = ($monitors.UserFriendlyName -notmatch '^0$' | foreach {[char]$_}) -join ""
        MonitorSerial = ($monitors.serialnumberid -notmatch '^0$' | foreach {[char]$_}) -join ""
    } 

    $allMonitors += $Object
}

$allMonitors | Select MonitorName,MonitorSerial
$allMonitors | Export-Csv -append -force /Computer.csv -NoTypeInformation

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    • 2016-03-29
    • 2020-11-11
    • 1970-01-01
    • 2019-11-11
    • 2013-07-15
    • 1970-01-01
    相关资源
    最近更新 更多