Powershell电脑信息查询导出到excel
Powershell Computer information query export to excel
我正在尝试获取计算机列表并从中提取信息并将其导出到 excel sheet 中,其中我每行都有系统的所有信息。我正在尝试获取以下信息:
- 计算机名
- OS
- BIOS
- 上次启动
- 等等
我写的代码:
$computers = Get-Content "C:\example\Computers.txt)"
$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object PSComputerName, Manufacturer, SerialNumber, SMBIOSBIOSVersion
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | select csname, lastbootuptime
$objects +=$OSinfo +=$BIOSinfo +=$lastboot
$objects | Export-csv -Path "C:\example\output.csv"
但是,我不知道如何将所有这些信息放在一个跨页sheet 标签中。我也不知道如何告诉脚本如果它不能 ping 或找不到它,只说“离线”
这应该适合你:
$computers = Get-Content "C:\example\Computers.txt"
# Define the properties we want to select from each query
$osColumns = 'PSComputerName', 'Caption', 'OSArchitecture', 'Version', 'BuildNumber'
$biosColumns = 'PSComputerName', 'Manufacturer', 'SerialNumber', 'SMBIOSBIOSVersion'
$lastbootColumns = 'csname', 'lastbootuptime'
# Obtain the desired information from Wmi/Cim
$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select-Object $osColumns
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object $biosColumns
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | Select-Object $lastbootColumns
# Iterate over the computers and collect the computer-specific information
$computerInfo = foreach( $computer in $computers ) {
$thisOSInfo = $OSInfo | Where-Object { $_.PSComputerName -eq $computer }
$thisBIOSInfo = $BIOSinfo | Where-Object { $_.PSComputerName -eq $computer }
$thisLastboot = $lastboot | Where-Object { $_.csname -eq $computer }
# This row object will be returned as a PSCustomObject for export
$row = @{}
foreach( $prop in @( $thisOSInfo.psobject.Properties.Name ) ) {
if( $prop -in $osColumns ) {
$row[$prop] = $thisOSInfo.$prop
}
}
foreach( $prop in @( $thisBIOSInfo.psobject.Properties.Name ) ) {
if( $prop -in $biosColumns ) {
$row[$prop] = $thisBIOSInfo.$prop
}
}
foreach( $prop in @( $thisLastboot.psobject.Properties.Name ) ) {
if( $prop -in $lastbootColumns ) {
$row[$prop] = $thisLastboot.$prop
}
}
[PSCustomObject]$row
}
$computerInfo | Export-Csv -NoTypeInformation \path\to\report.csv
工作方式:
- Well-define 我们希望从每个查询中获得的列。稍后我们将使用它们来确保我们只从结果查询响应中获取我们想要的属性。请注意
Get-CimInstance
将具有 csname
属性 而不是 PSComputerName
.
- 查询 WMI 类 以获取有关已知计算机的信息,将每个响应存储在其自己的变量中。
- 遍历查询的每台计算机,从每个查询响应构建该行信息。
- 如果查询 属性 存在于相应的
*Columns
变量中,则只添加它。这对于您的代码来说不是必需的,但在导出其他一些 non-WMI/Cim 对象时,或者如果您之前不使用 Select-Object
以减少噪音,则变得很有必要。
- 每一行都被定义为易于修改的 hastable,然后转换为
PSCustomObject
以使其在 return 上表现得更像一个不可变对象。这对于 Export-Csv
的输出以您想要的方式输出是必要的。
- 将
Export-Csv
的计算机信息导出到 CSV 文件,该文件现在是 PSCustomObjects
的数组,其中每一行应该是您查询的一台计算机。
我正在尝试获取计算机列表并从中提取信息并将其导出到 excel sheet 中,其中我每行都有系统的所有信息。我正在尝试获取以下信息:
- 计算机名
- OS
- BIOS
- 上次启动
- 等等
我写的代码:
$computers = Get-Content "C:\example\Computers.txt)"
$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object PSComputerName, Manufacturer, SerialNumber, SMBIOSBIOSVersion
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | select csname, lastbootuptime
$objects +=$OSinfo +=$BIOSinfo +=$lastboot
$objects | Export-csv -Path "C:\example\output.csv"
但是,我不知道如何将所有这些信息放在一个跨页sheet 标签中。我也不知道如何告诉脚本如果它不能 ping 或找不到它,只说“离线”
这应该适合你:
$computers = Get-Content "C:\example\Computers.txt"
# Define the properties we want to select from each query
$osColumns = 'PSComputerName', 'Caption', 'OSArchitecture', 'Version', 'BuildNumber'
$biosColumns = 'PSComputerName', 'Manufacturer', 'SerialNumber', 'SMBIOSBIOSVersion'
$lastbootColumns = 'csname', 'lastbootuptime'
# Obtain the desired information from Wmi/Cim
$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select-Object $osColumns
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object $biosColumns
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | Select-Object $lastbootColumns
# Iterate over the computers and collect the computer-specific information
$computerInfo = foreach( $computer in $computers ) {
$thisOSInfo = $OSInfo | Where-Object { $_.PSComputerName -eq $computer }
$thisBIOSInfo = $BIOSinfo | Where-Object { $_.PSComputerName -eq $computer }
$thisLastboot = $lastboot | Where-Object { $_.csname -eq $computer }
# This row object will be returned as a PSCustomObject for export
$row = @{}
foreach( $prop in @( $thisOSInfo.psobject.Properties.Name ) ) {
if( $prop -in $osColumns ) {
$row[$prop] = $thisOSInfo.$prop
}
}
foreach( $prop in @( $thisBIOSInfo.psobject.Properties.Name ) ) {
if( $prop -in $biosColumns ) {
$row[$prop] = $thisBIOSInfo.$prop
}
}
foreach( $prop in @( $thisLastboot.psobject.Properties.Name ) ) {
if( $prop -in $lastbootColumns ) {
$row[$prop] = $thisLastboot.$prop
}
}
[PSCustomObject]$row
}
$computerInfo | Export-Csv -NoTypeInformation \path\to\report.csv
工作方式:
- Well-define 我们希望从每个查询中获得的列。稍后我们将使用它们来确保我们只从结果查询响应中获取我们想要的属性。请注意
Get-CimInstance
将具有csname
属性 而不是PSComputerName
. - 查询 WMI 类 以获取有关已知计算机的信息,将每个响应存储在其自己的变量中。
- 遍历查询的每台计算机,从每个查询响应构建该行信息。
- 如果查询 属性 存在于相应的
*Columns
变量中,则只添加它。这对于您的代码来说不是必需的,但在导出其他一些 non-WMI/Cim 对象时,或者如果您之前不使用Select-Object
以减少噪音,则变得很有必要。
- 如果查询 属性 存在于相应的
- 每一行都被定义为易于修改的 hastable,然后转换为
PSCustomObject
以使其在 return 上表现得更像一个不可变对象。这对于Export-Csv
的输出以您想要的方式输出是必要的。 - 将
Export-Csv
的计算机信息导出到 CSV 文件,该文件现在是PSCustomObjects
的数组,其中每一行应该是您查询的一台计算机。