Hey all, I'm learning PowerShell and came across an obstacle I can't seem to overcome. I'm working on a script to determine the remaining lifetime on a workstation's DHCP lease. Here's my code:
write-host "Computer Name?" $CompName=read-host invoke-command -computername $CompName -scriptblock {get-netipaddress} | select-object -Property ValidLifetime
I'm able to pull remaining lifetime time but I get multiple values. For example when I run this script on one of our dispactch computers I get four "sections" of responses: IPv6, IPv6 Loopback, IPv4, and localhost.
[dispatch-4]: PS C:\Users\jobee\Documents> get-netipaddress IPAddress : fe80::443d:367d:683f:d011%5 InterfaceIndex : 5 InterfaceAlias : Ethernet AddressFamily : IPv6 Type : Unicast PrefixLength : 64 PrefixOrigin : WellKnown SuffixOrigin : Link AddressState : Preferred ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore IPAddress : ::1 InterfaceIndex : 1 InterfaceAlias : Loopback Pseudo-Interface 1 AddressFamily : IPv6 Type : Unicast PrefixLength : 128 PrefixOrigin : WellKnown SuffixOrigin : WellKnown AddressState : Preferred ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore IPAddress : 192.168.1.131 InterfaceIndex : 5 InterfaceAlias : Ethernet AddressFamily : IPv4 Type : Unicast PrefixLength : 24 PrefixOrigin : Dhcp SuffixOrigin : Dhcp AddressState : Preferred ValidLifetime : 7.23:59:35 PreferredLifetime : 7.23:59:35 SkipAsSource : False PolicyStore : ActiveStore IPAddress : 127.0.0.1 InterfaceIndex : 1 InterfaceAlias : Loopback Pseudo-Interface 1 AddressFamily : IPv4 Type : Unicast PrefixLength : 8 PrefixOrigin : WellKnown SuffixOrigin : WellKnown AddressState : Preferred ValidLifetime : Infinite ([TimeSpan]::MaxValue) PreferredLifetime : Infinite ([TimeSpan]::MaxValue) SkipAsSource : False PolicyStore : ActiveStore [dispatch-4]: PS C:\Users\jobee\Documents>
How do I change my script to just pull the IPv4 lifetime?
Thanks,
Joe B