Need some help with below tasks:
1. Email Report (csv) a list of VMs - includes parameters CpuHotAddEnabled, MemoryHotAddEnabled, MemoryReservationLockedToMax, VMHost, VMFolderPath, IP Address, OS
2. Exclude Virtual Machines in specific Folders (Parent and it's child folders including) - Folder type VM
Below is what I have for now, but don't understand how to go via Folder and exclude specific.
#Initialize PowerCLI
Import-Module vmware.powercli
# Farm Login
$vCUser = 'username'
$vCPass = 'password'
# LIST OF FARM
$vCenterIP = "vCenter-1 IP"
foreach ($IPAddress in $vCenterIP){
# Connessione a vCenter
Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443
}
#Variables
$Date = get-date
$Datefile = ( get-date ).ToString('yyyy-MM-dd-hhmmss')
$ErrorActionPreference = "SilentlyContinue"
# Variable to change
$CreateCSV= "yes"
$GridView = "no"
$SendEmail = "yes"
$FileCSV = New-Item -type file "C:\Report\VMwareHotAdd_$datefile.csv"
Write-Host "Gathering VM statistics"
$report = @()
foreach($vm in Get-View -ViewType Virtualmachine) {
$vms = "" | Select-Object VMName,VMHost,VMState,CPUHotAdd,MemoryHotAdd,MemoryReservationLockedToMax,Hostname,IPAddress,OS
$vms.VMName = $vm.Name
$vms.VMState = $vm.summary.runtime.powerState
$vms.OS = $vm.Config.GuestFullName
$vms.Hostname = $vm.guest.hostname
$vms.IPAddress = $vm.Guest.Net.IpAddress -join ' , '
$vms.VMHost = Get-View -Id $vm.Runtime.Host -property Name | select -ExpandProperty Name
$vms.CPUHotAdd = $vm.Config.CpuHotAddEnabled
$vms.MemoryHotAdd = $vm.Config.MemoryHotAddEnabled
$vms.MemoryReservationLockedToMax = $vm.Config.MemoryReservationLockedToMax
$Report += $vms
}
#Output
if ($GridView -eq "yes") {
$report | Out-GridView }
if ($CreateCSV -eq "yes") {
$report | Export-Csv $FileCSV -NoTypeInformation }
#file CSV
$filename = "C:\Report\VMwareHotAdd_$datefile.csv"
$smtpServer = “email”
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($filename)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "email"
$msg.To.Add("email")
$msg.Subject = "Weekly Report - / VMware Virtual Machine Hot Add CSV - $Date"
$msg.Body = "Please find attached Weekly report of Virtual Machine Hot Add Status"
$msg.Attachments.Add($att)
$smtp.Send($msg)
#Disconnect session from VC
Disconnect-VIserver -Confirm:$false
Thanks a lot Luc, you are awesome.
Cheers