$latestCsv = Get-ChildItem -Path . -Filter "VMListReport-$($env:COMPUTERNAME)-*.csv" -File | Sort-Object LastWriteTime -Descending | Select-Object -First 1 $oldvmList = if ($latestCsv) { @(Import-Csv -LiteralPath $latestCsv.FullName) } else { $null } $report = Get-VM | ForEach-Object { $vm = $_ if ($vm.Generation -eq "2") { $firmware = Get-VMFirmware -VM $vm $security = Get-VMSecurity -VM $vm $secureboot = $firmware.SecureBoot $tpmenabled = $security.TpmEnabled $osType = if ($firmware.SecureBootTemplate -eq "MicrosoftWindows") { "Windows" } else { "Linux" } } else { $secureboot = $null $tpmenabled = $null $osType = $null } $disks = @(Get-VMHardDiskDrive -VM $vm) $dvds = @(Get-VMDVDDrive -VM $vm) $nics = @(Get-VMNetworkAdapter -VM $vm) $osDisk = $null $controllerType = switch ($vm.Generation) { 2 { 'SCSI' } 1 { 'IDE' } default { $null } } if ($null -ne $controllerType) { $osDisk = $disks | Where-Object { $_.ControllerType -eq $controllerType } | Sort-Object ControllerNumber, ControllerLocation | Select-Object -First 1 } $osDiskSizeGB = if ($osDisk.Path) { try { [math]::Round((Get-VHD -Path $osDisk.Path -ErrorAction Stop).Size / 1GB, 1) } catch { $null } } else { "(nodisk)" } $os = $null $osver = $null $vmip = $null if ($vm.State -eq 'Running') { $kvp = & ".\get-kvpdata.ps1" -VMName $vm.Name $os = $kvp | Where-Object Name -eq 'OSName' | Select-Object -ExpandProperty Data if ($os -like '*Windows*') { $osver = $kvp | Where-Object Name -eq 'OSVersion' | Select-Object -ExpandProperty Data } else { $osver = $kvp | Where-Object Name -eq 'OSMajorVersion' | Select-Object -ExpandProperty Data } $vmip = & ".\get-vmnicinfo.ps1" -VMName $vm.Name | Select-Object -First 1 | Select -ExpandProperty IPAddresses | Select -First 1 } else { if ($oldvmList) { foreach ($oldvm in $oldvmList) { if ($oldvm.vmid -eq $vm.VMId){ if($oldvm.OperatingSystem) { $os = "($($oldvm.OperatingSystem))" $os = $os.Replace('((', '(').Replace('))', ')') } if($oldvm.OSVersion) { $osver = "($($oldvm.OSVersion))" $osver = $osver.Replace('((', '(').Replace('))', ')') } if($oldvm.IPAddress0) { $vmip = "($($oldvm.IPAddress0))" $vmip = $vmip.Replace('((', '(').Replace('))', ')') } } } } } [pscustomobject]@{ VMName = $vm.Name Generation = $vm.Generation ConfigPath = $vm.ConfigurationLocation VMId = $vm.VMId ## Firmware ## Security SecureBoot = $secureboot TPMEnabled = $tpmenabled OSType = $ostype OperatingSystem = $os OSVersion = $osver ## Processor vCPUs = $vm.ProcessorCount NestedVirt = (Get-VMProcessor -VM $vm).ExposeVirtualizationExtensions ## Memory # MemoryAssigned = $vm.MemoryAssigned DynamicMemory = $vm.DynamicMemoryEnabled RAMStartupGB = [math]::Round($vm.MemoryStartup / 1GB, 1) RAMMinimumGB = [math]::Round($vm.MemoryMinimum / 1GB, 1) RAMMaximumGB = [math]::Round($vm.MemoryMaximum / 1GB, 1) ## HardDrives OSDiskPath = if ($null -ne $osDisk) { $osDisk.Path } else { "(nodisk)" } OSDiskGB = $osDiskSizeGB #DataDisks = [math]::Max($disks.Count - 1, 0) DataDisks = $disks.Count - $(if ($null -ne $osDisk) { 1 } else { 0 }) ## DVDDrives DVDs = $dvds.Count ## NetoworkAdapters NICs = $nics.Count IPAddress0 = $vmip Version = $vm.Version CreationTime = $vm.CreationTime # isClustered = $vm.isClustered } } $fileName = '.\VMListReport-{0}-{1}.csv' -f $env:COMPUTERNAME, (Get-Date -Format 'yyyyMMdd-HHmmss') $report | Export-Csv -Path $fileName -NoTypeInformation -Encoding UTF8 Write-Host "CSV out to: $($fileName)" $report