Hyper-v taking time to fetch IP address for VM through powershell

MAHESH NIKAM 1 Reputation point
2020-10-07T13:30:34.347+00:00

following are two queries and its execution time:

[NOTE: In below script , ID_LIST is list of VM ides that is not included]

CASE 1: With IP address , it's require more time.

2020-10-07 18:43:19,340 DEBUG - START Executing powershell:
Executing PS 1484170718:
'

$vms=get-vm | Where-Object {$_.id -in ID_LIST} | select VMName,Path,Id,State,Status,Computername,ConfigurationLocation ;$FinalOuTPut = New-Object System.Collections.Generic.List[System.Object];
ForEach($vm in $vms) {
$NicIPaddresses = Get-VMNetworkAdapter -VmName $vm.VMName | Select IPaddresses ;
$vmObj=New-Object -TypeName PSObject -Property @{
'VMName' = $vm.VMName
'Path' = $vm.Path
'Id' = $vm.Id
'State' = $vm.State
'Status' = $vm.Status
'Computername' = $vm.Computername
'ConfigurationLocation' = $vm.ConfigurationLocation
'IpAddresses' = $NicIPaddresses.IPaddresses
}; $FinalOuTPut.Add($vmObj); };
$FinalOuTPut

'

2020-10-07 18:43:46,653 DEBUG - [Power Shell Response Trace Data] 1484170718

==========================================================================

CASE 2: without IP Address

2020-10-07 18:46:34,027 DEBUG -
Executing PS 1960823611:
'$vms=get-vm | Where-Object {$_.id -in ID_LIST} | select VMName,Path,Id,State,Status,Computername,ConfigurationLocation ;$FinalOuTPut = New-Object System.Collections.Generic.List[System.Object];
ForEach($vm in $vms) {
$vmObj=New-Object -TypeName PSObject -Property @{
'VMName' = $vm.VMName
'Path' = $vm.Path
'Id' = $vm.Id
'State' = $vm.State
'Status' = $vm.Status
'Computername' = $vm.Computername
'ConfigurationLocation' = $vm.ConfigurationLocation
}; $FinalOuTPut.Add($vmObj); };
$FinalOuTPut '

2020-10-07 18:46:35,433 DEBUG - [Power Shell Trace Data] 1960823611

Hyper-V
Hyper-V
A Windows technology providing a hypervisor-based virtualization solution enabling customers to consolidate workloads onto a single server.
2,688 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,507 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 46,551 Reputation points
    2020-10-07T14:34:22.01+00:00

    Are you just noting that by adding more code to your script that it takes longer to complete?

    Do you need the information from both legacy and virtual NICs? If not, be specific about the data you want to collect.


  2. TimCerling(ret) 1,156 Reputation points
    2020-10-08T14:10:25.303+00:00

    You add code to a script for it to perform more functions. It makes sense that it will take more time.

    Did you know that you can extract the IP address information from your first Get-VM cmdlet? In essence, by issuing the Get-VMNetworkAdapter you are accessing the VM a second time for information that is available from your original Get-VM command, so of course the script will take longer to execute.

    http://techgenix.com/vm-ip-address/ shows how to use PowerShell to get the IP address from the Get-VM command by using an -ExpandProperty switch on the Select-Object command.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.