最近透過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

 

參考的相關文章:

黑暗執行緒 - 呼叫Excel的程式無法以排程方式執行
http://blog.darkthread.net/post-2013-05-15-run-excel-in-scheduled-task.aspx

Windows Server 2008 (非R2) 排程任務時好時壞?
http://www.dotblogs.com.tw/daniel07793/archive/2012/05/07/72002.aspx

 

 


 

arrow
arrow
    全站熱搜

    Nathan 發表在 痞客邦 留言(0) 人氣()