Last active
January 2, 2023 14:20
-
-
Save andersalavik/2a0954c3c2e8f96b221a30e937d4d6ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Cred to Chris.Childerhose on the Veeam community forum for the initial script. | |
# https://community.veeam.com/script-library-67/powershell-migrate-vms-from-hyper-v-to-vsphere-1585?postid=12982#post12982 | |
# | |
# This is version works with 1 och 2 dns addresses. | |
# | |
$path = "C:\TEMP\NetIP.json" | |
if (-not (Test-Path $path)) { | |
$NetIP = Get-NetIPConfiguration | |
# Check if IP address is not APIPA | |
if ($NetIP.IPv4Address.IPAddress -notlike "169.25*") { | |
$myObject = [PSCustomObject]@{ | |
IPAddress = $NetIP.IPv4Address.IPAddress | |
Subnet = $NetIP.IPv4Address.PrefixLength | |
Gateway = $NetIP.IPv4DefaultGateway.NextHop | |
} | |
# Filter out $NetIP.DNSServer.ServerAddresses[] to only include IPv4 addresses | |
$dnsAddresses = $NetIP.DNSServer.ServerAddresses | Where-Object { | |
# Check if the address is a valid IPv4 address | |
$_ -match "\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b" | |
} | |
# Add DNS server addresses | |
if ($dnsAddresses.Count -eq 1) { | |
$myObject | Add-Member -Type NoteProperty -Name DNS1 -Value $dnsAddresses | |
} | |
if ($dnsAddresses.Count -ge 2) { | |
$myObject | Add-Member -Type NoteProperty -Name DNS1 -Value $dnsAddresses[0] | |
$myObject | Add-Member -Type NoteProperty -Name DNS2 -Value $dnsAddresses[1] | |
} | |
# Save IP configuration to JSON file | |
$myObject | ConvertTo-Json -Depth 1 | Out-File $path | |
} | |
} | |
$hypervisor = Get-CimInstance -ClassName Win32_ComputerSystem | Select-Object -ExpandProperty Manufacturer | |
# Check if VM is running on VMware | |
if ($hypervisor -match "vmware") { | |
# Get CD-ROM drive letter and path to VMTools installer | |
$cddrive = Get-Volume | Where-Object{$_.DriveType -eq "CD-ROM"} | |
$installer = "$($cddrive.DriveLetter):\setup64.exe" | |
$arg = '/S /v /qn ADDLOCAL=ALL' | |
# Check if VMTools ISO is mounted | |
if (Test-Path $installer) { | |
Start-Process $installer -ArgumentList $arg -Wait | |
} | |
# Check if there is a backup of the IP configuration and restore to new VMXNET3 | |
elseif (Test-Path $path) { | |
$myObject = Get-Content $path | ConvertFrom-Json | |
$alias = Get-NetIPConfiguration | Where-Object {$_.InterfaceDescription -like "*vmxnet*" } | Select-Object -ExpandProperty InterfaceAlias | |
New-NetIPAddress -InterfaceAlias $alias -IPAddress $myObject.IPAddress -PrefixLength $myObject.Subnet -DefaultGateway $myObject.Gateway | |
if ($myObject.DNS2) { | |
Set-DnsClientServerAddress -InterfaceAlias $alias -ServerAddress ($myObject.DNS1,$myObject.DNS2) | |
} else { | |
Set-DnsClientServerAddress -InterfaceAlias $alias -ServerAddress $myObject.DNS1 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment