powershell 操作Excel 附加释放资源

$strPath="C:\powershell\test.xls"

$objexcel=New-Object -ComObject excel.application

$objexcel.Visible=$false

$workbook=$objexcel.Workbooks.add()

$sheet=$workbook.worksheets.item(1)

$x=2


$strComputer="."

$objwmi=Get-WmiObject -Class win32_share

 $sheet.cells.item(1,1)=("Name of Share")

 $sheet.cells.item(1,2)=("Description of share")

 $sheet.cells.item(1,3)=("Type of share")

 foreach($objshare in $objwmi)

 {

    $sheet.cells.item($x,1)=$objshare.Name

    $sheet.cells.item($x,2)=$objshare.Description

    $sheet.cells.item($x,3)=$objshare.type

    $x++

 }

 $range=$sheet.usedrange

 $range.Entirecolumn.autofit()

$objexcel.ActiveWorkbook.SaveAs($strPath)

$objexcel.quit()

$range=$null

$sheet=$null

$workbook=$null

$objexcel=$null

[gc]::collect()


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。