VMware Cloud Community
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

VM Cloned from Past couple of days

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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','')}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership
Jump to solution

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','')}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd.

Your script is Working as expected

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

   }}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

For SamAccountName, Department name is getting Populated.

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

   }}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Still DisplayName User Department name is not getting populated

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

   }}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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>'"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

The Display Name user accounts doesn't exist in AD and that is the reason data is not populated for it.

Thanks Lucd.

Reply
0 Kudos