none
Scheduled Task with PowerShell

    Question

  • I am looking to create a PowerShell script that will setup and run a scheduled task for BGInfo as the system user. I thought the script was working but when I run it on my computer a scheduled Task isn't created. Am I doing something wrong? I'm hoping someone more proficient at PowerShell can point out my mistakes. 

    #Files to be copied from network drive.
    $files = @()
        $files += "\\morbackup02\software\IT Tools and Fixes\BGinfo\BGinfo64.exe"
        $files += "\\morbackup02\software\IT Tools and Fixes\BGinfo\mor_config.bgi"
    
    #Local folder/files to check for.
    $localfolder = "C:\BGinfo"
    $localfile = @()
        $localfile += "C:\bginfo\bginfo64.exe"
        $localfile += "C:\bginfo\mor_config.bgi"
    
    #Check for BGinfo folder and files. If they don't exist, make them.
    if (-not (test-path $localfolder -pathtype container)) {md C:\BGinfo}
    if (-not (test-path $localfile -pathtype leaf)) {copy $files C:\BGinfo}
    
    #Scheduled task properties.
    $taskname = 'BGinfo'
    $bginfo = 'C:\bginfo\bginfo64.exe'
    $argument = 'C:\bginfo\mor_config.bgi /timer:0 /all /nolicprompt'
    $description = "Display PC info on desktop background."
    
    #Check for BGinfo task and remove it.
    Get-ScheduledTask -TaskName $taskname -ErrorAction SilentlyContinue |
    Unregister-ScheduledTask -Confirm:$false
    
    #Create new BGinfo task to run at logon of any user and run every hour for 1 day. 
    $action = New-ScheduledTaskAction -Execute $bginfo -Argument $argument
    $trigger = New-ScheduledTaskTrigger -AtLogOn
    $task = Register-ScheduledTask -TaskName $taskname -Description $description -Trigger $trigger -Action $action
    $task.Triggers.Repetition.Duration = "P1D"
    $task.Triggers.Repetition.Interval = "PT1H"
    $task | Set-ScheduledTask -User "NT AUTHORITY\SYSTEM"
    
    #Run the task.
    Start-ScheduledTask -TaskName $taskname

    Wednesday, December 5, 2018 8:09 PM

All replies

  • You are setting it to run once at logon then registering the task.  After it is registered you are trying to change it.  That won't work. 

    Set the trigger parameters before registering the task.


    \_(ツ)_/

    Wednesday, December 5, 2018 8:54 PM
  • It's easier to set up using a GPO rather than trying to script using a scheduled task.

    -- Bill Stewart [Bill_Stewart]

    Thursday, December 6, 2018 3:54 PM
    Moderator