![](/img/trans.png)
[英]Powershell script run from task scheduler unable to open word document
[英]Powershell Script Will not Run upon Open or Pass Parameter from Task Scheduler
我在任務計划程序/腳本提示中調用 PowerShell 應用程序,如下所示:
C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe
然后我在添加參數提示中調用可執行腳本,如下所示:
C:\\1hwebster\\PSISE_Scripts\\OpenExcelFile.ps1
這成功地在 PowerShell 窗口中打開了我的腳本。 但是,它不運行腳本。
我還需要將一個參數從任務調度程序傳遞給腳本,為此我嘗試了 -File 方法:
-文件 "C:\\1hwebster\\PSISE_Scripts\\OpenExcelFile.ps1" fpathfname 'C:\\1hwebster\\VBA\\DailyEconomicCalendar.xlsm'
這將 fpathfname 參數讀取為第二個文件並批評我的語法。
我也試過 -Command 方法:
-Command "& C:\\1hwebster\\PSISE_Scripts\\OpenExcelFile.ps1 -fpathfname 'C:\\1hwebster\\VBA\\DailyEconomicCalendar.xlsm'"
我已經嘗試了圍繞 -File 和 -Command 內容的所有引號和單引號迭代,但也沒有成功。 我需要輸入什么才能成功運行 PowerShell 腳本並從任務計划程序傳遞參數? 提前致謝。
Windows 10 64 位。 PowerShell 5.1
如何將參數從 Windows 計划任務傳遞到 PowerShell 腳本。
如果將此作為計划任務進行測試,請創建一個新操作並首先運行它:
taskkill /f /im conhost.exe
失敗的任務有時會留下 conhost.exe 的實例。 如果在運行腳本之前沒有殺死它,腳本可能會失敗。
添加到 powershell 腳本 %USERPROFILE%\\Desktop\\1.ps1:
Write-host "
All the arguments are: $args"
Write-host " The first argument is: " $args[0]
Write-host " The second argument is: " $args[1]
cmd /c pause
exit
構建您的計划任務操作:
如果參數 / s 中沒有空格或引號,則不必引用參數 / s。
powershell %USERPROFILE%\Desktop\1.ps1 Argument1 Argument2
如果參數 / s 中有空格和/或引號,則必須將參數 / s 單引號。
powershell %USERPROFILE%\Desktop\1.ps1 'Argument One' 'Argument Two'
powershell %USERPROFILE%\Desktop\1.ps1 '"ArgumentOne"' '"ArgumentTwo"'
將整個命令復制並粘貼到一個新操作中,Windows 10 任務計划程序會詢問您是否要在“程序/腳本”和“添加參數”之間拆分粘貼。 單擊確定。 單擊是。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.