Reputation: 4050
I would like my Windows Sandbox to automatically find the ip/name of every host on my home network and so automatically populate my C:\Windows\system32\drivers\etc\hosts
and create a helpler script that will connect to the shares on those hosts. This could also be used on any new system that I setup so would generally be very useful. I have identified the parts that can build this, but can't pull together the last parts (how to pick apart the objects in PowerShell).
The excellent Ping-Subnet project here allows me to collect the IPs of the various hosts (although the Windows Sandbox is isolated on the Hyper-V default router), it can still ping everything on my home LAN, so I can assume that the hosts are on 192.168.0.x or 192.168.1.x (if there is a better way to identify the correct subnet, that would be good to know)
Ping-Subnet 192.168.0.x > get the IPs
Ping-Subnet 192.168.1.x > get the IPs
Then, I can use nslookup
on each IP to get the hostnames (maybe there is a CmdLet that can replace nslookup
though I don't know).
Then, I simply need to structure the resulting information into a string to append to my hosts
file that will look like this for my 3 servers (Asus, HPenvy, HP1). This is required as the Windows Sandbox is on a different subnet so can never resolve these names, but will be able to access them by name if these names are entered in hosts
:
192.168.1.31 Asus
192.168.1.71 HPenvy
192.168.1.94 HP1
Finally, I need to construct an equivalent script that connects to all of my shares (my home servers all have the same username/password, and all have a share called Drives
under which all other drives are read-only visible).
$cred = Get-Credential
# Create the drive mapping with the input credentials
New-PSDrive -Name M -PSProvider FileSystem -Root \\Asus\Drives -Persist -Credential $cred
New-PSDrive -Name N -PSProvider FileSystem -Root \\HPEnvy\Drives -Persist -Credential $cred
New-PSDrive -Name O -PSProvider FileSystem -Root \\HP1\Drives -Persist -Credential $cred
The above should be easy to construct once I have the IPs and Hostnames. Manipulating the objects with PowerShell pipelines is where I'm stuck. e.g. when I do Ping-Subnet 192.168.1.1 | select Computername
I can't get that to work (returns nothing), nor do I see how to slice up the nslookup
output. How can I collect the hostnames + IPs and output my required strings above?
Upvotes: 1
Views: 1014