<# .SYNOPSIS Name: IPTray.ps1 Tool for changing IPv4 settings quickly. .DESCRIPTION Administrator tool for changing IPv4 settings from the system tray and managing different configuration profiles. .NOTES Updated: N/A Release Date: 2019-08-25 Author: Michael Bazdell #> #Requires -RunAsAdministrator $netAdapters = Get-NetAdapter $netIPs = Get-NetIPAddress $regPath = 'HKCU:\Software\IP Tray' # Create default config registry entries $netAdapters | ForEach-Object { $ifindex = $_.ifIndex $configPath = "$regPath\nic$ifindex\config00" # Only create entries that don't already exist if (!(Test-Path "$configPath")) { $adapterIP = Get-NetIPConfiguration -InterfaceIndex $ifIndex New-Item -Path "$regPath\nic$ifindex" -Name "config00" -Force New-ItemProperty -Path $configPath -PropertyType string -Name "Name" -Value $_.InterfaceAlias New-ItemProperty -Path $configPath -PropertyType string -Name "IP" -Value ($adapterIP).IPv4Address New-ItemProperty -Path $configPath -PropertyType string -Name "Gateway" -Value ($adapterIP).IPv4DefaultGateway.NextHop New-ItemProperty -Path $configPath -PropertyType string -Name "Prefix" -Value ($netIPs | Where-Object {($_.InterfaceIndex -eq $ifIndex) -and ($_.AddressFamily -eq "IPv4")}).PrefixLength New-ItemProperty -Path $configPath -PropertyType string -Name "DNS" -Value (Get-DnsClientServerAddress -InterfaceIndex $_.ifIndex -AddressFamily IPv4).ServerAddresses if ($_.NetIPv4Interface.DHCP -eq $Enabled) { New-ItemProperty -Path $configPath -PropertyType dword -Name "DHCP" -Value 1 } else { New-ItemProperty -Path $configPath -PropertyType dword -Name "DHCP" -Value 0 } } } # Add assemblies for WPF Add-Type -AssemblyName PresentationFramework Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing Add-Type -AssemblyName WindowsFormsIntegration [System.Windows.Forms.Application]::EnableVisualStyles() # Choose an icon to display in the systray $icon = [System.Drawing.Icon]::ExtractAssociatedIcon("C:\Windows\System32\telnet.exe") # Add the systray icon $Main_Tool_Icon = New-Object System.Windows.Forms.NotifyIcon $Main_Tool_Icon.Text = "IP Tray" $Main_Tool_Icon.Icon = $icon $Main_Tool_Icon.Visible = $true # Build the ContextMenu object $Main_Tool_Icon.ContextMenu = New-Object System.Windows.Forms.ContextMenu # Menu entries $netAdapters | ForEach-Object { # $ifindex is used a lot $ifindex = $_.ifIndex # Adapters $nicButton = New-Object System.Windows.Forms.MenuItem $nicButton.Text = "$($_.interfacealias) - " + ($netIPs | Where-Object {($_.InterfaceIndex -eq $ifindex) -and ($_.AddressFamily -eq "IPv4")}).IPAddress # Configs # Get the available configs for the adapter from the registry $configsReady = Get-ChildItem "HKCU:\Software\IP Tray\nic$ifindex" $configsReady | ForEach-Object { $reg = $_ | Get-ItemProperty $configButton = New-Object System.Windows.Forms.MenuItem $configButton.Text = $reg.Name # Add config option to the adapter's menu $nicButton.MenuItems.Add($configButton) # Apply the selected config $changeIP = { if ($reg.DHCP -eq 1) { Set-NetIPInterface -InterfaceIndex $ifindex -Dhcp Enabled ipconfig /renew } else { # If the config isn't using DHCP then set the required variables $newIP = @{ IPAddress = $reg.IP PrefixLength = $reg.Prefix DefaultGateway = $reg.Gateway InterfaceIndex = $ifindex AddressFamily = "IPv4" } $newDNS = @{ ServerAddresses = $reg.DNS InterfaceIndex = $ifindex } Set-NetIPInterface -InterfaceIndex $ifindex -Dhcp Disabled $netIPs | Where-Object ($_.InterfaceIndex -eq $ifindex) | Remove-NetRoute $netIPs | Where-Object ($_.InterfaceIndex -eq $ifindex) | Remove-NetIPAddress New-NetIPAddress @newIP Set-DnsClientServerAddress @newDNS } } # Assign the actions to the button $configButton.add_Click($changeIP.GetNewClosure()) } # Edit config window from POSHGUI $editWindow = { $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = '600,245' $Form.text = "nic$ifindex" $Form.TopMost = $false $Name = New-Object system.Windows.Forms.Label $Name.text = "Name" $Name.AutoSize = $true $Name.width = 25 $Name.height = 10 $Name.location = New-Object System.Drawing.Point(225,25) $Name.Font = 'Microsoft Sans Serif,14' $IP = New-Object system.Windows.Forms.Label $IP.text = "IP" $IP.AutoSize = $true $IP.width = 25 $IP.height = 10 $IP.location = New-Object System.Drawing.Point(225,60) $IP.Font = 'Microsoft Sans Serif,14' $Gateway = New-Object system.Windows.Forms.Label $Gateway.text = "Gateway" $Gateway.AutoSize = $true $Gateway.width = 25 $Gateway.height = 10 $Gateway.location = New-Object System.Drawing.Point(225,130) $Gateway.Font = 'Microsoft Sans Serif,14' $DNS = New-Object system.Windows.Forms.Label $DNS.text = "DNS" $DNS.AutoSize = $true $DNS.width = 25 $DNS.height = 10 $DNS.location = New-Object System.Drawing.Point(225,165) $DNS.Font = 'Microsoft Sans Serif,14' $Prefix = New-Object system.Windows.Forms.Label $Prefix.text = "Prefix" $Prefix.AutoSize = $true $Prefix.width = 25 $Prefix.height = 10 $Prefix.location = New-Object System.Drawing.Point(225,95) $Prefix.Font = 'Microsoft Sans Serif,14' $DHCP = New-Object system.Windows.Forms.Label $DHCP.text = "DHCP" $DHCP.AutoSize = $true $DHCP.width = 25 $DHCP.height = 10 $DHCP.location = New-Object System.Drawing.Point(225,200) $DHCP.Font = 'Microsoft Sans Serif,14' $DHCPcheck = New-Object system.Windows.Forms.CheckBox $DHCPcheck.AutoSize = $false $DHCPcheck.width = 20 $DHCPcheck.height = 20 $DHCPcheck.location = New-Object System.Drawing.Point(329,210) $DHCPcheck.Font = 'Microsoft Sans Serif,14' $NameBox = New-Object system.Windows.Forms.TextBox $NameBox.multiline = $false $NameBox.width = 271 $NameBox.height = 20 $NameBox.location = New-Object System.Drawing.Point(316,25) $NameBox.Font = 'Microsoft Sans Serif,14' $IPBox = New-Object system.Windows.Forms.TextBox $IPBox.multiline = $false $IPBox.width = 271 $IPBox.height = 20 $IPBox.location = New-Object System.Drawing.Point(316,60) $IPBox.Font = 'Microsoft Sans Serif,14' $PrefixBox = New-Object system.Windows.Forms.TextBox $PrefixBox.multiline = $false $PrefixBox.width = 271 $PrefixBox.height = 20 $PrefixBox.location = New-Object System.Drawing.Point(316,95) $PrefixBox.Font = 'Microsoft Sans Serif,14' $GatewayBox = New-Object system.Windows.Forms.TextBox $GatewayBox.multiline = $false $GatewayBox.width = 271 $GatewayBox.height = 20 $GatewayBox.location = New-Object System.Drawing.Point(316,130) $GatewayBox.Font = 'Microsoft Sans Serif,14' $DNSBox = New-Object system.Windows.Forms.TextBox $DNSBox.multiline = $false $DNSBox.width = 271 $DNSBox.height = 20 $DNSBox.location = New-Object System.Drawing.Point(316,165) $DNSBox.Font = 'Microsoft Sans Serif,14' $Configs = New-Object system.Windows.Forms.ListBox $Configs.BackColor = "#ffffff" $Configs.text = "Configs" $Configs.width = 192 $Configs.height = 225 $Configs.location = New-Object System.Drawing.Point(10,5) $Save = New-Object system.Windows.Forms.Button $Save.text = "Save" $Save.width = 60 $Save.height = 30 $Save.location = New-Object System.Drawing.Point(400,205) $Save.Font = 'Microsoft Sans Serif,10' $Delete = New-Object system.Windows.Forms.Button $Delete.text = "Delete" $Delete.width = 60 $Delete.height = 30 $Delete.location = New-Object System.Drawing.Point(500,205) $Delete.Font = 'Microsoft Sans Serif,10' $Form.controls.AddRange(@($Name,$IP,$Gateway,$DNS,$Prefix,$DHCP,$DHCPcheck,$NameBox,$IPBox,$PrefixBox,$GatewayBox,$DNSBox,$Configs,$Save,$Delete)) # Add available configs to the edit window function updateConfigList { $Configs.Items.Clear() (Get-ChildItem "$regPath\$($Form.Text)" | Get-ItemProperty).Name | ForEach-Object { $Configs.Items.Add("$_") } if ($Configs.Items.IndexOf("New...") -lt 0 ) { $Configs.Items.Add("New...") } } updateConfigList $Configs.SetSelected(0,$true) # Config list functionality $Configs.add_SelectedIndexChanged({ $configsReady = Get-ChildItem "HKCU:\Software\IP Tray\nic$ifindex" $reg = $configsReady | Get-ItemProperty | Where-Object { $_.Name -eq $Configs.selecteditems } $NameBox.Text = $reg.Name $IPBox.Text = $reg.IP $GatewayBox.Text = $reg.Gateway $DNSBox.Text = $reg.DNS $PrefixBox.Text = $reg.Prefix if ($reg.DHCP -eq 1) { $DHCPcheck.Checked = $true } else { $DHCPcheck.Checked = $false } }) $Save.add_Click({ if ($Configs.selecteditems -match "New...") { # New configs # Get the next available config folder number $i = (($configsReady).Name | ForEach-Object {[int]( $_ -split "(\d\d$)" )[1]} | Measure-Object -Maximum).Maximum $i++ $i = "{0:d2}" -f [int]$i $reg = "$regPath\$($Form.Text)\config$i" New-Item -Path ($reg|Split-Path) -Name "config$i" -Force New-ItemProperty -Path $reg -PropertyType string -Name "Name" -Value $NameBox.Text New-ItemProperty -Path $reg -PropertyType string -Name "IP" -Value $IPBox.Text New-ItemProperty -Path $reg -PropertyType string -Name "Prefix" -Value $PrefixBox.Text New-ItemProperty -Path $reg -PropertyType string -Name "DNS" -Value $DNSBox.Text New-ItemProperty -Path $reg -PropertyType string -Name "Gateway" -Value $GatewayBox.Text if ($DHCPcheck.Checked) { New-ItemProperty -Path $reg -PropertyType dword -Name "DHCP" -Value 1 } else { New-ItemProperty -Path $reg -PropertyType dword -Name "DHCP" -Value 0 } } else { # Updating an existing config $configsReady = Get-ChildItem "HKCU:\Software\IP Tray\nic$ifindex" $reg = $configsReady | Where-Object {($_ | Get-ItemProperty).Name -eq $Configs.selecteditems } Set-ItemProperty -Path Registry::$reg -Name "Name" -Value $NameBox.Text Set-ItemProperty -Path Registry::$reg -Name "IP" -Value $IPBox.Text Set-ItemProperty -Path Registry::$reg -Name "Prefix" -Value $PrefixBox.Text Set-ItemProperty -Path Registry::$reg -Name "DNS" -Value $DNSBox.Text Set-ItemProperty -Path Registry::$reg -Name "Gateway" -Value $GatewayBox.Text if ($DHCPcheck.Checked) { Set-ItemProperty -Path Registry::$reg -Name "DHCP" -Value 1 } else { Set-ItemProperty -Path Registry::$reg -Name "DHCP" -Value 0 } } updateConfigList $Configs.SetSelected(($Configs.Items.IndexOf($($NameBox.Text))),$true) }) $Delete.add_Click({ $reg = $configsReady | Where-Object {($_ | Get-ItemProperty).Name -eq $Configs.selecteditems } $Confirm = [System.Windows.MessageBox]::Show("Delete $($Configs.selecteditems)?",'Delete Confirmation','YesNo','Warning') switch ($Confirm) { 'Yes' { Remove-Item -Path Registry::$reg -Force } } updateConfigList }) [void]$Form.ShowDialog() } # Change button $changeButton = New-Object System.Windows.Forms.MenuItem $changeButton.Text = "Change..." # Action for Change button $changeButton.add_Click($editWindow.GetNewClosure()) # Add buttons $nicButton.MenuItems.Add($changeButton) $Main_Tool_Icon.contextMenu.MenuItems.AddRange($nicButton) } # Exit button $exitButton = New-Object System.Windows.Forms.MenuItem $exitButton.Text = "Exit" $Main_Tool_Icon.contextMenu.MenuItems.AddRange($exitButton) $exitButton.add_Click({ $Main_Tool_Icon.Visible = $false $window.Close() Stop-Process $pid }) # Make PowerShell Disappear - Thanks Chrissy $windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);' $asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru $null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0) # Use a Garbage colection to reduce Memory RAM # https://dmitrysotnikov.wordpress.com/2012/02/24/freeing-up-memory-in-powershell-using-garbage-collector/ # https://docs.microsoft.com/fr-fr/dotnet/api/system.gc.collect?view=netframework-4.7.2 [System.GC]::Collect() # Create an application context for it to all run within - Thanks Chrissy # This helps with responsiveness, especially when clicking Exit - Thanks Chrissy $appContext = New-Object System.Windows.Forms.ApplicationContext [void][System.Windows.Forms.Application]::Run($appContext)