Public/Get-VirtualMachineInfo.ps1
|
<#
.SYNOPSIS Get information about virtual machines. .DESCRIPTION Returns information about virtual machines, including their state, CPU usage, memory usage, uptime, and network interfaces. .PARAMETER Name Specify the name of the virtual machine to retrieve information for. .PARAMETER Interfaces Include network interface information in the results. .PARAMETER All Include all available information in the results. .PARAMETER IpAddresses Include IP address information in the results. .PARAMETER InterfacesNames Include network interface names in the results. .EXAMPLE ./Get-VirtualMachinesInfo.ps1 -Name "MyVM" #> function Get-VirtualMachineInfo { [CmdletBinding()] param( [parameter(Mandatory = $false)] [string]$Name, [parameter(Mandatory = $false)] [switch]$Interfaces, [parameter(Mandatory = $false)] [switch]$All, [parameter(Mandatory = $false)] [switch]$IpAddresses, [parameter(Mandatory = $false)] [switch]$InterfacesNames ) # and get the vm info list (based on VmName) $VmList = if ((-not $VmName) -or ($All)) { Get-Vm -Name $script:VirtualMachineManager.VMName } else { Get-Vm } if ($All) { $VmList | Select-Object -Property * } elseif ($Interfaces) { $adapters = $VmList.NetworkAdapters | Select-Object -Property Name, SwitchName, MacAddress, Status, IPAddresses $adapters | Select-Object -Property * } elseif ($IpAddresses) { $MachineIpAddresses = $VmList.NetworkAdapters $MachineIpAddresses | Select-Object -Property VMName, SwitchName, IPAddresses } else { # Get the network adapters and add IPAddresses property to the VM info $vmList | Select-Object -Property name, state, cpuusage, memoryassigned, uptime, @{Name = "IpV4"; Expression = { $VmList.NetworkAdapters.IPAddresses -split "\n" | ForEach-Object -Begin { $i = 0 } -Process { if (($i % 2) -eq 0) { $_ } $i++ } } } } } |