#Requires -Version 5 #Requires -RunAsAdministrator [Cmdletbinding()] Param ( [switch]$EnableInternetConnectionSharing, [switch]$DisableInternetConnectionSharing, [switch]$SetPwnagotchiSubnet, [ipaddress]$ScopeAddress = '192.168.137.1', # Default value, can be overridden [string]$SubnetPrefix = '192.168.137.' # Default prefix, can be overridden ) # Load helper functions Function Create-HNetObjects { <# .SYNOPSIS A helper function that does the heavy lifting with NetCfg.HNetShare #> [Cmdletbinding()] Param ( $InternetAdaptor = $(Select-NetAdaptor -Message "Please select your main ethernet adaptor with internet access that will be used for internet sharing."), $RNDISGadget = $(Select-NetAdaptor -Message "Please select your 'USB Ethernet/RNDIS Gadget' adaptor") ) Begin { regsvr32.exe /s hnetcfg.dll $HNetShare = New-Object -ComObject HNetCfg.HNetShare } Process { if ($HNetShare.EnumEveryConnection -ne $null) { $InternetInt = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($InternetAdaptor).Name } $InternetIntConfig = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($InternetInt) $RNDISInt = $HNetShare.EnumEveryConnection | Where-Object { $HNetShare.NetConnectionProps.Invoke($_).Name -eq ($RNDISGadget).Name } $RNDISIntConfig = $HNetShare.INetSharingConfigurationForINetConnection.Invoke($RNDISInt) } } End { Return $(New-Object -TypeName PSObject -Property @{InternetIntConfig=$InternetIntConfig;RNDISIntConfig=$RNDISIntConfig;}) } } Function Enable-InternetConnectionSharing { <# .SYNOPSIS Enables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface. #> [Cmdletbinding()] $HNetObject = Create-HNetObjects $HNetObject.InternetIntConfig.EnableSharing(0) $HNetObject.RNDISIntConfig.EnableSharing(1) Write-Output "[x] Enabled Internet Connection Sharing." } Function Disable-InternetConnectionSharing { <# .SYNOPSIS Disables internet connection sharing between the 'main' uplink interface and the 'USB Ethernet/RNDIS Gadget' interface. #> [Cmdletbinding()] $HNetObject = $(Create-HNetObjects) $HNetObject.InternetIntConfig.DisableSharing() $HNetObject.RNDISIntConfig.DisableSharing() Write-Output "[x] Disabled Internet Connection Sharing." } Function Test-PwnagotchiSubnet { <# .SYNOPSIS Tests the registry for the correct ScopeAddress. #> [Cmdletbinding()] Try { $RegKeys = Get-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters -ErrorAction Stop If ($RegKeys.ScopeAddress -notlike "$SubnetPrefix*") { Write-Error "Internet Connection Sharing is not set to the expected subnet. Run Set-PwnagotchiSubnet with the correct parameters." -ErrorAction Stop } } Catch { Write-Error "Error accessing registry to check ICS subnet: $_" -ErrorAction Stop } } Function Set-PwnagotchiSubnet { <# .SYNOPSIS Set the registry for the correct ScopeAddress. #> [Cmdletbinding()] Param ( [ipaddress]$ScopeAddress ) Try { [void]([ipaddress]$ScopeAddress) [void]([byte[]] $ScopeAddress.split('.')) } Catch { Write-Error "$ScopeAddress is not a valid IP." } Try { Set-ItemProperty -Name ScopeAddress -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop Set-ItemProperty -Name ScopeAddressBackup -Path "HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\" -Value $ScopeAddress -ErrorAction Stop Write-Warning "The Internet Connection Sharing subnet has been updated. A reboot of windows is required !" } Catch { $PSCmdlet.ThrowTerminatingError($PSItem) } } # Main Function Function Setup-PwnagotchiNetwork { <# .SYNOPSIS Function to setup networking. #> Param ( [switch]$EnableInternetConnectionSharing, [switch]$DisableInternetConnectionSharing, [switch]$SetPwnagotchiSubnet, $ScopeAddress = '192.168.2.1' # Default to 192.168.2.1 for AT&T compatibility ) Begin { Try { Write-Debug "Begin" $ErrorSplat=@{ErrorAction="stop"} Write-Debug "Testing subnet" Try { Test-PwnagotchiSubnet @ErrorSplat } Catch { If ($SetPwnagotchiSubnet) { Write-Debug "Setting subnet" Set-PwnagotchiSubnet -ScopeAddress $ScopeAddress @ErrorSplat } Else { Write-Error "Internet Connection Sharing is not set to the expected subnet. Run this script with the -SetPwnagotchiSubnet to setup the network." -ErrorAction Stop } } } Catch { $PSCmdlet.ThrowTerminatingError($PSItem) } } Process { Write-Debug "Process" Try { If ($EnableInternetConnectionSharing) { Write-Debug "Enable network Sharing" Enable-InternetConnectionSharing @ErrorSplat } ElseIf ($DisableInternetConnectionSharing) { Write-Debug "Disable network Sharing" Disable-InternetConnectionSharing @ErrorSplat } } Catch { $PSCmdlet.ThrowTerminatingError($PSItem) } } End { Write-Debug "End" Try { # Nothing to return. } Catch { $PSCmdlet.ThrowTerminatingError($PSItem) } } } Function Select-NetAdaptor { <# .SYNOPSIS A menu function to select the correct network adaptors. #> Param ( $Message ) $Adaptors = Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected'} | Sort-Object LinkSpeed -Descending do { Write-Host $Message $index = 1 foreach ($Adaptor in $Adaptors) { Write-Host "[$index] $($Adaptor.Name), $($Adaptor.InterfaceDescription)" $index++ } $Selection = Read-Host "Number" } until ($Adaptors[$selection-1]) Return $Adaptors[$selection-1] } # Dynamically create params for Setup-PwnagotchiNetwork function based on script input. Setup-PwnagotchiNetwork @psBoundParameters