Created
September 16, 2021 01:11
-
-
Save Torxsmind/ed11ce491c7bc86847c2abf70c5ea8f2 to your computer and use it in GitHub Desktop.
Script to grab a NIC IP information, then create a secondary script to set the config back
This file contains hidden or 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
| ### Set Common Parameters ### | |
| $now = Get-Date -UFormat "%Y-%m-%d_%H-%M-%S" | |
| $extNIC = "Ethernet" | |
| $extSM = "255.255.255.0" | |
| $outpath = "c:\temp\" | |
| $outscriptname = "setIP_postwork.ps1" | |
| $outfile = $outpath + $outscriptname | |
| $CheckFilePath = Test-Path $outfile | |
| ## Get the current IPv4 info for external NIC ## | |
| $NetIPextnic = (Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias $extnic) | |
| $NetIPextnicIndex = $NetIPextnic.InterfaceIndex | |
| $ipv4_extnic = $NetIPextnic.IPAddress | |
| ## Get Subnetmask ## | |
| $NetIPextnicPrefix = $NetIPextnic.PrefixLength | |
| ## Get the current default gateway of the external NIC ## | |
| $gateway_extnic = (Get-NetRoute -DestinationPrefix "0.0.0.0/0" -InterfaceIndex $NetIPextnicIndex) | |
| $gw_extnic = $gateway_extnic.nexthop | |
| ## Get the DNS Server Addresses of the external NIC ## | |
| $DnsPri = (Get-DnsClientServerAddress -InterfaceIndex $NetIPextnicIndex -AddressFamily IPv4).ServerAddresses[0] | |
| $DnsSec = (Get-DnsClientServerAddress -InterfaceIndex $NetIPextnicIndex -AddressFamily IPv4).ServerAddresses[1] | |
| $Dns = $DnsPri + "," + $DnsSec | |
| ## Check if setIP_Postwork.ps1 exists, if so rename it ## | |
| If ($CheckFilePath -eq $True) { | |
| Rename-Item -Path $outfile -NewName ($now + $outscriptname) | |
| } | |
| ## Create / Write Post Script ## | |
| Add-Content $outfile "### Set Common Parameters ###" | |
| Add-Content $outfile ("$" + "ExtNicAlias = ""$extNIC""") | |
| Add-Content $outfile ("$" + "ExtNicIndex = ""$NetIPextnicIndex""") | |
| Add-Content $outfile ("$" + "ExtNic_IPv4 = ""$ipv4_extnic""") | |
| Add-Content $outfile ("$" + "ExtNic_mask = ""$NetIPextnicPrefix""") | |
| Add-Content $outfile ("$" + "ExtNic_gw = ""$gw_extnic""") | |
| Add-Content $outfile ("$" + "ExtNic_dns = ""$Dns""") | |
| Add-Content $outfile "### Set IP address on External NIC ###" | |
| Add-Content $outfile ("New-NetIPAddress -InterfaceIndex " + "$" + "ExtNicIndex" + " -IPAddress " + "$" + "ExtNic_IPv4" + " -AddressFamily IPv4 -DefaultGateway " + "$" + "ExtNic_gw" + " -PrefixLength " + "$" + "ExtNic_mask") | |
| Add-Content $outfile "### Set DNS Server Address on External NIC ###" | |
| Add-Content $outfile ("Set-DnsClientServerAddress -InterfaceIndex " + "$" + "ExtNicIndex -ServerAddresses " + "$" + "ExtNic_dns") | |
| Add-Content $outfile "### Disable IPv6 on External NIC ###" | |
| Add-Content $outfile ("Disable-NetAdapterBinding –InterfaceAlias " + "$" + "ExtNicAlias –ComponentID ms_tcpip6") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment