gpt4 book ai didi

powershell - PowerShell将多个变量导出到CSV?

转载 作者:行者123 更新时间:2023-12-03 00:01:28 27 4
gpt4 key购买 nike

一般信息

$ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem | Select -Property Model , Manufacturer , Description , PrimaryOwnerName , SystemType

启动配置
$BootConfiguration = Get-WmiObject -Class Win32_BootConfiguration | Select -Property Name , ConfigurationPath 

BIOS信息
$BIOS = Get-WmiObject -Class Win32_BIOS | Select -Property PSComputerName , Manufacturer , Version #| Export-Csv -InputObject 

操作系统信息
$OS = Get-WmiObject -Class Win32_OperatingSystem | Select -Property Caption , CSDVersion , OSArchitecture , OSLanguage  

我想将所有这些变量导出到带有标题的csv文件中,但无法这样做。

最佳答案

下面将get-wmiobject命令分别创建的每个对象的noteproperties组合到$ report变量中。从那里您可以导出到csv。

通过遍历gwmi调用创建的每个变量并将noteproperties添加到report变量的循环,可以更好地简化此操作。

$ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem | Select -Property Model , Description , PrimaryOwnerName , SystemType
$BootConfiguration = Get-WmiObject -Class Win32_BootConfiguration | Select -Property Name , ConfigurationPath
$BIOS = Get-WmiObject -Class Win32_BIOS | Select -Property PSComputerName , Manufacturer , Version
$OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem | Select -Property Caption , CSDVersion , OSArchitecture , OSLanguage

$report = New-Object psobject
$report | Add-Member -MemberType NoteProperty -name Model -Value $ComputerSystem.Model
$report | Add-Member -MemberType NoteProperty -name Description -Value $ComputerSystem.Description
$report | Add-Member -MemberType NoteProperty -name PrimaryOwnerName -Value $ComputerSystem.PrimaryOwnerName
$report | Add-Member -MemberType NoteProperty -name SystemType -Value $ComputerSystem.SystemType
$report | Add-Member -MemberType NoteProperty -name Name -Value $BootConfiguration.Name
$report | Add-Member -MemberType NoteProperty -name ConfigurationPath -Value $BootConfiguration.ConfigurationPath
$report | Add-Member -MemberType NoteProperty -name PSComputerName -Value $BIOS.PSComputerName
$report | Add-Member -MemberType NoteProperty -name Manufacturer -Value $BIOS.Manufacturer
$report | Add-Member -MemberType NoteProperty -name Version -Value $BIOS.Version
$report | Add-Member -MemberType NoteProperty -name Caption -Value $OS.Caption
$report | Add-Member -MemberType NoteProperty -name CSDVersion -Value $OS.CSDVersion
$report | Add-Member -MemberType NoteProperty -name OSArchitecture -Value $OS.OSArchitecture
$report | Add-Member -MemberType NoteProperty -name OSLanguage -Value $OS.OSLanguage

$report | export-csv .\file.csv -NoTypeInformation

关于powershell - PowerShell将多个变量导出到CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26869622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com