VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

scheduling_powershell script_task scheduler

Hi Luc,

I am on windows 2008 r2 server and wants to create schedule task to run this report automatically at one particular time of the day .

iam running using ISE .but for some reasons its not working .

pastedImage_2.png

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To create the scheduled task from a script, you do something like this.

If you need the scheduled task to run at the highest level (RunLevel), you will need to run the script from an elevated PS session.

#Requires -Modules ScheduledTasks


$user = 'domain\user'

$pswd = 'VMware1!'


$sAction = @{

   Execute = 'Powershell.exe'

   Argument = '-NoProfile -WindowStyle Hidden -File C:\Scripts\hello.ps1'

}

$action = New-ScheduledTaskAction @sAction


$sTrigger = @{

   Daily = $true

   At = '9am'

}

$trigger = New-ScheduledTaskTrigger @sTrigger


$sTask = @{

   Action = $action

   Trigger = $trigger

   User = $user

   Password = $pswd

   TaskName = 'Test Task'

   Description = 'Testing a scheduled task'

   RunLevel = 'Highest'

}

$task = Register-ScheduledTask @sTask

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

18 Replies
LucD
Leadership
Leadership
Jump to solution

I would need some more information.
What is the script doing?
How did you define the scheduled task?

Command, user...


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sjesse
Leadership
Leadership
Jump to solution

You don't schedule ise, that's the editor, you schedule the powershell.exe, and you put the location of the script in the arguments part

pastedImage_0.png

pastedImage_1.png

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

script is doing healthcheck for vsphere environment .

pastedImage_0.png

pastedImage_2.png
pastedImage_3.png
Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i attached scren shot.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The script will only run when the user is logged on (as you defined in the task).
Is the script not running, producing errors, hanging...?

How do you pass the credentials to connect to the vSphere server?

Hardcoded? Or via a CredentialStoreItem entry?

Note that the latter needs to be created by the same user as the account running the script.

Perhaps add a Start-Transcript to get an idea of what is going on.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

i wanted to check thats why i have given that it shud run when user logs in .

script runs fine when i run manullay

password is hardcoded as per the last thread .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean the script in the ISe with "manual"?

What happens when you right-click the entry in the Task Scheduler and select Run?

What does the History tab show when you select the task?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

only thing which is not provided in health chck script is

C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts> .\Initialize-PowerCLIEnvironment.ps1

as powercli commnads are not loaded in powershell .

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

can you give me powershell commands to schedule this .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you still using an old PowerCLI version?

That init script is not needed in newer versions.

But you didn't answer what actually goes wrong.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Schedule what?
You mean creating the scheduled task via a script?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To create the scheduled task from a script, you do something like this.

If you need the scheduled task to run at the highest level (RunLevel), you will need to run the script from an elevated PS session.

#Requires -Modules ScheduledTasks


$user = 'domain\user'

$pswd = 'VMware1!'


$sAction = @{

   Execute = 'Powershell.exe'

   Argument = '-NoProfile -WindowStyle Hidden -File C:\Scripts\hello.ps1'

}

$action = New-ScheduledTaskAction @sAction


$sTrigger = @{

   Daily = $true

   At = '9am'

}

$trigger = New-ScheduledTaskTrigger @sTrigger


$sTask = @{

   Action = $action

   Trigger = $trigger

   User = $user

   Password = $pswd

   TaskName = 'Test Task'

   Description = 'Testing a scheduled task'

   RunLevel = 'Highest'

}

$task = Register-ScheduledTask @sTask

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks ,

I am checking it .

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

thnaks Luc .iam able to schedule task with this code .

can you please tell me if i can sort that event when task started using get-eventlog command .

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you mean the results coming out from Get-EventLog?

If yes, you can indeed sort the objects.

For example

Get-EventLog -LogName Application |

Sort-Object -Property TimeGenerated


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

lets say powershell script was run @ 14:30 PM using scheduled task.

once this is completed ,am i supposed to get entry in below ? or it will not be captured as it runs in background.

get-eventlog -logname application

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure what you mean by "...in below".

The easiest way is to redirect your stdin and stdout for the script.

It only requires a small change.
But watch out, this will not capture output from Write-Host, only stdout & stderr!

$sAction = @{

   Execute = 'Powershell.exe'

   Argument = '-NoProfile -WindowStyle Hidden -Command C:\Scripts\hello.ps1 2>&1 > c:\Output\script.log'

}

$action = New-ScheduledTaskAction @sAction


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks Luc .i will ask my query in next thread .

Reply
0 Kudos