PowerShell脚本需求:匹配IP前三段选取对应FTP服务器
Hey there! I've put together a PowerShell script that fits your exact requirements, plus breakdowns of how each part works so you can tweak it as needed:
PowerShell Script for Targeted FTP File Transfer to Matching Subnet Servers
This script runs locally on each VM, identifies which FTP server belongs to the same C-class subnet as any of the VM's IP addresses, then uses that server to transfer your large file.
# Define your list of FTP server IPs (add/remove as needed) $ftpServers = @("10.10.10.10","10.11.10.10","10.10.12.10","10.10.10.13") # Grab all non-loopback IPv4 addresses on the local VM (handles multi-IP setups) $localIps = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.IPAddress -notlike "127.*" } # Extract the first three octets (subnet prefix) from each local IP (for C-class /24 subnets) $localSubnetPrefixes = $localIps | ForEach-Object { $octets = $_.IPAddress -split "\." "$($octets[0]).$($octets[1]).$($octets[2])" } # Find the first FTP server that shares a subnet prefix with any local IP $targetFtpServer = $ftpServers | Where-Object { $ftpOctets = $_ -split "\." $ftpPrefix = "$($ftpOctets[0]).$($ftpOctets[1]).$($ftpOctets[2])" $localSubnetPrefixes -contains $ftpPrefix } | Select-Object -First 1 # Exit if no matching FTP server is found if (-not $targetFtpServer) { Write-Error "Oops! No matching FTP server found for any of the VM's subnets." exit 1 } Write-Host "Successfully found matching FTP server: $targetFtpServer" # --- Configure your file transfer details here --- $localFilePath = "C:\Data\YourLargeFile.iso" # Replace with your local file path $ftpRemotePath = "/VMFiles/YourLargeFile.iso" # Replace with the remote path on FTP $ftpUsername = "ftp_user" # Replace with your FTP username $ftpPassword = "secure_password123" # Replace with your FTP password # Create secure credentials to avoid exposing plaintext passwords $securePassword = ConvertTo-SecureString $ftpPassword -AsPlainText -Force $ftpCredential = New-Object System.Management.Automation.PSCredential ($ftpUsername, $securePassword) # Transfer the large file using WebClient (efficient for big files, streams instead of loading whole file) try { $webClient = New-Object System.Net.WebClient $webClient.Credentials = $ftpCredential $fullFtpUrl = "ftp://$targetFtpServer$ftpRemotePath" Write-Host "Starting file transfer to $fullFtpUrl..." $webClient.UploadFile($fullFtpUrl, $localFilePath) Write-Host "File transfer completed successfully! 🎉" } catch { Write-Error "Transfer failed: $_" } finally { # Clean up the WebClient object to free resources if ($webClient) { $webClient.Dispose() } }
How It Works (Key Bits Explained)
- Multi-IP Support: The script grabs all active non-loopback IPs on the VM, so even if your VM has addresses across multiple subnets, it'll check all of them.
- Subnet Matching: For each local IP and FTP server, we split the address into octets and compare the first three (since you're using C-class 255.255.255.0 subnets). The first matching FTP server is selected.
- Secure Credentials: We convert the plaintext password to a secure string to keep sensitive data safe in memory.
- Large File Friendly:
System.Net.WebClientstreams the file instead of loading the entire thing into RAM, which is crucial for big files that would crash a script using simpler methods.
Quick Tweaks for Your Use Case
- If you need to transfer to all matching FTP servers (not just the first), remove the
Select-Object -First 1line and loop through the$targetFtpServerarray. - If your FTP uses active mode instead of passive, swap out the WebClient section with
FtpWebRequestand set$request.UsePassive = $false. - Add progress tracking by using
FtpWebRequestwith a stream and updating a progress bar—great for extra-large files where you want to see how things are going.
内容的提问来源于stack exchange,提问作者Apps4Rent Guru
相关产品推荐
相关产品推荐

