My script basically runs on specific times during a day. Accesses the server and downloads data.
So far I have tried playing around with start-sleep command and timespan command but I am new to this.
start-sleep ((get-date "03:04 pm") - (get-date)).TotalSeconds;
$filename = (get-date -format "hh_mm_dd_MM_yyyy") + "AU" + ".csv"
$xacontroller = "abc.contoso.com"
$xasessions = Get-BrokerSession -AdminAddress $xacontroller -
MaxRecordCount 100000 | Export-Csv -Path "C:\Temp\Data\$filename"
start-sleep ((get-date "03:06 pm") - (get-date)).TotalSeconds;
$filename = (get-date -format "hh_mm_dd_MM_yyyy") + "SG" + ".csv"
$xacontroller = "def.contoso.com"
$xasessions = Get-BrokerSession -AdminAddress $xacontroller -
MaxRecordCount 100000 | Export-Csv -Path "C:\Temp\Data\$filename"
Logic : sleep till 3:04 PM and run the first block. sleep till 3:06 PM and run the second block.
What I am trying to achieve : Sleep after 3:06 PM till next day 3:04 PM
Expectation : This script needs to run everyday automatically without using task scheduler. I am using powershell 3.
Any ideas are appreciated. Thank you.
If you want to run the script at specific time, you should have something looking for this time to come (you can call it event handler).
In WMI for example, event handler is a something waiting for specific event to happen (e.g: startup, when external volume is plugged...)
In task scheduler, the events can be system startup, user login, daily at specific time (which is what you want)...
I don't think it is a good practice to keep your powershell script running all the day to perform single task at specific time, instead task scheduler is available.
Save your script in a location and use the following script to register new scheduled task at specific time to run it.
$ScriptPath = "$home\Desktop\script.ps1"
$Trigger= New-ScheduledTaskTrigger -Daily -At 03:04pm
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-executionpolicy bypass -noprofile -file $ScriptPath"
Register-ScheduledTask -TaskName "MyTask" -Trigger $Trigger -User $env:username -Action $Action