Powershell script schedule based on a csv file timing

Azure-learning 56 Reputation points
2022-07-13T18:33:27.633+00:00

I have to schedule azure powershell script based on input csv file .

file.csv contents are--
VMName, backupTiming

I need to run my script 2 hours before the timing mentioned in the file.csv(backupTiming).

Could you please suggest on the recommended ways.

@Rich Matheisen @soumi-MSFT @Ian Xue (Shanghai Wicresoft Co., Ltd.) Could you please suggest and help.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,462 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Aung Zaw Min Thwin 306 Reputation points
    2022-07-14T08:08:44.987+00:00

    Basically you can use (Get-Date '7:30').AddHours(-2)

    pls check the below sample code. Haven't tested yet :)

    $csvData = @'  
    VMName, backupTiming-EST  
    machine1-poc,07:30  
    '@ | ConvertFrom-Csv  
      
    $csvData | ForEach-Object {  
        $ScheduledAt = (Get-Date $_.'backupTiming-EST').AddHours(-2)  
        $Trigger = New-ScheduledTaskTrigger -Once -At $ScheduledAt  
          
        $action = New-ScheduledTaskAction -Execute "Your script" -Argument 'Your args'  
        $principal = New-ScheduledTaskPrincipal -UserId 'DOMAIN\user' -RunLevel Highest  
        $settings = New-ScheduledTaskSettingsSet  
        $task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger -Settings $settings  
        Register-ScheduledTask -TaskName ($_.VMName + '_' + $ScheduledAt.ToString('s') ) -InputObject $task  
    }  
      
    
    0 comments No comments