Public/Get-VirtualMachinesInfo.ps1
|
$Script:GetVmWrapper = { param( [string]$VmName ) if (-not $VmName) { Get-Vm } else { Get-Vm -Name $VmName } } <# .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-VirtualMachinesInfo { [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 ) # Define default properties to show if no selection is made $DefaultPropsToShow = @("Name", "State", "CPUUsage", "MemoryAssigned", "Uptime", "IPAddresses") # get user selection for which props to show $SelectedProps = $PSBoundParameters['Get'] # and get the vm info list (based on VmName) $VmList = & $Script:GetVmWrapper -VmName $Name # add default props to selection if not already included $DefaultPropsToShow | Where-Object { $_ -notin $SelectedProps } | ForEach-Object { $SelectedProps += $_ } 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++ } } } } } |