1 2 Previous Next 26 Replies Latest reply on Apr 9, 2020 12:36 PM by bikashyadav

    delete/move file on datastore

    kevinjj Enthusiast

      Hello guys, is it possible to get a report of those events using script?

      Thanks much.

        • 1. Re: delete/move file on datastore
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Sure is, try the following script

          $tgtEvents = "DatastoreFileDeletedEvent","DatastoreFileMovedEvent"
          $report = @()
          
          $events = Get-VIEvent -Start (Get-Date).AddHours(-1)
          $events | where {$tgtEvents -contains $_.GetType().Name} | %{
               $row = "" | Select FileName,Datastore,UserName,Time,Message
               $row.Filename = $_.TargetFile
               $row.Datastore = $_.Datastore.Name
               $row.UserName = $_.UserName
               $row.Time = $_.CreatedTime
               $row.Message = $_.FullFormattedMessage
               $report += $row
          }
          $report
          

          You can change the -Start parameter to monitor further back in the past.

           

           

          ____________

          Blog: LucD notes

          Twitter: lucd22

          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
          1 person found this helpful
          • 2. Re: delete/move file on datastore
            kevinjj Enthusiast

            Thanks Luc for the quick reply. Will this script get failed operation? or can status be shown in the report?

            • 3. Re: delete/move file on datastore
              kevinjj Enthusiast

              not sure what type of move is able to be tracked. only the ones using datastore browser? only files? what about folders?

              • 4. Re: delete/move file on datastore
                LucD Guru
                User ModeratorsvExpertCommunity Warriors

                The problem is that the event gets written away before the action is completed.

                So the Result field is still blank and the State field says "queued".

                 

                You can find this result field as follows, but unfortunately it will be blank (at least it was in my test setup).

                $tgtEvents = "DatastoreFileDeletedEvent","DatastoreFileMovedEvent"
                $report = @()
                
                $events = Get-VIEvent -Start (Get-Date).AddHours(-1)
                $events | where {$tgtEvents -contains $_.GetType().Name} | %{
                     $event = $_
                     $task = $events | where {$_.GetType().Name -eq "TaskEvent" -and $_.ChainId -eq $event.ChainId}
                     $row = "" | Select FileName,Datastore,UserName,Time,Message,Result
                     $row.Filename = $_.TargetFile
                     $row.Datastore = $_.Datastore.Name
                     $row.UserName = $_.UserName
                     $row.Time = $_.CreatedTime
                     $row.Message = $_.FullFormattedMessage
                     $row.Result = $task.Info.Result
                     $report += $row
                }
                $report
                

                 

                 

                 

                 

                 

                ____________

                Blog: LucD notes

                Twitter: lucd22

                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                • 5. Re: delete/move file on datastore
                  LucD Guru
                  vExpertUser ModeratorsCommunity Warriors

                  These events track files and folders.

                  If the delete or move is not done through the DeleteDatastoreFile_Task or MoveDatastoreFile_Task method then there won't be an event I'm afraid.

                  The Datastore browser uses these methods as well in the background.

                   

                   

                   

                   

                  ____________

                  Blog: LucD notes

                  Twitter: lucd22

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: delete/move file on datastore
                    kevinjj Enthusiast

                    just deleted a *.log file on datastore by using datastore browser, but the sript returned nothing. looks like the operation wasn't captured?

                    • 7. Re: delete/move file on datastore
                      LucD Guru
                      vExpertUser ModeratorsCommunity Warriors

                      Just did the same test and I can see the log file deleted event.

                       

                       

                      Are you connected to a vCenter ? Or directly to the ESX(i) host ?

                       

                       

                      ____________

                      Blog: LucD notes

                      Twitter: lucd22

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: delete/move file on datastore
                        kevinjj Enthusiast

                        connected to a vcenter server.

                        • 9. Re: delete/move file on datastore
                          LucD Guru
                          Community WarriorsvExpertUser Moderators

                          Do you see the delete file under the Tasks & Events tab in vCenter ?

                          If yes, then there must be an event.

                           

                           

                           

                           

                          ____________

                          Blog: LucD notes

                          Twitter: lucd22

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: delete/move file on datastore
                            kevinjj Enthusiast

                            I do see the events under  the Tasks & Events tab, but the script doesn't detect them.

                            • 11. Re: delete/move file on datastore
                              kevinjj Enthusiast

                              PowerCLI Version

                              -


                              VMware vSphere PowerCLI 4.1 build 264274

                              • 12. Re: delete/move file on datastore
                                LucD Guru
                                User ModeratorsCommunity WarriorsvExpert

                                I just tried it again and I can see the event with both PowerCLU 4u1 and 4.1

                                 

                                Do you see a "DatastoreFileDeletedEvent" line when you do

                                Get-VIEvent -Start (Get-Date).AddHours(-6) | Group-Object -Property {$_.GetType().Name}
                                

                                If not then there must be something wrong.

                                Does the

                                $defaultVIserver
                                

                                variable show your vCenter ?

                                 

                                 

                                 

                                ____________

                                Blog: LucD notes

                                Twitter: lucd22

                                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 13. Re: delete/move file on datastore
                                  kevinjj Enthusiast

                                   

                                  I don't see "DatastoreFileDeletedEvent", but some other events are there.such as  DrsVmMigratedEvent, VmBeingHotMigratedEvent, Taskevent

                                   

                                   

                                  $defaultviserver returns the vcenter server that I'm connected to.

                                   

                                   

                                  • 14. Re: delete/move file on datastore
                                    LucD Guru
                                    vExpertUser ModeratorsCommunity Warriors

                                    Are you by any chance runnign this against Virtual Center 2.5 and/or ESX 3.5 ?

                                    The Datastore events are introduced with API 4.i which means vCenter 4 and ESX(i) 4.

                                     

                                     

                                     

                                     

                                    ____________

                                    Blog: LucD notes

                                    Twitter: lucd22

                                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                    1 2 Previous Next