2016-12-12 259 views
0

我想創建一個Windows計劃任務,使用PowerShell在任何用戶的登錄時運行.exe,然後每分鐘重新運行它並讓它每分鐘運行一次。PowerShell計劃任務

$computer = $env:ComputerName 
$taskName = "MyTask" 
$L = New-ScheduledTaskAction –Execute "PowerShell.exe" -Argument "$dropboxPath\request\Scripts\File_Permissions.ps1" 
$T = New-ScheduledTaskTrigger -AtLogOn 
$P = New-ScheduledTaskPrincipal -UserId "$env:ComputerName\$adminname" -LogonType Password -RunLevel Highest 
$S = New-ScheduledTaskSettingsSet 
Register-ScheduledTask -TaskName $taskName -Action $L -Trigger $T -User $adminname -Password $adminpassword -Settings $S 
$T.RepetitionInterval = (New-TimeSpan -Minutes 1) 
$T.RepetitionDuration = ([timespan]::MaxValue) 
Set-ScheduledTask $taskName -Trigger $T 

我的腳本給出了一個錯誤,它不喜歡RepetitionInterval和RepetitionDuration。

在互聯網上尋找年齡,找不到這項工作。這裏的另一個嘗試也失敗:

$computer = $env:ComputerName 
$L = New-ScheduledTaskAction –Execute "PowerShell.exe" -Argument "$dropboxPath\request\Scripts\File_Permissions.ps1" 
$T = @() 
$T += New-ScheduledTaskTrigger -AtLogOn -User * 
$T += New-ScheduledTaskTrigger -AtLogOn -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration ([timespan]::MaxValue) 
$P = New-ScheduledTaskPrincipal -UserId "$env:ComputerName\$adminname" -LogonType Password -RunLevel Highest 
$S = New-ScheduledTaskSettingsSet 
Register-ScheduledTask -TaskName "MyTask" -Action $L -Trigger $T -User $adminname -Password $adminpassword -Settings $S 
+0

請後確切的錯誤信息 –

回答

0

我認爲你應該使用這樣的:

Register-ScheduledTask -TaskName $taskName -Action $L -Trigger $T -User $adminname -Password $adminpassword -Settings $S 
$task = get-scheduledtask -TaskName $taskName 
$task.Triggers.repetition.Duration = 'PT60M' 
$task.Triggers.repetition.Interval = 'PT01M' 
$task | Set-ScheduledTask 

here拍攝。

0

好吧,我已經採取了上述來自其他用戶(4c74356b41(感謝)),並改變了它周圍一點,我現在有這個工作作爲最後:

$computer = $env:ComputerName 
$taskName = "MyTask" 
$L = New-ScheduledTaskAction –Execute "PowerShell.exe" -Argument "c:\request\Scripts\File_Permissions.ps1" 
$T = New-ScheduledTaskTrigger -AtLogOn 
$P = New-ScheduledTaskPrincipal -UserId "$env:ComputerName\$adminname" -LogonType Password -RunLevel Highest 
$S = New-ScheduledTaskSettingsSet 
Register-ScheduledTask -TaskName $taskName -Action $L -Trigger $T -User $adminname -Password $adminpassword -Settings $S 
$task = get-scheduledtask -TaskName $taskName 
$task.Triggers.repetition.Interval = 'PT01M' 
$task.Triggers.repetition.Duration = 'P1D' 
$task | Set-ScheduledTask -User $adminname -Password $adminpassword