########################## #### VMDETAILS.PS1 #### ########################## # Usage: # Get-VMDetails -VMHost $VMHost -Template $Template -HardwareProfile $HardwareProfile -HDDPart $HDDPart -Network $Network -ExcludedHDD $ExcludedHDD -ExcludedNetwork $ExcludedNetwork # Params: # $Template = Virtual Machine Template for Deploy # $HardwareProfile = Hardware Profile for Template # $VMHost = One of your Hyper-V Servers' Name in VMHosts (like hyperv02 or Auto) # $HDDPart = Hard Disk Partition (like C:, D:, E: or Auto) # Example: Get-VMDetails -VMHost hyperv02 -Template W2K8R2STDT1 -HardwareProfile TEMP2GB -HDDPart Auto -Network Auto -ExcludedHDD "C:" -ExcludedNetwork "Backup Network" # Gets Template and Hardware Details function Get-VMDetails { param ($VMHost, $Template, $HardwareProfile, $HDDPart, $Network, $ExcludedHDD, $ExcludedNetwork) $Temp = Get-Template | Where { $_.Name -eq "$Template" } $OperatingSystem = $Temp.OperatingSystem.Name $SysprepScript = $Temp.SysprepScript.SharePath $Hardware = Get-HardwareProfile | Where { $_.Name -eq "$HardwareProfile" } $CpuType = $Hardware.CPUType.Name If ($VMHost -eq "Auto") { $BestServer = Get-VMHost * | Select-Object -Property Name,AvailableMemory | Sort-object AvailableMemory | Select-Object -Last 1 $VMHostName = $BestServer.Name $HDDPart = $HDDPart If ($HDDPart -eq "Auto") { $BestDisk = (Get-VMHost $VMHostName).DiskVolumes Foreach ($i in $BestDisk) { If ($i -like "$ExcludedHDD*") { Write-Host Disk $ExcludedHDD is excluded. } Else { $DiskStatus = (Get-VMHost $VMHostName | Get-VM | Get-VirtualHardDisk | sort-object Location | Select-Object -Property Location | Where-Object {$_.Location -like "$i*"}).Count [array]$BestDisk += @("$DiskStatus $i") } } $HDDPart = ($BestDisk | Sort-object | Select-Object -First 1).Split()[1].SubString(0,2) } } Else { $VMHostFQDN = Get-VMHost | Where { $_.Name -like "$VMHost*" } $VMHostName = $VMHostFQDN.Name If ($HDDPart -eq "Auto") { $BestDisk = (Get-VMHost $VMHostName).DiskVolumes Foreach ($i in $BestDisk) { If ($i -like "$ExcludedHDD*") { Write-Host Disk $ExcludedHDD is excluded. } Else { If ($i.VolumeLabel -like "*Reserved") { Write-Host Disk $i is excluded. } Else { $DiskStatus = (Get-VMHost $VMHostName | Get-VM | Get-VirtualHardDisk | sort-object Location | Select-Object -Property Location | Where-Object {$_.Location -like "$i*"}).Count [array]$BestDisk += @("$DiskStatus $i") } } } $HDDPart = ($BestDisk | Sort-object | Select-Object -First 1).Split()[1].SubString(0,2) } } If ($Network -eq "Auto") { $Networks = Get-VMHost $VMHostName | Get-VirtualNetwork If ($Networks.Count -gt "1") { Foreach($i in $Networks) { If($i.Name -notlike "Internet*") { If($i.Locations -eq "Internal Network") { $i.Locations | Out-Null } Else { If($i.Name -eq "$ExcludedNetwork") { $i.Name | Out-Null } Else { $NetworkName = $i.Name } } } Else { $NetworkName = $i.Name } } } Else { $NetworkName = (Get-VMHost $VMHostName | Get-VirtualNetwork).Name } } Else { $NetworkName = $Network } $Results = New-Object Psobject $Results | Add-Member Noteproperty OperatingSystem $OperatingSystem $Results | Add-Member Noteproperty SysprepScript $SysprepScript $Results | Add-Member Noteproperty CpuType $CpuType $Results | Add-Member Noteproperty VMHostName $VMHostName $Results | Add-Member Noteproperty HDDPart $HDDPart $Results | Add-Member Noteproperty NetworkName $NetworkName Write-Output $Results }