PowerCLI

 View Only
Expand all | Collapse all

scheduling_powershell script_task scheduler

  • 1.  scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 01:12 PM

    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 .



  • 2.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 01:51 PM

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

    Command, user...



  • 3.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 02:50 PM

    script is doing healthcheck for vsphere environment .



  • 4.  RE: scheduling_powershell script_task scheduler
    Best Answer

    Posted Aug 13, 2019 05:08 PM

    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.



  • 5.  RE: scheduling_powershell script_task scheduler

    Posted Aug 14, 2019 09:38 AM

    Thanks ,

    I am checking it .



  • 6.  RE: scheduling_powershell script_task scheduler

    Posted Aug 14, 2019 11:56 AM

    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 .



  • 7.  RE: scheduling_powershell script_task scheduler

    Posted Aug 14, 2019 12:04 PM

    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



  • 8.  RE: scheduling_powershell script_task scheduler

    Posted Aug 14, 2019 01:29 PM

    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



  • 9.  RE: scheduling_powershell script_task scheduler

    Posted Aug 14, 2019 02:19 PM

    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



  • 10.  RE: scheduling_powershell script_task scheduler

    Posted Aug 14, 2019 03:45 PM

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



  • 11.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 02:00 PM

    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



  • 12.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 02:51 PM

    i attached scren shot.



  • 13.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 02:54 PM

    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.



  • 14.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 03:44 PM

    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 .



  • 15.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 03:50 PM

    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?



  • 16.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 04:14 PM

    can you give me powershell commands to schedule this .



  • 17.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 04:26 PM

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



  • 18.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 03:53 PM

    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 .



  • 19.  RE: scheduling_powershell script_task scheduler

    Posted Aug 13, 2019 04:16 PM

    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.