Automation

 View Only
Expand all | Collapse all

List out Tasks occured last 24hrs..

VMSavvy

VMSavvyApr 28, 2011 02:00 PM

  • 1.  List out Tasks occured last 24hrs..

    Posted Apr 28, 2011 12:52 PM

    Hi all,

    I have a single line code which lists out the vcenter errors for a given time interval..

    Get-VIEvent -Start (Get-Date).AddHours(-48) -Type Error | Select-object CreatedTime, FullFormattedMessage | ConvertTo-Html -title "Errors the last 2 days" -body "<H2>Errors the last 2 days.</H2>" | Out-File -Append $filelocation

    Is there a way we can list the Tasks performed by users or triggered by the system for certain interval.. I have tried with -Type Task and Event but doesn't list anything..

    Thanks in advance!!

    VMSavvy :smileyhappy:



  • 2.  RE: List out Tasks occured last 24hrs..

    Posted Apr 28, 2011 12:58 PM

    Did you try the -Username parameter ?

    Another option is to use the Group-Object cmdlet.

    Something like this

    Get-VIEvent -Start (Get-Date).adddays(-2) | where {$_.GetType().Name -eq "TaskEvent"} | Group-Object -Property UserName


  • 3.  RE: List out Tasks occured last 24hrs..

    Posted Apr 28, 2011 01:52 PM

    Its not working LucD..

    Like Get-VIEvent do we have anything else??



  • 4.  RE: List out Tasks occured last 24hrs..

    Posted Apr 28, 2011 01:58 PM

    No, not as a PowerCLI cmdlet.

    What is not working, the Username parameter or the Group-Object solution ?

    Any error messages ?



  • 5.  RE: List out Tasks occured last 24hrs..

    Posted Apr 28, 2011 02:00 PM

    No errors.. Just a blank page output..



  • 6.  RE: List out Tasks occured last 24hrs..

    Posted Apr 28, 2011 07:27 PM

    That looks as if there were no Tasks executed during the time period.

    Do you get any events ?

    Does this return anything ?

    Get-VIEvent -Start (Get-Date).adddays(-2)

    Or perhaps your vCenter doesn't keep the Tasks and Events.

    Did you check in the vSphere client under Administration - vCenter Server Settings - Database Retention Policy.



  • 7.  RE: List out Tasks occured last 24hrs..

    Posted May 02, 2011 04:05 AM

    Yeah vCenter Server Settings doesn't have the database rentention policy set.. I've made changes now.. Will run the script after sometime and will update.. Thanks for a good point LucD.

    VMSavvy..



  • 8.  RE: List out Tasks occured last 24hrs..

    Posted May 02, 2011 01:59 PM
      |   view attached

    Hi Luc,

    Sorry for the delay.. This is what I got after enabling the database rentention policy.. Pls help!!

    VMSavvy..



  • 9.  RE: List out Tasks occured last 24hrs..

    Posted May 02, 2011 05:11 PM

    It looks as if you are retrieving the tasks now.

    Problem seems to be that you are trying to display an array/collection instead of the of the individual elements in the array/collection.

    You can do this with a loop and construct the html table in there.

    Have a look at the Simple HTML snapshot report with snapshot creator thread. In the snapshot-html-mail.ps1 script I use a loop through the snapshot array and construct a html table

    foreach($snapshot in $mySnaps){
        $htmlReport = $htmlReport +
        "<tr><td class='text'>" + $snapshot.Name + "</td>" +
        "<td class='text'>" + $snapshot.SizeMB + "</td>" +
        "<td class='text'>"+ $snapshot.vm.Name + "</td>" +
        "<td class='text'>" + $snapshot.PowerState + "</td>" +
        "<td class='text'>" + $snapshot.Created + "</td>" +
        "<td class='text'>" + $snapshot.Creator + "</td></tr>"
    }

    You should do something similar with your array/collection.

    Perhaps attach the script that you are using to produce the html page. I can have a look.



  • 10.  RE: List out Tasks occured last 24hrs..

    Posted May 03, 2011 04:29 AM
      |   view attached

    Hi Luc,

    Have attached the script that I'm using..

    Not sure if I can tag this to the thread... can I include a list of 4 vcenters in this script and run it so I can get reports delivered for each of them separately.. this is just to avoid having 4 different scripts and 4 scheduled tasks on the script server which will in turn reduce the time and load on our script server..

    Thanks in advance!!

    Attachment(s)

    ps1
    script.ps1   4 KB 1 version


  • 11.  RE: List out Tasks occured last 24hrs..
    Best Answer

    Posted May 03, 2011 11:16 AM
      |   view attached

    Nice script.

    The attached script will do all vCenters in one run.

    Attachment(s)

    ps1
    multi-vcservers.ps1   4 KB 1 version


  • 12.  RE: List out Tasks occured last 24hrs..

    Posted May 04, 2011 05:31 AM

    Thanks for adding the vcenters part..

    did u get to see if there is a way for listing out tasks/events throught this script?

    VMSavvy..



  • 13.  RE: List out Tasks occured last 24hrs..

    Posted May 04, 2011 07:20 AM

    I added these lines after the errors section.

    # Tasks during the last 24 hours

    Get-VIEvent -Start (Get-Date).AddHours(-24) | where {$_.GetType().Name -eq "TaskEvent"} | `
    Select-object CreatedTime, FullFormattedMessage | `
    ConvertTo-Html -title "vCenter tasks during the last 24 hours" `
        -body "<H2>vCenter tasks during the last day</H2>" | `
    Out-File -Append $filelocation



  • 14.  RE: List out Tasks occured last 24hrs..

    Posted May 05, 2011 05:21 AM

    Hey Luc,

    That worked and I'm almost close to what I wanted...

    Here is the sample output from your code...

    CreatedTime FullFormattedMessage
    5/5/2011 9:58:09 AMTask: Enter maintenance mode

    5/5/2011 9:57:45 AMTask: Move into resource pool

    5/5/2011 9:57:34 AMTask: Move into resource pool

    5/5/2011 9:52:13 AMTask: Rename virtual machine

    That was giving me what time task was created and what the task is.. I managed to try different headings and ended up with "UserName" to know who initiated the task.. so now the output is..

    CreatedTime FullFormattedMessage UserName
    5/5/2011 9:58:09 AMTask: Enter maintenance mode
    DCLAB\pardha
    5/5/2011 9:57:45 AMTask: Move into resource pool
    DCLAB\pardha
    5/5/2011 9:57:34 AMTask: Move into resource pool
    DCLAB\pardha
    5/5/2011 9:52:13 AMTask: Rename virtual machine

    Now, I think it would make more clear sense to know on which(host,vm,cluster) this task occured.. I'm trying with Name, ResourceName, SourceName, Type and so on... and it just returns a blank column.. any tip for me here pls..

    Thanks a lot..



  • 15.  RE: List out Tasks occured last 24hrs..

    Posted May 05, 2011 09:41 AM

    Use the Info.EntityName property.

    Like this

    Get-VIEvent -Start (Get-Date).AddHours(-24) | where {$_.GetType().Name -eq "TaskEvent"} | `
    Select-object CreatedTime, FullFormattedMessage,@{N="Entity";E={$_.Info.EntityName}},UserName | `
    ConvertTo-Html -title "vCenter tasks during the last 24 hours" `
        -body "<H2>vCenter tasks during the last day</H2>" | `
    Out-File -Append $filelocation


  • 16.  RE: List out Tasks occured last 24hrs..

    Posted May 06, 2011 03:45 AM

    Kuods Luc......All sorted with that..

    You never say "I don't know or I'm not sure" about anything in scripting do you?? :smileygrin:

    I wonder what it takes to be atleast 20% of you in scripting :smileyhappy:

    I'm a basic scripter and would like to enhance my skills in this area.. please get me any resources, links that you have will sure help me in the future.. I'm following you and your site lucd.info as well..

    A BIG THANKS for helping me..

    VMSavvy (Pardha)



  • 17.  RE: List out Tasks occured last 24hrs..

    Posted May 06, 2011 05:17 AM

    Thanks, but rest assured that I learn new stuff every day :-)

    For PowerShell I can recommend the free ebook Tobias made available.

    And there are of course numerous PowerShell books on the market.

    For PowerCLI Hal's book is a good intro.

    To learn about automating all vSphere aspects with PowerCLI you can go for our book.



  • 18.  RE: List out Tasks occured last 24hrs..

    Posted May 12, 2011 08:37 AM

    Hi LucD,

       Help needed from you.In our Environment we maintain 250GB Data store should be free(Total DS Size is 4.69 TB).But still  after helath check we found that   Data store  Free space is less than 3 %.So can we  want to find which user had created the  VM(Name) andHow many  New VMs  on which Data store along with Date,Time.

    Thanks

    Kumar Nilay



  • 19.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 12:46 PM
      |   view attached

    Hi LucD,

         When i ran the script for the Task for last 24 hrs its  shows error

    Hi

    Its shows error when i excute the script for Task for Last 24 Hr

    [vSphere PowerCLI] C:\tmp> .\script.ps1
    File C:\tmp\script.ps1 cannot be loaded. The file C:\tmp\script.ps1 is not digi
    tally signed. The script will not execute on the system. Please see "get-help a
    bout_signing" for more details..
    At line:1 char:13
    + .\script.ps1 <<<<
        + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
        + FullyQualifiedErrorId : RuntimeException

    Can you help me what need to modify.

    Attachment(s)

    ps1
    script.ps1   4 KB 1 version


  • 20.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 12:55 PM

    It looks as if the ExecutionPolicy onyour PC is set to AllSigned.

    Please check with

    Get-ExecutionPolicy

    You can try to change it with

    Set-ExecutionPolicy RemoteSigned


  • 21.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 01:45 PM

    Hi LucD,

      Its remore signed then also its shows Digital signed error

    [vSphere PowerCLI] C:\tmp> Get-ExecutionPolicy
    RemoteSigned
    [vSphere PowerCLI] C:\tmp> Set-ExecutionPolicy RemoteSigned

    Execution Policy Change
    The execution policy helps protect you from scripts that you do not trust.
    Changing the execution policy might expose you to the security risks described
    in the about_Execution_Policies help topic. Do you want to change the execution
    policy?
    [Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y
    [vSphere PowerCLI] C:\tmp> .\script.ps1
    File C:\tmp\script.ps1 cannot be loaded. The file C:\tmp\script.ps1 is not digi
    tally signed. The script will not execute on the system. Please see "get-help a
    bout_signing" for more details..
    At line:1 char:13
    + .\script.ps1 <<<<
        + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
        + FullyQualifiedErrorId : RuntimeException



  • 22.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 02:04 PM

    I suspect that you copied the script from a browser and that the origin is kept in the alternate data stream of the file.

    See Either My Scripts Don’t Run Or They All Do!



  • 23.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 03:02 PM
      |   view attached

    LucD,

    Now its working as you suggested due browser it was giving problem but now its shows error like

    Any suugestion to work fine

    [vSphere PowerCLI] C:\tmp> .\scripttask.ps1
    Add-PSSnapin : Cannot add Windows PowerShell snap-in VMware.VimAutomation.Core
    because it is already added. Verify the name of the snap-in and try again.
    At C:\tmp\scripttask.ps1:6 char:13
    + Add-PSsnapin <<<<  VMware.VimAutomation.Core
        + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Core:Stri
       ng) [Add-PSSnapin], PSArgumentException
        + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.Ad
       dPSSnapinCommand

    The term 'Initialize-VIToolkitEnvironment.ps1' is not recognized as the name of
    a cmdlet, function, script file, or operable program. Check the spelling of th
    e name, or if a path was included, verify that the path is correct and try agai
    n.
    At C:\tmp\scripttask.ps1:7 char:36
    + Initialize-VIToolkitEnvironment.ps1 <<<<
        + CategoryInfo          : ObjectNotFound: (Initialize-VIToolkitEnvironment
       .ps1:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Attachment(s)

    ps1
    scripttask.ps1   4 KB 1 version


  • 24.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 03:11 PM

    The warning about the snapin causes no harm, it just says that you tried to load the PowerCLI snapin a 2nd time.

    You're most probably located in another folder then where 'Initialize-VIToolkitEnvironment.ps1' is located.

    Try giving the complete path.



  • 25.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 03:48 PM
      |   view attached

    LucD,

       Ok Where the output  will throw.Any path ? because i am not able to find logfile under the path which you specified in the script

    $filelocation="C:\VMwareMaintenance\Daily\"

    and also its shows error

    The term 'Initialize-VIToolkitEnvironment.ps1' is not recognized as the name of
    a cmdlet, function, script file, or operable program. Check the spelling of th
    e name, or if a path was included, verify that the path is correct and try agai
    n.
    At C:\tmp\Task24.ps1:7 char:36
    + Initialize-VIToolkitEnvironment.ps1 <<<<
        + CategoryInfo          : ObjectNotFound: (Initialize-VIToolkitEnvironment
       .ps1:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Please suggest on this

    Attachment(s)

    ps1
    Task24.ps1   4 KB 1 version


  • 26.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 04:03 PM
      |   view attached

    Output got under C:\VMwareMaintenance\Daily

    Is it correct.I have attached the output

    Attachment(s)

    htm
    vc-atlk-prod 11-05-11.htm   112 KB 1 version


  • 27.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 04:29 PM

    Not sure if the output is correct.

    Are you working in a double-byte character set ?



  • 28.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 04:54 PM

    LucD,

    Sorry i am confused double byte character.I running the script from XP OS.But double byte character not using  if you mean English and other language



  • 29.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 05:10 PM

    Forget it, must be a FF problem.

    With IE the htm file opens correctly.

    It's looking good.



  • 30.  RE: List out Tasks occured last 24hrs..

    Posted May 12, 2011 07:40 AM

    Happy to see that my customized script is being used by others too... :smileygrin:

    VMSavvy..



  • 31.  RE: List out Tasks occured last 24hrs..

    Posted May 11, 2011 12:41 PM
      |   view attached

    Hi

    Its shows error when i excute the script for Task for Last 24 Hr

    [vSphere PowerCLI] C:\tmp> .\script.ps1
    File C:\tmp\script.ps1 cannot be loaded. The file C:\tmp\script.ps1 is not digi
    tally signed. The script will not execute on the system. Please see "get-help a
    bout_signing" for more details..
    At line:1 char:13
    + .\script.ps1 <<<<
        + CategoryInfo          : NotSpecified: (:) [], PSSecurityException
        + FullyQualifiedErrorId : RuntimeException

    Can you help me what need to modify.

    Attachment(s)

    ps1
    script.ps1   4 KB 1 version