Ok so I have a new vm report that outputs a html file that list all of the vms that were created in the last 7 days. This is for my operations team to manage backups. One thing they have asked me for is if I can include the cluster name because that is how the policies are designated by. Anyone know how to include the cluster name of a particular VM in the script below, thanks
#Logging information
$OutputFileLocation = "new_vms_7_days.log"
Start-Transcript -path $OutputFileLocation -append
#echo to screen or list gathering
write-host ""
write-host "Generating Report...."
#formatting
$head = @"
<style>
body { background-color:white;
font-family:Tahoma;
font-size:12pt; }
td, th { border:1px solid black;
border-collapse:collapse; }
th { background-color: #39516B;
color: white;
font-size:10pt;
font-variant: small-caps;
font-family: arial;
text-align: center; }
table, tr, td, th { padding: 1px; margin: 0px }
table { margin-left:25px; }
</style>
"@
$body = get-date | Select DateTime |
ConvertTo-Html -Fragment -PreContent "<h2>LastRun</h2>" |
Out-String
#Get new vm events in the last 7 days
$body = Get-VM | Get-VIEvent -Start (Get-Date).adddays(-7) | `
where {$_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent"} | `
Sort-Object -Property CreatedTime | `
select @{N=”VMname”; E={$_.Vm.Name}},
@{N=”CreatedTime”; E={$_.CreatedTime}},
@{N=”Host”; E={$_.Host.Name}},
@{N=”User”; E={$_.UserName}},
@{N=”vCPU”; E={(Get-VM $_.Vm.Name).numcpu }},
@{N=”MemoryGB”; E={[Math]::Round((Get-VM $_.Vm.Name).MemoryGB)}} |`
ConvertTo-Html -Fragment -PreContent "<h2>New VMs in last 7 days</h2>" |
Out-String
#Output to html report
ConvertTo-Html -Head $head -Title "VMs Created in last 7 days" -PostContent $body -Property VMName, CreatedTime, Host, User, vCPU, MemoryGB, 'ProvisionedSpace(GB)', 'UsedSpace(GB)' |Out-File new_vms_last_7_days.html
Stop-Transcript
Any help is much appreciated, btw this script was created from various google searches and put together, not my original work
You can use the host.parent property on the VM object.
E.g.
$body = Get-VM | Get-VIEvent -Start (Get-Date).adddays(-7) |
where {$_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent"} |
Sort-Object -Property CreatedTime | %{
$vm = Get-VM $_.Vm.Name;
$_ | select @{N=”VMname”; E={$vm.Name}},
@{N="cluster"; E={$vm.host.Parent.name}},
@{N=”CreatedTime”; E={$_.CreatedTime}},
@{N=”Host”; E={$_.Host.Name}},
@{N=”User”; E={$_.UserName}},
@{N=”vCPU”; E={$vm.numcpu }},
@{N=”MemoryGB”; E={[Math]::Round($Vm.MemoryGB)}}}
I also did a single Get-VM call which should speed things up a little as well
Edit: Or, what LucD said
Since you have the VM, you can use the same calculated property as I did in 1. Re: Accessing the cluster name of a VM to get the clustername.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can use the host.parent property on the VM object.
E.g.
$body = Get-VM | Get-VIEvent -Start (Get-Date).adddays(-7) |
where {$_.Gettype().Name -eq "VmBeingDeployedEvent" -or $_.Gettype().Name -eq "VmCreatedEvent" -or $_.Gettype().Name -eq "VmRegisteredEvent"} |
Sort-Object -Property CreatedTime | %{
$vm = Get-VM $_.Vm.Name;
$_ | select @{N=”VMname”; E={$vm.Name}},
@{N="cluster"; E={$vm.host.Parent.name}},
@{N=”CreatedTime”; E={$_.CreatedTime}},
@{N=”Host”; E={$_.Host.Name}},
@{N=”User”; E={$_.UserName}},
@{N=”vCPU”; E={$vm.numcpu }},
@{N=”MemoryGB”; E={[Math]::Round($Vm.MemoryGB)}}}
I also did a single Get-VM call which should speed things up a little as well
Edit: Or, what LucD said
Thanks guys, this really helped. Save my operators some time!! You guys rock as always