1

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.

6
  • Check out jobs - > learn.microsoft.com/en-us/powershell/module/… Commented Jun 22, 2019 at 9:50
  • 2
    What's the reason Task Scheduler isn't used? Its very purpose is to schedule runs at specific times, so not using it sounds weird. Commented Jun 22, 2019 at 12:37
  • powershell is VERY BAD at running for long periods of time. [grin] really quite bad in that it sometimes will crash, hang, or just vanish if left running for "long enuf". it is simply not designed for what you are describing. so ... use what IS designed for such - task scheduler is one such thing. Commented Jun 22, 2019 at 14:05
  • A reason I looked for this solution: I am looking for a secure method to run a task automatically while not logged in. Storing Password, not so much a good idea. Commented Jun 17, 2021 at 21:56
  • 1
    @BendertheGreatest - yep, there are ways around it ... but they all have gotchas. it appears to be better to bite the bullet and switch to using c# when you find that you need ling-running code. Commented Sep 15, 2021 at 13:12

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.