1

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?

YorSubs
  • 3,194
  • 7
  • 37
  • 60
  • The WinSandbox is just another host on your home network, using the same DHCP scope. So, one should expect communications from it to hit other hosts, if ping, etc., and other stuff are properly configured/allowed. [Resolve-DnsName](https://learn.microsoft.com/en-us/powershell/module/dnsclient/resolve-dnsname?view=windowsserver2022-ps), is what you are after relative to NSLookup. There are many PS equivalents to cmd.exe commands. Just do a web search for them. You are also asking about a non-default cmdlet. The cmdlet must provide what you want. You can change that code in that project. – postanote Oct 02 '22 at 17:45
  • 1
    It sounds like you're looking for the following: ``ping-subnet 192.168.0.0 | ForEach-Object ComputerName -PipelineVariable ipv4 | Resolve-DnsName -QuickTimeout -ErrorAction SilentlyContinue -ErrorVariable errs | ForEach-Object { "{0}`t{1}" -f $ipv4, $_.NameHost }`` – mklement0 Oct 02 '22 at 17:48
  • 1
    I wish I knew how you always find this so easy to do! Seems that `ping-subnet` really wants to see anything but `.0` to work for me, and my class C is `192.169.1.x`, so I changed the argument to `192.168.1.1` and it's working perfectly. I can construct the 2nd script easily with that thanks. I really don't understand the `foreach Computername -PipelineVariable ipv4` bit (I see that you are populating the `ipv4` variable, but the `-PipelineVariable` part is voodoo. Thanks, it's great! – YorSubs Oct 02 '22 at 18:21
  • Glad to hear it, @YorSubs. `Ping-Subnet` accepts anything that it can cast to `[IPAddress]`, so the last component shouldn't matter, as long as it's between `0` an `255`. `foreach Computername -PipelineVariable ipv4` extracts the `.ComputerName` property value from each of `Ping-Subnet`'s output objects and also stores it in pipeline variable `$ipv4`, so that you can refer to it later in the same pipeline in a script block; this is necessary, because the `Resolve-DnsName` output objects don't contain the input IPv4 address (as such). – mklement0 Oct 02 '22 at 18:57

0 Answers0