
Script per configurare la scheda di rete in DHCP o con IP statico. Permette di scegliere quale scheda di rete configurare.
function show-menu-DHCP-or-static
{
cls
Write-Host "Press '1' DHCP"
Write-Host "Press '2' Static"
}
function DHCP {
$IPtype = "IPv4"
$interface = $adapter | Get-NetIPInterface -AddressFamily $IPtype
If ($interface.Dhcp -eq "Disabled") {
# Remove existing gateway
Write-Host "Removing existing gateway"
If (($interface | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$interface | Remove-NetRoute -Confirm:$false
}
# Enable DHCP
Write-Host "Enabling DHCP on interface"
$interface | Set-NetIPInterface -DHCP Enabled
# Configure the DNS Servers automatically
Write-Host "Enabling automatic DNS"
$interface | Set-DnsClientServerAddress -ResetServerAddresses
}
}
function Static-IP {
#Variable
$IP = Read-Host "IP Address"
$CIDR = Read-Host "CIDR"
$GW = Read-Host "Gateway"
$DNS = Read-Host "DNS"
$IPtype = "IPv4"
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
Write-Host "Removing existing IP"
$adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
Write-Host "Removing existing gateway"
$adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
# Configure the IP address and default gateway
Write-Host "Configuring new IP"
$adapter | New-NetIPAddress -AddressFamily $IPType -IPAddress $IP -PrefixLength $CIDR -DefaultGateway $GW
# Configure the DNS client server IP addresses
Write-Host "Configuring new gateway"
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
}
#Selection NIC
$nic = Get-NetAdapter | select name
For ($i = 0; $i -lt $nic.Length; $i++){
$str = ($i + 1).ToString();
$str = $str + " - ";
$str = $str + $nic[$i].name;
Write-Host($str);
}
$response = Read-Host "Type the network number";
$SelectedNic = $nic[$response-1].name;
$adapter = Get-NetAdapter | Where-Object {$_.Name -eq $SelectedNic}
###
#DHCP or Static
show-menu-DHCP-or-static
$Input = Read-Host "Please make a selection"
switch ($input) {
'1' {
$Type = 'DHCP'
DHCP
}
'2' {
$Type = 'Static'
Static-IP
}
}
Disclaimer: nessuna garanzia è fornita con questo script. Lo scrivente non è responsabile di qualunque danno possa eventualmente derivarne – lo utilizzate a vostro rischio.
Liked this post? Follow this blog to get more.