Excel导出不工作在我的脚本

感谢您的反馈,他们给我改进的脚本在我的桌面上打开了一个excel文档,因为这将在整个networking上运行,我不希望发生这种情况。 但是,我确实希望保留单元格的工作表的devise。 这是我的。

$Date = [DateTime]::Now.AddDays(-1) $Date.tostring("MM-dd-yyyy"), $env:Computername $objExcel.visible = $True $objSheet = $objWorkbook.Worksheets.Item(1) $objSheet.Cells.Item(1,1) = "Server" $objSheet.Cells.Item(1,2) = "LogName" $objSheet.Cells.Item(1,3) = "Time" $objSheet.Cells.Item(1,4) = "Source" $objSheet.Cells.Item(1,5) = "Message" $objSheetFormat = $objSheet.UsedRange $objSheetFormat.Interior.ColorIndex = 19 $objSheetFormat.Font.ColorIndex = 11 $objSheetFormat.Font.Bold = $True $row = 1 $servers = gc c:\Myscripts\servers.txt foreach ($server in $servers) { $row = $row + 1 $AppLog = Get-EventLog -LogName Application -EntryType Error -computer $server -Newest 5 

所以如何在Excel中获得这个表单,而不是打开一个文件,只是将文件保存到我的指定位置,像下面的东西?

 { | Export-Csv -Path c:\ ('security-log-{0}.csv' -f ([DateTime]::Now).ToString("MM-dd-yyyy") } 

您可以调用Workbook上的SaveAs()方法来保存它:

 $objWorkbook.SaveAs('C:\security-log-{0:MM-dd-yyyy}.csv' -f [datetime]::Now)