我写了一个简短的Powershell脚本来复制一些Excel数据。该脚本可以正常工作,但每次运行时都会打开Excel,甚至认为可见属性设置为$ false。任何人有任何想法为什么?我搜索了答案,但找不到任何解决方案。我也显示了该属性,并且设置正确。Powershell COM Excel可见不工作
打开工作簿时Excel打开。
感谢,
克里斯J.
代码:
## function to close all com objects
function Release-Ref ($ref{
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
#Read-Host -Prompt "Press Enter to exit"
#####################################################################
## Load excel com objects attach to file
#####################################################################
$ExcelPath = 'C:\Chris_Test.xls'
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $False
write-host "Visible 1"
write-host $Excel.Visible
$ExcelWorkBook = $Excel.Workbooks.Open($ExcelPath)
$Excel.Visible = $False
write-host "Visible 2"
write-host $Excel.Visible
$ExcelWorkSheet = $Excel.WorkSheets.item("Sheet1")
$Excel.Visible = $False
write-host "Visible 3"
write-host $Excel.Visible
$row = 2
$South_HX_Avg = "This"
$Middle_HX_Avg = " is a "
$North_HX_Avg = "test."
##
## Cells.Item(Row,Column)
#$ExcelWorkSheet.Cells.Item($row,1).Value2 = $South_HX_Avg
#$ExcelWorkSheet.Cells.Item(2,2).Value2 = $Middle_HX_Avg
#$ExcelWorkSheet.Cells.Item(2,3).Value2 = $North_HX_Avg
#####################################################################
# Close connections to Excel
# set interactive to false so no save buttons are shown
#####################################################################
$Excel.DisplayAlerts = $false
$Excel.ScreenUpdating = $false
$Excel.Visible = $False
write-host "Visible 4"
write-host $Excel.Visible
$Excel.Visible = $false
$Excel.Visible = $False
write-host "Visible 5"
write-host $Excel.Visible
$Excel.UserControl = $false
$Excel.Interactive = $false
## save the workbook
$ExcelWorkBook.Save()
## quit the workbook
$Excel.Quit()
## close all object references
Release-Ref($ExcelWorkSheet)
Release-Ref($ExcelWorkBook)
Release-Ref($Excel)
# Move-Item c:\scripts\test.zip c:\test -force
Read-Host -Prompt "Press Enter to exit"
你有没有尝试重新启动,只是为了确保你没有卡在内存中的东西卡住了吗?该代码看起来应该做你想做的事情。 – TheMadTechnician 2014-08-29 20:58:43
Release-Ref函数不能正确解析,我想你错过了一个右括号或括号。 – 2014-08-30 01:32:02