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
Try like this
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
Try like this
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
Thanks Lucd.
Your script is Working as expected
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
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
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
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
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
For SamAccountName, Department name is getting Populated.
For Owner Field with Display Name , Department name is not getting Populated.
Try changing it to
$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
Still DisplayName User Department name is not getting populated
Small change, try like this
$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
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.
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
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.
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
The Display Name user accounts doesn't exist in AD and that is the reason data is not populated for it.
Thanks Lucd.