spuder
spuder

Reputation: 18387

Powershell, get ip4v address of VM

I'm new to powershell and I"m trying to get just the IPv4 address of a vm and save it as a string.

I can get all network attributes like so:

PS C:\Windows\system32> get-vm | select -ExpandProperty networkadapters | select vmname, macaddress, switchname, ipaddres
sses

VMName                        MacAddress                    SwitchName                    IPAddresses
------                        ----------                    ----------                    -----------
foobar                                     vSwitch                       {192.0.2.1, fe80::84a...

I can get both the v4 and the v6 ip address

PS C:\Windows\system32> $IP = ( GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter).IpAddresses
PS C:\Windows\system32> $IP
192.0.2.1
fe80::d47e:
   ----------    

How can I get just the v4 address as a string?

Update

It looks like there is no object property that just includes the v4 address

PS C:\Windows\system32> GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter | Format-List -Property *


IovWeight                : 0
IovQueuePairsRequested   : 1
IovQueuePairsAssigned    : 0
IovInterruptModeration   : Default
IovUsage                 : 0
ClusterMonitored         : True
VirtualFunction          :
IsLegacy                 : False
IsManagementOs           : False
IsExternalAdapter        : False
Id                       : Microsoft:xxxxxxxxxxx
AdapterId                : xxxxxxxxxxx
DynamicMacAddressEnabled : True
MacAddress               : 00155D5B9B14
MacAddressSpoofing       : Off
SwitchId                 : xxxxxxxxxxx
Connected                : True
PoolName                 :
SwitchName               : vSwitch
AclList                  : {}
ExtendedAclList          : {}
IsolationSetting         : Microsoft.HyperV.PowerShell.VMNetworkAdapterIsolationSetting
CurrentIsolationMode     : Vlan
RoutingDomainList        : {}
DhcpGuard                : Off
RouterGuard              : Off
PortMirroringMode        : None
IeeePriorityTag          : Off
VirtualSubnetId          : 0
DynamicIPAddressLimit    : 0
StormLimit               : 0
AllowTeaming             : Off
VMQWeight                : 100
IPsecOffloadMaxSA        : 512
VmqUsage                 : 0
IPsecOffloadSAUsage      : 0
VFDataPathActive         : False
VMQueue                  :
MandatoryFeatureId       : {}
MandatoryFeatureName     : {}
VlanSetting              : Microsoft.HyperV.PowerShell.VMNetworkAdapterVlanSetting
BandwidthSetting         :
BandwidthPercentage      : 0
TestReplicaPoolName      :
TestReplicaSwitchName    :
StatusDescription        : {OK}
Status                   : {Ok}
IPAddresses              : {192.0.2.1, fe80::xxxxxxxxxxx}
ComputerName             : xxxxxxxxxxx
Name                     : Network Adapter
IsDeleted                : False
VMId                     : xxxxxxxxxxx
VMName                   : foobar
VMSnapshotId             : 00000000-0000-0000-0000-000000000000
VMSnapshotName           :
Key                      :                                                                                                                 

Upvotes: 1

Views: 6438

Answers (3)

Graham Gold
Graham Gold

Reputation: 2475

IPAddresses looks like an array or list and you only want the first one so try:

$IP = ( GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter).IpAddresses[0]

Upvotes: 1

spuder
spuder

Reputation: 18387

Assuming there is only 1 V4 address, and that the v4 address is the first output, do:

$IP = ( GEt-VM -ComputerName $HVCOMPUTERNAME -VMName $HVNAME | Get-VMNetworkAdapter).IpAddresses | Select-String -List 1

Upvotes: 1

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26268

You can just filter out any IP that has ":" in it, as such:

$IP | ?{$_ -notmatch ':'}

Upvotes: 5

Related Questions