Retrieving VM Subnet Mask and Default Gateway via PowerCLI
search cancel

Retrieving VM Subnet Mask and Default Gateway via PowerCLI

book

Article ID: 435607

calendar_today

Updated On:

Products

VMware vSphere ESXi

Issue/Introduction

When using the PowerCLI Get-VM cmdlet combined with the Guest property (e.g., Get-VM <VMName> | Select-Object -ExpandProperty Guest), users can retrieve IP addresses, but the Subnet Mask and Default Gateway information is not returned.

Resolution

To retrieve the subnet mask and default gateway, users can execute a command directly within the guest operating system using the Invoke-VMScript cmdlet. This requires that VMware Tools is running.

 

Command example and output is following.

PS C:\Users\Administrator> Invoke-VMScript -VM "Win2022" -ScriptType PowerShell -ScriptText "ipconfig | findstr /R 'IPv4 Subnet Default'"
WARNING: The version of VMware Tools on VM 'Win2022' is out of date and may cause Invoke-VMScript to work improperly.

ScriptOutput
-------------------------------------------------------------------------------------------------------------------------------------------
|     IPv4 Address. . . . . . . . . . . : 192.168.1.2
|     Subnet Mask . . . . . . . . . . . : 255.255.255.0
|     Default Gateway . . . . . . . . . : 192.168.1.254
|
-------------------------------------------------------------------------------------------------------------------------------------------

Additional Information

If Invoke-VMScript execution fails, please refer to the following Knowledge.

Unable to Invoke powercli command Invoke-VMScript

Unable to use the Invoke-VMSCript cmdlet