导出为CSV而不是XLS文件

我有一个脚本,把一切都很好地放在电子表格中。 问题是,我需要它作为一个CSV文件导出。 所有的foreach循环在这里完全让我困惑,至于把脚本中的导出csv函数放在哪里。 如果有人能够就我如何把领域变成一个CSV文件,我将不胜感激。

$date = 0 $date = get-date -format "yyyy-MMM-dd-hhmm" $date #New Excel Application $Excel = New-Object -Com Excel.Application $Excel.visible = $False # Create 1 worksheets $Excel = $Excel.Workbooks.Add() # Assign each worksheet to a variable and # name the worksheet. $Sheet1 = $Excel.Worksheets.Item(1) $Sheet1.Name = "HH_SERVERS" #Create Heading for General Sheet $Sheet1.Cells.Item(1, 1) = "Machine_Name" $Sheet1.Cells.Item(1, 2) = "OS" $Sheet1.Cells.Item(1, 3) = "Software" $Sheet1.Cells.Item(1, 4) = "Vendor" $Sheet1.Cells.Item(1, 5) = "Version" $colSheets = ($Sheet1) foreach ($colorItem in $colSheets) { $intRow = 2 $intRowDisk = 2 $intRowSoft = 2 $intRowNet = 2 $WorkBook = $colorItem.UsedRange $WorkBook.Interior.ColorIndex = 20 $WorkBook.Font.ColorIndex = 11 $WorkBook.Font.Bold = $True } #Auto Fit all sheets in the Workbook foreach ($colorItem in $colSheets) { $WorkBook = $colorItem.UsedRange $WorkBook.EntireColumn.AutoFit() clear } $Servers = get-content "c:\temp\HH_Servers.txt" foreach ($Server in $Servers) { $GenItems2 = gwmi Win32_OperatingSystem -Comp $Server $Software = gwmi Win32_Product -Comp $Server # Populate General Sheet(1) with information foreach ($objItem in $GenItems2) { $Sheet1.Cells.Item($intRow, 2) = $objItem.Caption } #Populate Software Sheet foreach ($objItem in $Software) { $Sheet1.Cells.Item($intRowSoft, 1) = $Server $Sheet1.Cells.Item($intRowSoft, 3) = $objItem.Name $Sheet1.Cells.Item($intRowSoft, 4) = $objItem.Vendor $Sheet1.Cells.Item($intRowSoft, 5) = $objItem.Version $intRowSoft = $intRowSoft + 1 } } $outputfile = "c:\temp\" + $date.toString() + "-HH_Server_Software" $Excel.SaveAs($outputfile) $Excel.Close() Write-Host "*******************************" -ForegroundColor Green Write-Host "The Report has been completed." -ForeGroundColor Green Write-Host "*******************************" -ForegroundColor Green # ======================================================================== # END of Script # ================== 

您不能将整个工作簿保存为CSV。 您需要保存个人工作表。 CSV文件格式的值是6(不记得我发现的地方):

 $xlCSV = 6 $outputfile = "c:\temp\" + $date.toString() + "-HH_Server_Software.csv" $Sheet1.SaveAs($outputfile, $xlCSV) 

(使用Excel 2013在Windows 7上testing)

感谢@Matt对链接到XLFileFormat枚举的评论。