2017-02-15 70 views
1

顯示我已經在「\」 [默認]路徑使用Windows任務計劃程序GUI中定義了一些預定的任務,但是當我在PowerShell中運行Get-ScheduledTask,它不歸還。爲什麼?計劃任務不進去,ScheduledTask結果

我試圖與Get-ScheduledTask -TaskName "MyTaskName"我的任務名稱之一,但它與

其實我已經試過https://library.octopusdeploy.com/step-template/actiontemplate-windows-scheduled-task-disable「‘TASKNAME’等於‘MyTaskName’財產沒有找到MSFT_ScheduledTask對象」來了,但它不工作,所以我試圖直接運行腳本。

UPDATE 我發現在http://www.fixitscripts.com/problems/getting-a-list-of-scheduled-tasks下面的腳本來獲取任務列表:提前

# PowerShell script to get scheduled tasks from local computer 
$schedule = new-object -com("Schedule.Service") 
$schedule.connect() 
$tasks = $schedule.getfolder("\").gettasks(0) 
$tasks | Format-Table Name , LastRunTime # -AutoSize 
IF($tasks.count -eq 0) {Write-Host 「Schedule is Empty」} 
Read-Host 

感謝您幫助。

+1

什麼在命令提示符回報呢「的schtasks /查詢/ v」?你看到你的任務? –

+0

@DavidBrabant它返回很長的信息,很難找到其中的特定任務名稱。 – mehran

回答

0

您是否嘗試過使用com對象?此代碼的工作對我來說:

# FOR A REMOTE MACHINE 
$s = 'SERVER_NAME' # update this with server name 
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect($s) 


# FOR LOCAL MACHINE 
($TaskScheduler = New-Object -ComObject Schedule.Service).Connect() 

#now we can query the schedules... 
cls;$TaskScheduler.GetFolder('\').GetTasks(0) | Select Name, State, Enabled, LastRunTime, LastTaskResult | Out-GridView 

此代碼將檢索特定任務,並啓用它:

$task = $TaskScheduler.GetFolder('\').GetTask("TASKNAME") 
$task.Enabled = $true 
+0

第一個與'。'不兼容。也沒有它。與'。'它得到了以下錯誤_Exception調用「的GetFolder」與「1」的說法(S):「只支持此操作時,您連接到服務器」 _ – mehran

+0

第二個被發現了以下錯誤:_Exception叫「GetTask」與「1」參數:「Access is denied._ – mehran

+0

抱歉,現在不在Windows機器上,請嘗試在提升的PowerShell會話中運行。另外,對於本地任務,語法需要略有不同 - 請參閱我的編輯,與最新的變化 – TechSpud