Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are parenthesis around a powershell command different than without?

I am using Get-NetIPAddress. ALL I want is the actual address without ANY formatting or tables or anything.

If I use

(Get-NetIPAddress).IPAddres

I get a list of addresses for all ports. I want only a single port so I tried to pipe it but gave nothing in return:

(Get-NetIPAddress).IPv4Address | Where-Object InterfaceAlias -eq "MyPortName"

But if I do this:

Get-NetIPAddress.IPv4Address | Where-Object InterfaceAlias -eq "MyPortName"

It says that this is an invalid command.

If I use this:

Get-NetIPAddress | Where-Object InterfaceAlias -eq "MyPortName" | Where-Object AddressFamily -eq IPv4 | Select-Object IPAddress

I get:

IPAddress
---------
10.45.22.100

So what is going on when I put parenthesis around Get-NetIPAddress and how can I get JUST the darn address number for a particular port?

like image 548
PSNewb Avatar asked Feb 04 '23 08:02

PSNewb


1 Answers

By enclosing a command with () you create an order of precedence, so everything inside of () executes first and after that your other commands are evaluated.

also, there is no such construct as cmdlet.property

as for the second part, after you do (Get-NetIPAddress).IPv4Address you are probably getting another object, just compare output from:

Get-NetIPAddress | Get-Member

and

(Get-NetIPAddress).IPv4Address | Get-Member
like image 88
4c74356b41 Avatar answered Feb 06 '23 21:02

4c74356b41