PowerShell Run if Day and Hour

I am trying to write a PowerShell script to run a segment of code if the day and hour match:

if it is Monday 10am hour run:

if it is Monday 6pm hour run:

If it is Sunday 7pm hour run:

etc.

I have found several bulky ways of doing this but I am trying to keep things tight. Any ideas?
-TNT-Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SeanSystem EngineerCommented:
why not do a scheduled task to run the script at those times?
0
-TNT-Author Commented:
Because there are a total of about 12 tasks that need to be run and on several computers. So to schedule 12 tasks on 5 computers will take a long time, plus if we replace a computer we have to do it all over again.
0
becraigCommented:
Easy solution
        $hour= Get-Date -Format HH
        $day = Get-Date -Format dddd
 if ($day -eq "Monday" -and $hour -eq "10")
{
Execute something
}
 if ($day -eq "Sunday" -and $hour -eq "7")
{
Execute something
}

You can also write your day into a switch if this will be daily / hourly
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

SeanSystem EngineerCommented:
what you can do is get-date -format ddd to get the day of the week and get-date -format hh:mm to get the time. Then you can do an if statement to check those to run your script and then put everything in large loop with a sleep timer.
0
footechCommented:
Here's another method.  Just substitute your action for "First", "Second", etc.
switch (Get-Date)
{
    {$_.dayofweek -eq "Monday" -and $_.Hour -eq 10}
        { "First"}
    {$_.dayofweek -eq "Monday" -and $_.Hour -eq 18}
        { "Second" }
    {$_.dayofweek -eq "Sunday" -and $_.Hour -eq 19}
        { "Third" }
}

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Are the tasks the same for all machines? If so, running a PS script on one "server" doing the task scheduling might be a better approach. Or let that single machine run 12 tasks, each performing the same action for a list of machines.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.