Code review: Is this the best approach for retrieving network info of a host?

Let me start by saying that this is the very first time I've worked with Powershell. I've been asked to take over the maintenance of a startup script that is used to join the station to the (samba4) domain.

After reviewing the script I found enough bugs to want to do a major rewrite, but for the purpose of this thread, I'm focusing on the section that retrieves the workstation's IP address.

Questions:
1) Is this a good (best practice) approach or is there a better approach?

2) What adjustments do I need to make to have it be more generic so that I can retrieve the same info from a remote host? My goal is to move the function into a module so that it can be used in other scripts.

for remote computers you need to add -computername parameter to each wmi call (and may be switch to CIM)

.IPEnabled -match 'true' — working but not the best option
you shoukd read about -match operator. it's for strings, not booleans as .IPEnabled
you can use where{ $_.IpEnabled -eq $True } or (because it's already boolean where{ $_.IpEnabled }
or return to -Filter "IPEnabled = True"

and...serious bug... you code will return only the last info.'comp1','comp2','comp3' | Get-HostInfo will return info only for comp3, becuase you iutput data to pipeline in the end{} block
you should clean end{} block and generate output to pipeline, not variableNew-Object -TypeName PSObject -Property $hostinfo

It does return all the proper info for both hosts provided they are on the same subnet. If the remote host is on a different subnet, instead of receiving the desired data I receive the following error output for that host.

afaik there is no subnet limitations. I think this is firewall that block wmi calls
btw, I overlooked one more thing – how you get macaddress
you make another wmi caLL (the same call) and search the same object...
instead of

I figured out that fix for the mac address assignment shortly after applying the prior adjustments.

When I wrote the function, I thought about putting the $hostinfo assignment inside the if { block but didn't because if the conditional never evaluated to true, then I wouldn't have the hash keys in the object, which I need to have available for later use in the script.

there is two options:
1. you can create hostinfo hash before foreach loop and fill it with defaults
2. you can create resulting object inside if { block (if your function ends here and all other object usage outside of a function (this is preferrable)