For the next report I wanted to have a copy of the VM state.. so I wrote this script to dump out the details i required into excel
Its not the fastest script…. I know how to improve its performance but honestly its not something I run very often so a quick and dirty solution was fine with me…
So about the Report…
VM Tab
VM,UUID,DNSName,IPAddress,ConfiguredOS,RunningOS,VMVersion,PowerState,Template,vCPUS,vMEM,ProvisionedGB,UsedSpaceGB,VMCluster,CurrentESXHost,Datastore(s),ResourcePool,FolderPath,VMConfigFileVMToolsVersion,VMToolsStatus
VMDK Tab
VM,Datastore,VMDK,StorageFormat,Capacity
VMNetwork Tab
VM,Name,Id,NicID,Type,NetworkName,MacAddress,ConnectionState
to the script…
#v1.0 vMan.ch, 03.03.2016 - Dumps VM Information to XLSX (Needs performance improvement by using has tables etc... but does the job so meh..)
#Requires Module --> https://github.com/dfinke/ImportExcel
#Requires PowerCLI
#Variables
$vCenter = 'vc.vMan.ch'
$OutputPath = "d:\VC\$vCenter.xlsx"
$LogOutPutPath = "d:\VC\$vCenter.log"
#Functions
#Logging Function
Function Log([String]$message, [String]$LogType, [String]$LogFile){
$date = Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
$message = $date + "`t" + $LogType + "`t" + $message
$message >> $LogFile
}
function LoadSnapins(){
$snapinList = @( "VMware.VimAutomation.Core")
$loaded = Get-PSSnapin -Name $snapinList -ErrorAction SilentlyContinue | % {$_.Name}
$registered = Get-PSSnapin -Name $snapinList -Registered -ErrorAction SilentlyContinue | % {$_.Name}
$notLoaded = $registered | ? {$loaded -notcontains $_}
foreach ($snapin in $registered) {
if ($loaded -notcontains $snapin) {
Add-PSSnapin $snapin
}
}
}
# Connect to VC
Function ConnectVC($FunkVC, $FunkLogFileLoc){
Log -Message "Connecting to VC: $FunkVC" -LogType "INFO" -LogFile $FunkLogFileLoc
$Funkconnstat = Connect-VIServer -server $FunkVC
if ($Funkconnstat){
Log -Message "Connection succeeded to VC: $FunkVC" -LogType "INFO" -LogFile $FunkLogFileLoc
Write-Host "Connection succeeded to VC: $FunkVC"
}
else{
Log -Message "Failed to connect to VC: $FunkVC" -LogType "ERROR" -LogFile $FunkLogFileLoc
Write-Host "Failed to connect to VC: $FunkVC"
}
}
#Disconnect from VC
Function DisconnectVC($FunkDCVC, $FunkDCLogFileLoc){
Log -Message "Disconnecting from VC: $FunkDCVC" -LogType "INFO" -LogFile $FunkDCLogFileLoc
$DCFunkconnstat = Disconnect-VIServer -server $FunkDCVC -Force:$true -Confirm:$false
if ($DCFunkconnstat){
Log -Message "Connection to VC: $FunkDCVC Dropped" -LogType "INFO" -LogFile $FunkDCLogFileLoc
Write-Host "Connection to VC: $FunkDCVC Dropped"
}
else{
Log -Message "Failed to disconnect from to VC: $FunkDCVC GOD HELP US ALL!" -LogType "ERROR" -LogFile $FunkDCLogFileLoc
Write-Host "Failed to disconnect from to VC: $FunkDCVC GOD HELP US ALL!"
}
}
filter Get-FolderPath {
<#
.SYNOPSIS
Collates the full folder path
.DESCRIPTION
The function will find the full folder path returning a
name and path
.NOTES
Source: Automating vSphere Administration
#>
$_ | Get-View | % {
$row = "" | select Name, Path
$row.Name = $_.Name
$current = Get-View $_.Parent
$path = $_.Name
do {
$parent = $current
if($parent.Name -ne "vm"){
$path = $parent.Name + "\" + $path
}
$current = Get-View $current.Parent
} while ($current.Parent -ne $null)
$row.Path = $path.Replace('\'+[String]$row.Name,"")
$row
}
}
###############################################SCRIPT###############################################
LoadSnapins
ConnectVC $vCenter $LogOutPutPath
$VMList = Get-VM
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "1). Create Hash table for ID to Name"
$VMList | Sort-Object Id | ForEach-Object -Begin {
$elements = @{}
} -Process {
$elements.Add($_.Id,$_.Name)
}
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "1). Created Hash table for ID to Name"
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "2). Create Hash table for VM to FolderPath"
$VMList | Get-FolderPath | Sort-Object Name | ForEach-Object -Begin {
$elementFolders = @{}
} -Process {
$elementFolders.Add($_.Name,$_.Path)
}
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "2). Created Hash table for VM to FolderPath"
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "3). Create Hash table for VM to Cluster lookup"
$Clusters=get-view -ViewType ClusterComputeResource -Property Name,Host
$vms=Get-View -ViewType VirtualMachine -Property Name,Summary.Runtime.Host
$VMs|%{ [hashtable]$VmsToHostHash+=@{ $_.Name.ToString() = $_.Summary.Runtime.Host.ToString()}}
foreach ($cluster in $clusters ) {$cluster.Host | % { [hashtable]$HostToClusterHash+= @{$_.ToString()=$cluster.Name}} }
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "3). Created Hash table for VM to Cluster lookup"
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "4). Dumping all VMDK Data"
$VMDKData = @()
foreach ($VM in $VMList){
$VMDKs = $VM | get-HardDisk
foreach ($VMDK in $VMDKs) {
if ($VMDK -ne $null){
$CapacityGB = $VMDK.CapacityKB/1024/1024
$CapacityGB = [int]$CapacityGB
$VMDKData += New-Object PSObject -Property @{
VM = $VM.Name
Datastore = $VMDK.FileName.Split(']')[0].TrimStart('[')
VMDK = $VMDK.FileName.Split(']')[1].TrimStart('[')
StorageFormat = $VMDK.StorageFormat
CapacityGB = $CapacityGB
}
}
}
}
$VMDKData | sort-object VM | select VM,Datastore,VMDK,StorageFormat,CapacityGB | Export-Excel $OutputPath -WorkSheetname VMDK
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "4). Done Dumping all VMDK Data"
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "5). Dumping all Network Data"
$VMNet = Get-NetworkAdapter -VM $VMList | Sort-Object Id | select Name,Type,NetworkName,MacAddress,Id,ConnectionState
$ReportNetwork = @()
foreach ($VM in $VMNet){
$ReportNetwork += New-Object PSObject -Property @{
Name = $VM.Name
Type = $VM.Type
NetworkName = $VM.NetworkName
MacAddress = $VM.MacAddress
Id = ($vm.Id -split '/')[0]
NicID = ($vm.Id -split '/')[1]
ConnectionState = $VM.ConnectionState
}
}
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "5). Done Dumping all Network Data"
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "6). Adding VM name to the Network Report by hash lookup"
$ReportNetwork | ForEach-Object {
$_| Add-Member -MemberType NoteProperty -Name VM -Value $elements."$($_.Id)" -PassThru
} | sort-object VM | select VM,Name,NicID,Type,NetworkName,MacAddress,ConnectionState | Export-Excel $OutputPath -WorkSheetname VMNetwork
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "6). Added VM name to the Network Report by hash lookup"
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "7). Dumping all VM Data"
$Report = @()
ForEach ($VM in $VMList){
$View = Get-View $VM
$Report += New-Object PSObject -Property @{
Name = $VM.Name
UUID = $View.config.uuid
DNSName = $VM.ExtensionData.Guest.Hostname
Template = $View.Config.Template
PowerState = $VM.PowerState
CurrentESXHost = $VM.VMHost
VMCluster = $HostToClusterHash[$vmsToHostHash[$VM.Name]]
Datastore = [String]$Datastores = (Get-Datastore -VM $VM).Name
VMConfigFile = $View.Config.Files.VmPathName
ProvisionedGB = [math]::Round($VM.ProvisionedSpaceGB)
UsedSpaceGB = [math]::Round($VM.UsedSpaceGB)
vCPUs = $VM.NumCpu
vMEM_MB = $VM.MemoryMB
VMToolsVersion = $View.Config.tools.ToolsVersion
VMToolsStatus = $View.Guest.ToolsVersionStatus
ResourcePool = $VM.ResourcePool.Name
ConfiguredOS = $View.Config.GuestFullName
RunningOS = $VM.Guest.OSFullName
IPAddress = [String]$VM.Guest.IPAddress
VMVersion = $VM.Version
}
}
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "7). Done Dumping all VM Data"
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "8). Adding FolderPath to each VM"
$Report | ForEach-Object {
$_ | Add-Member -MemberType NoteProperty -Name FolderPath -Value $elementFolders."$($_.Name)" -PassThru
} | sort-object Name | select Name,UUID,DNSName,IPAddress,ConfiguredOS,RunningOS,VMVersion,PowerState,Template,vCPUs,vMEM_MB,ProvisionedGB,UsedSpaceGB,VMCluster,ResourcePool,CurrentESXHost,Datastore,FolderPath,VMConfigFile,VMToolsVersion,VMToolsStatus | Export-Excel $OutputPath -WorkSheetname VM
Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
Write-host "8).Added FolderPath to each VM"
DisconnectVC $vCenter $LogOutPutPath
Remove-Variable * -Force -ErrorAction SilentlyContinue


Recent Comments