1 2 Previous Next 15 Replies Latest reply on Jul 23, 2019 8:37 PM by Sivaramsharmar

    VM Cloned from Past couple of days

    Sivaramsharmar Enthusiast

      Hi All,

       

      I am trying to fetch the VM Details which was created from past couple of days.

      I am getting the output, but when trying to filter only with VM name I am not getting exact output.

       

      Get-VIEvent -MaxSamples 10000 |where {$_.Gettype().Name-eq "VmCreatedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,@{N='VM Name';E={$_.FullformattedMessage.split('to')[2].split(' ')[2]}},@{N='Provisioned Type';E={"Created"}}

      Get-VIEvent -MaxSamples 10000 |where {$_.Gettype().Name-eq "VmBeingClonedEvent"} |Sort CreatedTime -Descending |Select CreatedTime, UserName,@{N='VM Name';E={$_.FullformattedMessage.split('to')[10]}},@{N='Provisioned Type';E={"Cloned"}}

       

      Expected Output:

       

      CreatedTime           UserName                VM Name        Provisioned Type
      -----------           --------                -------        ----------------
      5/15/2019 12:00:23 PM Domain\Userid VM1   Created

      5/15/2019 14:00:23 PM Domain\Userid VM2   Cloned

       

      But Output is coming as below:

      CreatedTime           UserName                VM Name        Provisioned Type
      -----------           --------                -------        ----------------
      5/15/2019 12:00:23 PM Domain\Userid xyz VM1   Created

      5/15/2019 14:00:23 PM Domain\Userid lmwsdf VM2   Cloned

        • 1. Re: VM Cloned from Past couple of days
          LucD Guru
          Community WarriorsUser ModeratorsvExpert

          Try like this

           

          Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-2) |

          where {$_ -is [VMware.Vim.VmCreatedEvent] -or $_ -is [VMware.Vim.VmClonedEvent]} |

          Sort CreatedTime -Descending |

          Select CreatedTime, UserName,

            @{N='VM Name';E={$_.Vm.Name}},

            @{N='Provisioned Type';E={$_.GetType().Name.Replace('Vm','').Replace('Event','')}}

           

          • 2. Re: VM Cloned from Past couple of days
            Sivaramsharmar Enthusiast

            Thanks Lucd.

             

            Your script is Working as expected

            • 3. Re: VM Cloned from Past couple of days
              Sivaramsharmar Enthusiast

              Hi Lucd,

               

              We just come up with one more requirement. We need to get the Cluster Name, DataStore Name , User Name & Department Name.

               

              $data = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-2) |where {$_ -is [VMware.Vim.VmCreatedEvent] -or $_ -is [VMware.Vim.VmClonedEvent]} |Sort CreatedTime -Descending |select CreatedTime, UserName,  @{N='VM Name';E={$_.Vm.Name}},@{N='Provisioned Type';E={$_.GetType().Name.Replace('Vm','').Replace('Event','')}}


              = $data | select *,@{N='Cluster';E={(get-vm $_."VM Name"|Get-Cluster).name}},@{N='Datastore';E={(Get-VM $_."VM name"|get-datastore).name}},@{N='OS';E={(get-vm $_."VM Name").guest.osfullname}},@{N='Owner';E={(get-vm $_."VM Name"|Get-Annotation -CustomAttribute Owner).value}}

               

               

              I have altered the script provided by you to fetch the Cluster, DataStore & Username.

               

              But not able to fetch the User Department name.

               

               

              Username is stored as Attribute at vCenter Level and it's a mix of both SAMAccountName & Display Name.

               

              Not sure how to get this as the last column in the same report.

               

               

              I have tried below command but not sure how to check if DisplayName exists or SAMAccountName exists on the VM Attribute.

               

               

              $data1 | select *,@{N='Department';E={(Get-ADUser -Filter "DisplayName -eq '$($_.Owner)'" -Properties *).Department}} 

               

               

              Expected Sample Output:

               

              Created Time                UserName          VM Name           Provisioned Type                Cluster       DataStore          OS           Owner                         Department

              7/15/2019                   User1              VM1                  Cloned                    Cluster1         DataSTORE1      2016           EndUser1                           XYZ

              7/14/2019                   User1              VM2                   Cloned                   Cluster2         DataSTORE2      2012           UFistname, ULastName (123)         ABC

              • 4. Re: VM Cloned from Past couple of days
                LucD Guru
                vExpertCommunity WarriorsUser Moderators

                That looks correct, and seems to work for me.
                Could it be that Owner custom attribute doesn't correspond with the DisplayName field in AD?
                Did you try the Get-ADUser on it's own with the value of an Owner?

                • 5. Re: VM Cloned from Past couple of days
                  Sivaramsharmar Enthusiast

                  We need to have a if else logic like below.

                   

                  if the Owner attribute is having SAM Account name then below command has to be executed

                   

                  $data1 | select *,@{N='Department';E={(get-aduser $_.Owner -Property *).Department}} 

                   

                   

                  Else Below command has to be executed ( As the Owner field is updated with Display Name )

                  $data1 | select *,@{N='Department';E={(Get-ADUser -Filter "DisplayName -eq '$($_.Owner)'" -Properties *).Department}} 

                   

                   

                  I am not able to incorporate in the existing script.

                   

                  Need your help on same

                  • 6. Re: VM Cloned from Past couple of days
                    LucD Guru
                    User ModeratorsCommunity WarriorsvExpert

                    Would something like this work?
                    if one method doesn't return the value, try the other method.

                    Note, I use a script-scope variable ($script:owner) to avoid having to use multiple Select-Object lines

                     

                    $data = Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-2) |

                       where {$_ -is [VMware.Vim.VmCreatedEvent] -or $_ -is [VMware.Vim.VmClonedEvent]} |

                      Sort CreatedTime -Descending |

                      select CreatedTime, UserName,

                       @{N='VM Name';E={$_.Vm.Name}},

                       @{N='Provisioned Type';E={$_.GetType().Name.Replace('Vm','').Replace('Event','')}},

                       @{N='Cluster';E={(Get-Cluster -VM $_.Vm.Name).Name}},

                       @{N='Datastore';E={(Get-Datastore -VM $_.Vm.Name).Name}},

                       @{N='OS';E={(Get-VM -Name $_.Vm.Name).Guest.OSFullname}},

                       @{N='Owner';E={

                       $script:owner = (Get-VM -Name $_.Vm.Name | Get-Annotation -CustomAttribute Owner).Value

                       $script:owner}},

                       @{N='Department';E={

                       $dept = (Get-ADUser $script:owner -Property *).Department

                       if($dept -eq ''){

                       $dept = (Get-ADUser -Filter "DisplayName -eq '$($script:owner)'" -Properties *).Department

                       }

                       $dept

                       }}

                    • 7. Re: VM Cloned from Past couple of days
                      Sivaramsharmar Enthusiast

                      For SamAccountName, Department name is getting Populated.

                      For Owner Field with Display Name , Department name is not getting Populated.

                      • 8. Re: VM Cloned from Past couple of days
                        LucD Guru
                        Community WarriorsUser ModeratorsvExpert

                        Try changing it to

                         

                           @{N='Department';E={

                           $dept = (Get-ADUser $script:owner -Property * -ErrorAction SilentlyContinue).Department

                           if($dept -eq $null){

                           $dept = (Get-ADUser -Filter "DisplayName -eq '$($script:owner)'" -Properties *).Department

                           }

                           $dept

                           }}

                        • 9. Re: VM Cloned from Past couple of days
                          Sivaramsharmar Enthusiast

                          Still DisplayName User Department name is not getting populated

                          • 10. Re: VM Cloned from Past couple of days
                            LucD Guru
                            Community WarriorsvExpertUser Moderators

                            Small change, try like this

                             

                               @{N='Department';E={

                             

                               $dept = (Get-ADUser -Filter "SamAccountName -eq '$($script:owner)'" -Property *).Department

                               if($dept -eq $null){

                               $dept = (Get-ADUser -Filter "DisplayName -eq '$($script:owner)'" -Properties *).Department

                               }

                               $dept

                               }}

                            • 11. Re: VM Cloned from Past couple of days
                              Sivaramsharmar Enthusiast

                              Thanks Lucd for the script.

                              Now seeing a strange issue.

                              Got 3 diplay names in Owner field and department name is fetched for only 1 VM.

                              For remaining 2 VMs it doesn't fetch the department name.

                              I have verified that there are no spaces in ending or starting of Owner Field.

                              In AD I can able to see the department name for those 2 display names.

                              • 12. Re: VM Cloned from Past couple of days
                                LucD Guru
                                vExpertUser ModeratorsCommunity Warriors

                                What is/are the name(s) for the VM(s) for which you see multiple values in the Owner field?

                                • 13. Re: VM Cloned from Past couple of days
                                  Sivaramsharmar Enthusiast

                                  It seems to be there is some typo in my previous post.

                                   

                                  I got 10 VM Names.

                                   

                                  Out of 10, 7 VMs are having SAMAccount Name & Department name is populating for those VMs.

                                   

                                  Out of 10, 3 VMs are having DisplayName in Owner Filed.

                                   

                                  Out of which the 1 VM which is having Display Name is having the Department Name populated.

                                   

                                  Remaining 2 VMs are also having Display Name but Department name is not populated.

                                   

                                  I have verified for those 2 VMs and there were no spaces.

                                  • 14. Re: VM Cloned from Past couple of days
                                    LucD Guru
                                    vExpertCommunity WarriorsUser Moderators

                                    What does the following return for the VMs where the Dept name is missing?

                                    Replace the <VM name> with the Displayname of those VMs.

                                     

                                    Get-ADUser -Filter "DisplayName -eq '<VM name>'"

                                    1 2 Previous Next