我的PowerShell腳本從IP攝像頭獲取快照,它運行在帶有Windows 10 IoT Core的Raspberry Pi3上。通過計劃任務腳本運行非常緩慢
當我從PowerShell運行腳本(只輸入c:\scripts\snapshot.ps1
)它運行良好。但是當我從cmd powershell -File c:\scripts\snapshot.ps1
(我需要這個用於從任務調度器運行)運行它時,它運行的時間太長,需要整個CPU內核,幾分鐘後我就殺了它。
同樣,當我從powershell運行此命令(no從cmd)。腳本將停止在Invoke-WebRequest -Uri $url -Headers $Headers -OutFile "$dir\$outFile"
。文件$outFile
已創建,但沒有內容,仍在運行並正在運行。我觀察整個腳本太慢了,當只用2 Write-Output
運行腳本時,它在2個寫入的字符串之間延遲。 我試過-MTA
和-NoProfile
和一些下一個powershell.exe
的說法,但沒有運氣。
我的腳本:
$user = 'operator'
$pass = 'password'
$url = 'http:\\url.jpg'
$dir = "c:\outputdir"
$date = Get-Date
$date = $date.ToString("yyyy-MM-dd_HH-mm-ss")
$outFile = "snapshot_$date.jpg"
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
Invoke-WebRequest -Uri $url -Headers $Headers -OutFile "$dir\$outFile"
編輯: 我的計劃任務運行:
Folder: \
HostName: pi
TaskName: \Create-snapshot
Next Run Time: 3/27/2017 3:32:00 AM
Status: Running
Logon Mode: Interactive/Background
Last Run Time: 3/27/2017 3:31:02 AM
Last Result: 267009
Author: pi\Administrator
Task To Run: C:\Data\Admin\Create-Snapshot\Create-Snapshot-Start.bat >> "c:\Data\Admin\Create-Snapshot\Log\Create-Snapshot-start 2017-03-27 2_47_25.log"
Start In: N/A
Comment: N/A
Scheduled Task State: Enabled
Idle Time: Disabled
Power Management: Stop On Battery Mode, No Start On Batteries
Run As User: Administrator
Delete Task If Not Rescheduled: Disabled
Stop Task If Runs X Hours and X Mins: 72:00:00
Schedule: Scheduling data is not available in this format.
Schedule Type: One Time Only, Minute
Start Time: 12:56:00 AM
Start Date: 3/27/2017
End Date: N/A
Days: N/A
Months: N/A
Repeat: Every: 0 Hour(s), 1 Minute(s)
Repeat: Until: Time: None
Repeat: Until: Duration: Disabled
Repeat: Stop If Still Running: Disabled
我試圖運行的任務,因爲NT AUTHORITY \ SYSTEM - 但沒有運氣。
謝謝你的時間。
Rob
你如何配置任務?你正在使用'-NoProfile'這樣的開關嗎?默認情況下,PowerShell會加載很多內容,如果不省略,它將在配置的任務中執行相同的操作。大多數使PowerShell成爲任務的例子將涵蓋這一點。 – Matt
我想要powershell -NoProfile文件「c:\ Data \ Admin \ Create-Snapshot \ Create-Snapshot.ps1」 - 從PowerShell遠程運行此命令或從計劃任務運行速度慢,當我從cmd運行它時或直接在PowerShell ISE中打開.ps1腳本並運行,沒關係.. –
這看起來類似於http://serverfault.com/questions/151824/process-runs-slower-as-a-scheduled-task-than-it -does-interactively –