VMware Cloud Community
vkaranam
Enthusiast
Enthusiast

Re: Who Built the vm

Hello Guys,

How are you?

I am looking for a script which shows

who built the vm :

Build date & time:

Operating system details:

here is the scenario. We have an automation for building the vm's and some of the virtual machines are built by the users. I want to retrive all the vm's and want to know which was was built by user and which one was built by automation??

Thanks for the help

VK

0 Kudos
27 Replies
vkaranam
Enthusiast
Enthusiast

Any help is appreciated??

Thanks

VK

0 Kudos
LucD
Leadership
Leadership

Try something like this

$body = Get-View -ViewType VirtualMachine -Property Name,Config.Annotation | 
select @{N="VMname"; E={$_.Name}},
   
@{N="Guest OS";E={$_.Guest}},
    @{N="Notes";E={$_.config.Annotation}},
    @{N="VMCreatedTime"; E={$_.CreatedTime}},
   
@{N="VMHost"; E={$_.Host.Name}},
    @{N="VMUser"; E={$_.Vm.userName}} | Out-String
Send-MailMessage
-To lucd@lucd.info -From lucd@lucd.info -Subject "Report" -Body $body  -SmtpServer mail.lucd.info


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

0 Kudos
vkaranam
Enthusiast
Enthusiast

Hello Lucd,

thanks for the reply. I ran the below and it is generating the reports. But it is pulling all the vm's present in the datastore. So i tried a diferent script as shown below. When i use body in that script it is throwing this error. I just need the reprot for a week.

$body = Get-VIEvent -Start (Get-Date).adddays(-7) |
where {$_.gettype().Name -eq "VmCreatedEvent"} |
select @{N="VMname"; E={$_.Vm.Name}},
    @{N="CreatedTime"; E={$_.CreatedTime}},
    @{N="Host"; E={$_.Host.Name}},
    @{N="User"; E={$_.UserName}} | Out-String
Send-MailMessage -To abc@abc.com -From VMBuild@abc.com -Subject "Report" -Body $body  -SmtpServer mail.abc.com 

Error:

Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.

At line:7 char:97

+ Send-MailMessage -To abc@abc.com -From VMBuild@abc.com -Subject "Report" -Body <<<<  $body  -SmtpServer mail.abc.com

    + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage

Thanks

VK

0 Kudos
LucD
Leadership
Leadership

It looks as if no events were found for the last week, and hence $body is equal to $null.

You could use a test before the Send-MailMessage and assign some text if there were no events

If(!$body){

   $body = "No events found"

}


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

0 Kudos
vkaranam
Enthusiast
Enthusiast

Thanks LucD!,

you are right i checked that. it is throwing that error when there are no events. I want to find in which container , folder the vm has been created. what is the arrtibute to find the folder.

@{N="VMFolder"; E={$_.Folder}}

Please correct me if i am wrong??

Thanks

VK

0 Kudos
LucD
Leadership
Leadership

I'm afraid the folder information is not present in the event.

You can get the folder where the VM currently is located, but that doesn't guarantee that the VM was created in the folder.


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

0 Kudos
vkaranam
Enthusiast
Enthusiast

hey LucD,

is there any way i can include some vm folder information script in the above script and get those details. i had this which pulls the details but how to make the integration working??

Connect-VIServer MYVISERVER

$VMFolder = @()
Foreach ($VM in (Get-VM |Get-View)){
$Details = “” |Select-Object VM,Path
$Folder = ((($VM.Summary.Config.VmPathName).Split(‘]‘)[1]).Split(‘/‘))[0].TrimStart(‘ ‘)
$Path = ($VM.Summary.Config.VmPathName).Split(‘/‘)[0]
If ($VM.Name-ne $Folder){
$Details.VM= $VM.Name
$Details.Path= $Path
$VMFolder += $Details
}
}
$VMFolder |Export-Csv -NoTypeInformation C:\temp\Mismatch.csv

Thanks

VK

0 Kudos
LucD
Leadership
Leadership

That gets the folder of the VM on the datastore, not the blue folder.

Is that what you want to show ?

Note that the VM might have been svMotioned since it was created, so that folder will not necessarily display the folder where the VM was created.


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

0 Kudos