#####
#
# Identify if there are any VM's with snapsots or CD's attached
#
#####

#read-host -assecurestring | convertfrom-securestring | out-file 'C:\Admin\PS\Auth\O365Account.pwd'
$password = get-content 'C:\Admin\PS\Auth\O365Account.pwd' | convertto-securestring
$O365cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "O365Account@<DomainName>.com",$password


#This stores your credentials into an xml file.  You can open the xml file, but the password is hashed.
#New-VICredentialStoreItem -Host ESXorvCenterHostname -User root -Password "Super$ecretPassword" -File C:\Admin\PS\prodvmware.xml
##

Get-Module -Name VMware* -ListAvailable | Import-Module

$User = Get-VICredentialStoreItem -Host HostName.DomainName.com -File "C:\Admin\PS\Auth\vCenter_vmware.xml"

Connect-VIServer -server $User.host -port 443 -User $User.user -Password $User.password

$LogFile = 'C:\Admin\PS\Logs\CDSSLog.txt'
$ALLVMs = 'C:\Admin\PS\HTML\ALLVMs.html'


$CDSSHead = "<style>"
$CDSSHead = $CDSSHead + "BODY{}"
$CDSSHead = $CDSSHead + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$CDSSHead = $CDSSHead + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:Orange;}"
$CDSSHead = $CDSSHead + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod;}"
$CDSSHead = $CDSSHead + "</style>"


#Identify is there is a CD mounted
#Get-VM | Where-Object { $_ | Get-CDDrive | Where-Object { $_.ConnectionState.Connected -eq 'true' } } | Select-Object Name, VMHost | ConvertTo-Html -Head $CDSSHead -body "<H2>VM's with mounted CD's</H2>" | Out-File $CDLog


#Identify if there are any snapshots
#Get-VM | Get-Snapshot | Select-Object PowerState, VM, Description, Created, SizeGB | ConvertTo-Html -Head $CDSSHead -body "<H2>VM's with Snap shots CD's</H2>"| Out-File $VSSLog

$vms = Get-VM 

$vms | Where-Object { $_ | Get-CDDrive | Where-Object { $_.ConnectionState.Connected -eq 'true' } } | Select-Object Name, VMHost | ConvertTo-Html -Head $CDSSHead -body "<H2>VM's with mounted CD's</H2>" > $ALLVMs
$vms | Get-Snapshot | Select-Object PowerState, VM, Description, Created, @{ n="SizeGB"; e={[math]::round( $_.SizeGB, 2 )}} | ConvertTo-Html -Head $CDSSHead -body "<H2>VM's with Snap shots CD's</H2>" >> $ALLVMs

$vms | Where-Object { $_.PowerState -like 'PoweredOn' } | Select-Object Name, @{ n="ProvisionedSpaceGB"; e={[math]::round( $_.ProvisionedSpaceGB, 2 )}}, @{ n="UsedSpaceGB"; e={[math]::round( $_.UsedSpaceGB, 2 )}}, MemoryGB, NumCpu,@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}}, @{N="IP Address";E={@($_.guest.IPAddress[0])}}, @{N="VM Tools Version";E={@($_.guest.ToolsVersion[0])}}, @{Name=”VMware Tools”;E={$_.ExtensionData.guest.ToolsStatus}}, VMHost | ConvertTo-Html -Head $CDSSHead -body "<H2> Powered On VM's</H2>" >> $ALLVMs

$vms | Where-Object { $_.PowerState -like 'PoweredOff' } | Select-Object Name, Host, @{ n="ProvisionedSpaceGB"; e={[math]::round( $_.ProvisionedSpaceGB, 2 )}}, @{ n="UsedSpaceGB"; e={[math]::round( $_.UsedSpaceGB, 2 )}}, MemoryGB, NumCpu,@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}}, @{N="IP Address";E={@($_.guest.IPAddress[0])}}, VMHost | ConvertTo-Html -Head $CDSSHead -body "<H2> Powered Off VM's</H2>" >> $ALLVMs


$SMTPServer = 'smtp.office365.com'
$SMTPPort = '587'
$From = 'O365Account@<DomainName>.com'
#$To = 'O365Group@<DomainName>.com'
$To = 'O365Account@<DomainName>.com'
$Subject = ('ALL VM Report')

$Body = Get-Content $ALLVMs | Out-String


Send-MailMessage -SmtpServer $SMTPServer -Port $SMTPPort -From $From -To $To -Subject $Subject -BodyAsHtml  ($Body) -Credential $O365cred -UseSsl


[System.GC]::Collect()

#Remove the snapshot
#Get-VM | Get-Snapshot | Select-Object Name | Remove-Snapshot