Menu Zamknij

Inwentaryzacja maszyn wirtualnych

Nie raz potrzebujemy zinwentaryzować maszyny wirtualne na posiadanym klastrze. Jeśli mamy SC VMM to problemu większego nie ma, gorzej gdy trzeba to zrobić ręcznie.

Spisanie 2-3 maszyn to nie problem, ale jak mamy ich setki i chcemy sobie zrobić zestawienie maszyn, dysków, kart sieciowych – ręczna robota zajmie nam nawet wiele godzin 🙂

Też miałem ten problem, więc napisałem prosty skrypt, który generuje czytelny dokument HTML zawierający wszystkie istotne informacje o naszym środowisku. Skrypt przyda się również, jeśli będziemy musieli zinwentaryzować środowisko klienta i przeprowadzić prostą analizę czy jest ono poprawnie ustawione.

Skrypt nie wymaga dodatkowej konfiguracji o ile włączymy go na hoście będącym członkiem klastra Hyper-V. Skrypt musi być uruchomiony z prawami lokalnego administratora

Czekam na komentarze…

#
# Get VM Summary
# C:\Tools\Get-VMSummary.ps1
#
# Author: [email protected]
# Version: 2013-08-31 17:31
#
#################################################

$ClusterNodes = Get-ClusterNode
$ClusterName = (Get-Cluster).Name
$ScriptPath = split-path $SCRIPT:MyInvocation.MyCommand.Path
$ReportFile = $($ScriptPath+"\GetVMSummary-report.html")

$report_VMs = @()
$report_HardDisks = @()
$report_Networks = @()
$report_Snapshots = @()
$html_header = "<html>
<head><title>Virtual Machines Summary</title></head>
<body>
<style>
* {font-family: 'trebuchet ms'; font-size:12px;}
h1 {font-size:20px;border-bottom:1px solid black;}
h2 {font-size:16px;}
td {white-space:nowrap;border-bottom: 1px solid #CCC;padding: 0 5px;}
table {border-collapse: collapse;}
th {padding: 10px 5px;width:75px;white-space:normal;}
tr:nth-child(even) {background: #eee;}
</style>
<h1>VM Report for $ClusterName</h1>
"
$html_footer = "<br><br>Generated by Get-VMSummary.ps1 (&copy; [email protected])<br></body></html>";
$counter=0
foreach($ClusterNode in $ClusterNodes)
{
 echo "Search VM on $ClusterNode"
$ClusterNode_Name = $ClusterNode.Name;
$VMList = Get-VM -ComputerName $ClusterNode_Name
foreach ( $VM in $VMList )
 {
 $VMId = $VM.Id;
 $VMName = $VM.Name;
 echo " - Get Info about $VMName"
$VMDVDDrive = $VM | Get-VMDVDDrive
 $VMMemory = $VM | Get-VMMemory
 $VMProcessor = $VM | Get-VMProcessor
 $_VMIntegrationService = $VM | Get-VMIntegrationService
 $VMIntegrationService = @{}
 $VMIntegrationService.TimeSynchronization = ($_VMIntegrationService|Where {$_.Name -eq 'Time Synchronization'}).Enabled
 $VMIntegrationService.Heartbeat = ($_VMIntegrationService|Where {$_.Name -eq 'Heartbeat'}).Enabled
 $VMIntegrationService.KVP_Exchange = ($_VMIntegrationService|Where {$_.Name -eq 'Key-Value Pair Exchange'}).Enabled
 $VMIntegrationService.Shutdown = ($_VMIntegrationService|Where {$_.Name -eq 'Shutdown'}).Enabled
 $VMIntegrationService.VSS = ($_VMIntegrationService|Where {$_.Name -eq 'VSS'}).Enabled
 $VMIntegrationService.Version = [string]$VM.IntegrationServicesVersion
$report_HardDisks_head = @("VM Id", "VM Name", "VHD Format", "VHD Type", "File size (GB)", "Minimum Size (GB)", "Current Size (GB)", "Logical Sector Size", "Physical Sector Size", "Fragmentation (%)", "Path", "Parent Path" )
 $_VMHardDiskDrive = $VM | Get-VMHardDiskDrive
 $vmdisks_filesize = 0
 $vmdisks_size = 0
 foreach( $disk in $_VMHardDiskDrive)
 {
 $item = $disk | Get-VHD -ComputerName $ClusterNode_Name
 $obj = New-Object -Type PSObject -Property @{
 "VM Id" = $VMId
 "VM Name" = $VMName
 "VHD Format" = [string]$item.VhdFormat
 "VHD Type" = [string]$item.VhdType
 "File size (GB)" = [Math]::round($item.FileSize/1024/1024/1024,2)
 "Minimum Size (GB)" = [Math]::round($item.MinimumSize/1024/1024/1024,2)
 "Current Size (GB)" = [Math]::round($item.Size/1024/1024/1024,2)
 "Logical Sector Size" = $item.LogicalSectorSize
 "Physical Sector Size" = $item.PhysicalSectorSize
 "Fragmentation (%)" = $item.FragmentationPercentage
 "Path" = $item.Path
 "Parent Path" = $item.ParentPath
 }
 $vmdisks_filesize += $item.FileSize
 $vmdisks_size += $item.Size
 $report_HardDisks += $obj
 }
$report_Networks_head = @("VM Id", "VM Name", "Switch Name", "Dynamic Mac Address", "Mac Address", "Connected", "VLAN Operation Mode", "VLAN ID", "Legacy", "Dhcp Guard", "Router Guard" )
 $_VMNetworkAdapter = $VM | Get-VMNetworkAdapter
 foreach( $item in $_VMNetworkAdapter)
 {
 $vlan = $item | Get-VMNetworkAdapterVlan
 $obj = New-Object -Type PSObject -Property @{
 "VM Id" = $VMId
 "VM Name" = $VMName
 "Switch Name" = $item.SwitchName
 "Mac Address" = $item.MacAddress
 "Dynamic Mac Address" = [string]$item.DynamicMacAddressEnabled
 "Connected" = [string]$item.Connected
 "VLAN Operation Mode" = [string]$vlan.OperationMode
 "VLAN ID" = $vlan.AccessVlanId
 "Legacy" = [string]$item.IsLegacy
 "Dhcp Guard" = [string]$item.DhcpGuard
 "Router Guard" = [string]$item.RouterGuard
 }
$report_Networks += $obj
 }
$report_Snapshots_head = @("VM Id", "VM Name", "Type", "Name", "Path", "Notes", "Creation Time", "Size Of System Files (GB)" )
 $_VMSnapshot = $VM | Get-VMSnapshot
 foreach( $item in $_VMSnapshot)
 {
 $obj = New-Object -Type PSObject -Property @{
 "VM Id" = $VMId
 "VM Name" = $VMName
 "Type" = $item.SnapshotType
 "Name" = $item.Name
 "Notes" = $item.Notes
 "Path" = $item.Path
 "Creation Time" = $item.CreationTime
 "Size Of System Files (GB)" = [Math]::round($item.SizeOfSystemFiles/1024/1024/1024,2)
 }
 $report_Snapshots += $obj
 }
$report_VMs_head = @("VM Id", "VM Name", "Host", "State", "Memory Startup (MB)", "Memory Minimum (MB)", "Memory Maximum (MB)", "Memory Dynamic", "Memory Buffer", "Memory Priority",
 "Smart Paging File In Use", "Processor Count", "Processor Reserve", "Processor Relative Weight", "Processor Compatibility For Migration", "Processor Compatibility For Older OS",
 "Hard Disks Total Used (GB)", "Hard Disks Total Allocated (GB)", "DVD Media Type", "DVD Path", "Integration Services - Time Synchronization", "Integration Services - Heartbeat",
 "Integration Services - Key-Value Pair Exchange", "Integration Services - Shutdown", "Integration Services - VSS", "Integration Services - Version" )
 $obj = New-Object -Type PSObject -Property @{
 "VM Id" = $VMId
 "VM Name" = $VMName
 "Host" = $VM.ComputerName
 "State" = [string]$VM.State
 "Memory Startup (MB)" = [Math]::round($VMMemory.Startup/1024/1024,0)
 "Memory Minimum (MB)" = [Math]::round($VMMemory.Minimum/1024/1024,0)
 "Memory Maximum (MB)" = [Math]::round($VMMemory.Maximum/1024/1024,0)
 "Memory Dynamic" = $VMMemory.DynamicMemoryEnabled
 "Memory Buffer" = $VMMemory.Buffer
 "Memory Priority" = $VMMemory.Priority
 "Smart Paging File In Use" = $VM.SmartPagingFileInUse
 "Status" = $VM.Status
 "Processor Count" = $VMProcessor.Count
 "Processor Compatibility For Migration" = $VMProcessor.CompatibilityForMigrationEnabled
 "Processor CompatibilityForOlderOS" = $VMProcessor.CompatibilityForOlderOperatingSystemsEnabled
 "Processor Reserve" = $VMProcessor.Reserve
 "Processor Relative Weight" = $VMProcessor.RelativeWeight
 "Hard Disks Total Used (GB)" = [Math]::round($vmdisks_filesize/1024/1024/1024,2)
 "Hard Disks Total Allocated (GB)" = [Math]::round($vmdisks_size/1024/1024/1024,2)
 "DVD Media Type" = [string]$VMDVDDrive.DvdMediaType
 "DVD Path" = $VMDVDDrive.Path
 "Integration Services - Time Synchronization" = $VMIntegrationService.TimeSynchronization
 "Integration Services - Heartbeat" = $VMIntegrationService.Heartbeat
 "Integration Services - Key-Value Pair Exchange" = $VMIntegrationService.KVP_Exchange
 "Integration Services - Shutdown" = $VMIntegrationService.Shutdown
 "Integration Services - VSS" = $VMIntegrationService.VSS
 "Integration Services - Version" = $VMIntegrationService.Version
 }
$report_VMs += $obj
$counter=$counter+1
 }
}
$html_header|Out-File $ReportFile -Encoding ascii
$report_VMs|ConvertTo-HTML -Fragment -Pre "<h2>Virtual Machines Info</h2>" $report_VMs_head|Out-File $ReportFile -Encoding ascii -Append
$report_HardDisks|ConvertTo-HTML -Fragment -Pre "<h2>Virtual Hard Disks</h2>" $report_HardDisks_head|Out-File $ReportFile -Encoding ascii -Append
$report_Networks|ConvertTo-HTML -Fragment -Pre "<h2>Networks</h2>" $report_Networks_head|Out-File $ReportFile -Encoding ascii -Append
$report_Snapshots|ConvertTo-HTML -Fragment -Pre "<h2>Snapshots</h2>" $report_Snapshots_head|Out-File $ReportFile -Encoding ascii -Append
$html_footer|Out-File $ReportFile -Encoding ascii -Append
ii $ReportFile

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Witryna wykorzystuje Akismet, aby ograniczyć spam. Dowiedz się więcej jak przetwarzane są dane komentarzy.