Requirement: I would like to copy large files to multiple VM using FTP. I have FTP with multiple IP Ranges with Class C Subnet. Ex.“10.10.10.10”,"10.11.10.10"“10.10.12.10”,“10.10.10.13” (All are Class C with subnet 255.255.255.
The script will be executed locally on each VM. so I want the script to match first 3 octets of assigned IP(few VM have multiple IP’s with different ranges too) and then pick the FTP IP from the list of IP address and set that as variable.
As per my knowledge, I created the IP array which contains all the FTP IP Address. Then I use Win32_NetworkAdapterConfiguration to get the list of IP address, Subnet, and Gateway. I put the Win32_NetworkAdapterConfiguration to the hash table so that I can use that for comparison.
Need help to compare hash table data and fetch the matched IP address from IP array and set that FTP IP as Variable.
Ex. If VM A have IP 10.10.12.25 then it should use 10.10.12.10 FTP IP.
I need to use Win32_NetworkAdapterConfiguration to make the script compatible with old operating systems running PS v2
I did not get what you try to do and I do not see where you compare IP adresses in your code snippet but if you need to partially compare IP addresses you could simply split the IP addresses to their octets you could use the [IPADDRESS] type … something like this:
For that matter, since you know the IP addresses, I’m sure you also know the systems those IPs are on and you just need to use the right IP for the system that’s running the script based on how I read your original question.
I know the FTP IP’s because it’s on single VM. I have 3rd party agent installed on all VM which will help me to deploy and execute the script on all VM. Something like 1000+ multiple domain join plus workgroup VM. I want entire traffic to be lan so same subnet needs to be used. Hence I want to discover and match the ip of VM from where the script will be executed with list of FTP IP’s because I will be adding all ip range addresses on FTP Server to make sure all traffic is lan.
I thought same but the problem is all 20 IP range that I have belongs Class C hence Subnet will be same for all VM i.e. 255.255.255.0
The only factor we can find between FTP IP’s and VM IP is first 3 octets will be same and last octet will be unique. Hence I will have build condition to match first 3 octets of FTP IP’s with VM IP.
Hey Amol,
That is fine that they all have the same Subnet Mast, but they would still belong to different subnets.
The thing is based on your example "$FTPIPs = @(“10.10.10.10”,“10.11.10.10"“10.10.12.10”,“10.10.10.13”)”. If you have a subnet mast of 255.255.255.0 then you have 2 FTP servers in 1 subnet, and 2 other FTP servers in their own subnets.
10.10.10.10 and 10.10.10.13 are in the same subnet
10.11.10.10 is in it’s own
10.10.12.10 is in it’s own
If you are intent on matching the first there octets, then you can use Olaf’s suggestion, but this is less flexible as you will only be able to use this method if your Subnet Mask is always and forever “255.255.255.0” It will not work for other masks such as “255.255.128.0” or “255.255.255.128” wheres as the method of using the subnet to compare them would account for these variances.
Based on the examples of Subnet provided by Curtis I thinks I should go with below option as its clean approach but as mentioned My subnet will always remain 255.255.255.0 so I will also see if I need to use Olaf option too.
$FTPIPs = @("10.10.10.10","10.11.10.10","10.10.12.10","10.10.10.13")
$Prop = @{
'IPAddress' = '10.11.10.54'
'SubnetMask' = '255.255.255.128'
'DefaultGateway' = '10.11.10.1'
}
$LocalIP = New-Object -TypeName PSobject -Property $Prop
$FTPIPs |
ForEach-Object {
If ((([IPAddress]$LocalIP.IPAddress).Address -band ([IPAddress]$LocalIP.SubnetMask).Address) -eq (([IPAddress]$_).Address -band ([IPAddress]$LocalIP.SubnetMask).Address)) {
"$_ is in the same subnet as $($LocalIP.IPAddress)"
}
}