#Requires -Version 3.0 -Modules NetAdapter # Example: # - to disable ICS # Set-ICS -VMSwitch minikube # - to enable ICS (and create the internal VM switch if it doesn't exist yet) # Set-ICS -VMSwitch minikube Enabled # Purposes: # - Create the VM switch if it doesn't exist # - Share the internet with the VM Switch using ICS (Internet Connection Sharing) # Outcomes: # - ICS deploys its own DHCP server within the internal network connected to the VM switch # - Each VM will be given an IP address associated to a hostname defined as .mshome.net # - IP addresses and associated hostnames are written to C:\Windows\System32\drivers\etc\hosts.ics param ( [Parameter(Mandatory)] [string]$VMSwitch, [switch]$Enabled ) regsvr32.exe /s hnetcfg.dll $pHNetShare = New-Object -ComObject HNetCfg.HNetShare try { # always disable the sharing first, Internet Connection Sharing doesn't support well a reboot and doesn't support more than one sharing $pHNetShare.EnumEveryConnection | ForEach-Object { $sConnectionName = $pHNetShare.NetConnectionProps.Invoke($_).Name $pSharingConfiguration = $pHNetShare.INetSharingConfigurationForINetConnection.Invoke($_) if ($pSharingConfiguration.SharingEnabled) { $pSharingConfiguration.DisableSharing() Write-Host "Disabled pre-existing ICS on '$sConnectionName'." } } if ($Enabled) { # get the internet connection name $pInternetConnection = Get-NetConnectionProfile | Where-Object {$_.IPv4Connectivity -eq 'Internet'} if ($pInternetConnection -eq $null) { Write-Warning -Message "Could not find any NetConnectionProfile currently connected to the internet, please connect to the internet." Exit 1 } $sInternetConnectionName = $pInternetConnection.InterfaceAlias # get the internet connection configuration object $pInternetConnection = $pHNetShare.EnumEveryConnection | Where-Object {$pHNetShare.NetConnectionProps.Invoke($_).Name -eq "$sInternetConnectionName"} $pInternetSharingConfiguration = $pHNetShare.INetSharingConfigurationForINetConnection.Invoke($pInternetConnection) # get the VM switch, create it if it doesn't exist $pVMSwitchConnection = $pHNetShare.EnumEveryConnection | Where-Object {$pHNetShare.NetConnectionProps.Invoke($_).Name -like "*$VMSwitch*"} if ($pVMSwitchConnection -eq $null) { # create a new internal VM switch if it doesn't exist yet try { New-VMSwitch -Name $VMSwitch -SwitchType Internal } catch { Write-Warning -Message "Could not create the VM switch $VMSwitch. Create with powershell 'New-VMSwitch -Name $VMSwitch' -SwitchType Internal." Exit 1 } $pVMSwitchConnection = $pHNetShare.EnumEveryConnection | Where-Object {$pHNetShare.NetConnectionProps.Invoke($_).Name -like "*$VMSwitch*"} if ($pVMSwitchConnection -eq $null) { Write-Warning -Message "Could not find a NetConnection matching VM switch $VMSwitch. Create with powershell 'New-VMSwitch -Name $VMSwitch' -SwitchType Internal." Exit 1 } Write-Host "Created internal VM Switch '$VMSwitch'." } # get the VM switch configuration object if ($pVMSwitchConnection.Count -ne 1) { Write-Warning -Message "Found too many NetConnection matching the VM switch $VMSwitch. Remove with powershell 'Remove-VMSwitch -Name $VMSwitch'." Exit 1 } $pVMSwitchSharingConfiguration = $pHNetShare.INetSharingConfigurationForINetConnection.Invoke($pVMSwitchConnection) $pInternetSharingConfiguration.EnableSharing(0) $pVMSwitchSharingConfiguration.EnableSharing(1) Write-Host "Shared internet from connection '$sInternetConnectionName' to VM Switch '$VMSwitch'." } } catch { Write-Warning -Message "An unexpected error has occurred!" Throw }