Powershell未将多行写入Excel
希望有人能提供帮助,我是Powershell的新手,我尝试过创建一些脚本来自动化一些内部流程。我想我已经跳到了最深处,但我就是不能让下面的工作 本质上,我试图使用WMI调用WMI从网络中的计算机检索数据 脚本使用我的C:Drive中的主机名列表来运行查询,并输出它应该输出的数据。有人能帮忙吗Powershell未将多行写入Excel,excel,powershell,automation,Excel,Powershell,Automation,希望有人能提供帮助,我是Powershell的新手,我尝试过创建一些脚本来自动化一些内部流程。我想我已经跳到了最深处,但我就是不能让下面的工作 本质上,我试图使用WMI调用WMI从网络中的计算机检索数据 脚本使用我的C:Drive中的主机名列表来运行查询,并输出它应该输出的数据。有人能帮忙吗 #Create new Excel workbook and write fields. $excel = New-Object -ComObject excel.application $excel.
#Create new Excel workbook and write fields.
$excel = New-Object -ComObject excel.application
$excel.visible = $False
$workbook = $excel.Workbooks.Add()
$workbook.Worksheets.Add() | Out-Null
$excel.DisplayAlerts = $False
$excel.Rows.Item(1).Font.Bold = $true
$Sheet= $workbook.Worksheets.Item(1)
$Sheet.Name = 'Server Information'
$Sheet.Cells.Item(1,1) = "Manufacturer"
$Sheet.Cells.Item(1,2) = "Hostname"
$Sheet.Cells.Item(1,3) = "PC Model"
$Sheet.Cells.Item(1,4) = "Username"
$Sheet.Cells.Item(1,5) = "Serial Number"
$Sheet.Cells.Item(1,6) = "OS Architecture"
$Sheet.Cells.Item(1,7) = "HDD Model"
$Sheet.Cells.Item(1,8) = "Total Disk Size (GB)"
$Sheet.Cells.Item(1,9) = "Physical Memory (GB)"
#Import data from text file.
Write-Host "Enter file path for hostnames list..." -ForegroundColor yellow
$computers = (Read-Host 'Insert File Path')
$computername = Get-Content $computers -ErrorAction "Inquire"
Write-Host ""
Write-Host "Excel workbook generated successfully. Writing data to rows and columns..." -ForegroundColor yellow
Write-Host $computername.count lines of data imported.
Write-Host ""
Write-Host "Starting WMI Querying..." -ForegroundColor yellow
#Loop through the Array and add data into the excel file created.
foreach ($computername in $computers) {
($Manufacturer,$Model,$User,$SerialNumber,$OSType,$DDModel,$DDSize,$RAMSize) = $computername.split('|')
$introw = $Sheet.UsedRange.Rows.Count + 1
$Manufacturer = $cs.Manufacturer
$Model = $cs.Model
$User = $cs.UserName
$SerialNumber = $bios.SerialNumber
$OSType = $os.Architecture
$DDModel = $dd.Model
$DDSize = $dd.Size
$RAMSize = $cs.PhysicalSize
(Get-Content C:\nodes.txt) | ForEach-Object {
$cs = gwmi win32_computersystem | Select-Object Manufacturer,@{Name="PC Model"; Expression = {$cs.Model}},Username,@{Name="Physical Memory (GB)";e={[math]::truncate($_.TotalPhysicalMemory /1GB)}}
$bios = gwmi win32_bios | Select-Object SerialNumber
$os = gwmi win32_OperatingSystem | select-object OSArchitecture
$dd = gwmi win32_DiskDrive | select-object Model,@{Name="Total Disk Size (GB)";e={[math]::truncate($dd.Size /1GB)}}
$Sheet.cells.item($introw, 1) = $Manufacturer
$Sheet.cells.item($introw, 2) = $Model
$Sheet.cells.item($introw, 3) = $User
$Sheet.cells.item($introw, 4) = $SerialNumber
$Sheet.cells.item($introw, 5) = $OSType
$Sheet.cells.item($introw, 6) = $DDModel
$Sheet.cells.item($introw, 7) = $DDSize
$Sheet.cells.item($introw, 8) = $RAMSize
$Sheet.UsedRange.EntireColumn.AutoFit();
}
}
#Write and output to Excel file.
$usedRange = $Sheet.UsedRange
$usedRange.EntireColumn.AutoFit() | Out-Null
$workbook.SaveAs("C:\Machine Inventory.xlsx")
$excel.Quit()
Write-Host ""
Write-Host "Process complete! The data has been exported to C:\Machine Inventory.xlsx" -ForegroundColor yellow
Write-Host ""
Write-Host "Press any key to continue ..." -ForegroundColor yellow
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
在foreach对象脚本块中,您不会增加$introw,所以您的脚本会反复重写一行。将$introw+=1添加到Get Content C:\nodes.txt | ForEach Object{…}块的末尾。应该这样做。我很确定脚本不会这样做,但是您是否能够只输出数据而不是将其写入Excel?若你们输出到屏幕上,它会显示正确吗?如果是这样,一种更好的方法是创建自定义对象数组,将该数组转换为以制表符分隔的CSV,将其复制到剪贴板,然后将其粘贴到Excel。如果你的脚本可以输出正确的信息到屏幕上,我可以帮助其余的。