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 .
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.
$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
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
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
script is doing healthcheck for vsphere environment .
i attached scren shot.
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
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 .
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
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 .
can you give me powershell commands to schedule this .
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
Schedule what?
You mean creating the scheduled task via a script?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
$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
Thanks ,
I am checking it .
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 .
Do you mean the results coming out from Get-EventLog?
If yes, you can indeed sort the objects.
For example
Sort-Object -Property TimeGenerated
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
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!
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
Thanks Luc .i will ask my query in next thread .