2015-01-06 113 views
2

如何在運行後保存下面的腳本?Powershell:Excel,運行後保存並關閉

腳本是:Powershell Disk Usage Report

$erroractionpreference = "SilentlyContinue" 
$a = New-Object -comobject Excel.Application 
$a.visible = $True 

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1) 

$c.Cells.Item(1,1) = "Server Name" 
$c.Cells.Item(1,2) = "Drive" 
$c.Cells.Item(1,3) = "Total Size (GB)" 
$c.Cells.Item(1,4) = "Free Space (GB)" 
$c.Cells.Item(1,5) = "Free Space (%)" 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 

$intRow = 2 

$colComputers = get-content "c:\servers.txt" 
foreach ($strComputer in $colComputers) 
{ 
$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3" 
foreach ($objdisk in $colDisks) 
{ 
$c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
$c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
$c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB) 
$c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB) 
$c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
$intRow = $intRow + 1 
} 
} 

根據https://social.technet.microsoft.com/Forums/windowsserver/en-US/919459dc-3bce-4242-bf6b-fdf37de9ae18/powershell-will-not-save-excel-file,這將工作,但我無法:

Add-Type -AssemblyName Microsoft.Office.Interop.Excel 
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault 
$Excel = New-Object -comobject Excel.Application 

$Excel.Visible = $true 

################ 
$Excel.workbooks.OpenText($file,437,1,1,1,$True,$True,$False,$False,$True,$False) 
$Excel.ActiveWorkbook.SaveAs($env:tmp + "\myfile.xls", $xlFixedFormat) 

$Excel.Workbooks.Close() 
$Excel.Quit()  
+1

無法集成此代碼?這不僅僅是在末尾的第二個片段中包含###後面的代碼的問題。你應該只需要在你的情況下將'$ Excel'更改爲'$'。 – Matt

回答

2

得到它的工作! - 特別感謝@馬特

完成腳本工作:

$erroractionpreference = "SilentlyContinue" 
$a = New-Object -comobject Excel.Application 
$a.visible = $True 
Add-Type -AssemblyName Microsoft.Office.Interop.Excel 
$xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault 


$a.Visible = $true 

$b = $a.Workbooks.Add() 
$c = $b.Worksheets.Item(1) 

$c.Cells.Item(1,1) = "Server Name" 
$c.Cells.Item(1,2) = "Drive" 
$c.Cells.Item(1,3) = "Total Size (GB)" 
$c.Cells.Item(1,4) = "Free Space (GB)" 
$c.Cells.Item(1,5) = "Free Space (%)" 

$d = $c.UsedRange 
$d.Interior.ColorIndex = 19 
$d.Font.ColorIndex = 11 
$d.Font.Bold = $True 

$intRow = 2 

$colComputers = get-content "c:\servers.txt" 
foreach ($strComputer in $colComputers) 
{ 
$colDisks = get-wmiobject Win32_LogicalDisk -computername $strComputer -Filter "DriveType = 3" 
foreach ($objdisk in $colDisks) 
{ 
$c.Cells.Item($intRow, 1) = $strComputer.ToUpper() 
$c.Cells.Item($intRow, 2) = $objDisk.DeviceID 
$c.Cells.Item($intRow, 3) = "{0:N0}" -f ($objDisk.Size/1GB) 
$c.Cells.Item($intRow, 4) = "{0:N0}" -f ($objDisk.FreeSpace/1GB) 
$c.Cells.Item($intRow, 5) = "{0:P0}" -f ([double]$objDisk.FreeSpace/[double]$objDisk.Size) 
$intRow = $intRow + 1 
} 
} 

$a.workbooks.OpenText($file,437,1,1,1,$True,$True,$False,$False,$True,$False) 
$a.ActiveWorkbook.SaveAs("C:\Users\Username\Desktop\myfile.xls", $xlFixedFormat) 

$a.Workbooks.Close() 
$a.Quit()  
2

要正確和完全關閉Excel,您還需要釋放COM引用。在我自己的測試中發現,除去Excel的變量還確保沒有剩餘的引用會使Excel.exe保持打開狀態(就像在ISE中調試一樣)。

不執行上述操作,如果您查看任務管理器,您可能會看到Excel仍在運行...在某些情況下,許多副本。

這同COM對象是如何包裝在「運行時可調用包裝」做

以下是骨架代碼應使用:

$excel = New-Object -ComObject Excel.Application 
$excel.Visible = $true 
$workbook = $excel.Workbooks.Add() 
# or $workbook = $excel.Workbooks.Open($xlsxPath) 

# do work with Excel... 

$workbook.SaveAs($xlsxPath) 
$excel.Quit() 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) 
# no $ needed on variable name in Remove-Variable call 
Remove-Variable excel 
2

這爲我工作:

$workbook.Close($false) 
$excel.Quit() 

[System.GC]::Collect() 
[System.GC]::WaitForPendingFinalizers() 

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workSheet) 
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) 

Remove-Variable -Name excel 
+0

謝謝。這是我唯一能夠徹底殺死Excel進程的方法,其他方法都失敗了。 – rincEwind

0

如MSDN文檔here中所述,ReleaseComObject調用只會將該COM對象的引用計數器減1。如果您的腳本有mul對同一個COM對象的tiple引用,它不會釋放該對象。

該文檔建議使用FinalReleaseComObject方法完全釋放COM對象並關閉所有Excel進程一次。

只有當您完成COM引用時才確定調用此方法,因爲不這樣做可能會導致難以調試的錯誤。