透過Windows工作排程於背景執行時 - 執行Powershell呼叫Excel程式失敗
最近透過Powershell來取得【指定的Windows主機】的硬碟使用狀況,然後將資料寫入Excel檔中。相關功能:
1. 使用【網域管理者】權限來設定每日排程
2. 將抓取的資料寫入【Excel】檔中
部分程式碼:
$excel = New-Object -COM "Excel.Application"
$workbook.Save()
$excel.Quit()
問題點:
排程選擇【只有使用者登入時才執行】:可以正常執行。(手動執行也正常)
排程選擇【不論使用者登入與否均執行】:執行失敗。
解決方案:
主要是先參考黑大寫的【暗黑執行緒-呼叫Excel的程式無法以排程方式執行】一文,
因為我的執行環境為32位元,所以於以下路徑新增【Desktop】資料夾,
執行工作排程則可正常寫入至Excel檔中。
相關路徑: C:\Windows\System32\config\systemprofile\Desktop
引用部分文章:
Excel 2007 automation on top of a Windows Server 2008 x64
https://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64
This solution is ...
・Windows 2008 Server x64
Please make this folder.
C:\Windows\SysWOW64\config\systemprofile\Desktop
・Windows 2008 Server x86
Please make this folder.
C:\Windows\System32\config\systemprofile\Desktop