Weekly schedule task using gMSA

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Sam Boutros Sam Boutros 3 weeks, 5 days ago.

  • Author
    Posts
  • #67864
    Profile photo of Biswajit
    Biswajit
    Participant

    Team,
    Mine is working fine but how can I schedule that weekly once (specific day & time). I have scheduled that script using Microsoft Group managed service account instead of normal service account due to more security.

    $DurationTimeSpan = New-TimeSpan -Hours 12 
    $DurationTimeSpanIndefinite = ([TimeSpan]::MaxValue)
    $DurationTempTest = New-TimeSpan -Days 1000
    $Argt = "-File C:\Scripts\test-services.ps1"
    $action = New-ScheduledTaskAction -Execute '%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe' -Argument $Argt
    $Trigger = New-ScheduledTaskTrigger -Once -At "07:00" -RepetitionInterval $DurationTimeSpan -RepetitionDuration $DurationTempTest
    $principle = New-ScheduledTaskPrincipal -UserId contoso\gMSA1234$ -LogonType Password
    Register-ScheduledTask myserviceadmintask -TaskPath \AD-Service -Action $action -Trigger $trigger -Principal $principle 
    
  • #67879
    Profile photo of Sam Boutros
    Sam Boutros
    Participant
    # To schedule a PowerShell script:
    
    $ScriptPath = 'C:\scripts\ping-report-v3.ps1'
    $TaskName   = 'PingReport'
    $TaskRun    = "powershell.exe -NoLogo -NonInteractive -WindowStyle Hidden -Command ""$ScriptPath""" 
    
    # Example: Hourly starting at 9 AM
    SCHTASKS.EXE /Create /S $Env:COMPUTERNAME /RU SYSTEM /TN $TaskName /TR $TaskRun /SC HOURLY /ST 09:00 /RL HIGHEST /F 
    break
    
    # Example: Weekly on Sundays at 2 AM
    SCHTASKS.EXE /Create /S $Env:COMPUTERNAME /RU SYSTEM /SC WEEKLY /D SUN /TN $TaskName /TR $TaskRun /ST 02:00 /RL HIGHEST /F 
    break
    
    # Example: Daily at 7 AM
    SCHTASKS.EXE /Create /S $Env:COMPUTERNAME /RU SYSTEM /SC DAILY /TN $TaskName /TR $TaskRun /ST 07:00 /RL HIGHEST /F 
    break
    
    # Run now:
    SCHTASKS /Run /TN "\$TaskName"
    break 
    

You must be logged in to reply to this topic.