
Script per configurare la scheda di rete in DHCP o con IP statico. Permette di scegliere quale scheda di rete configurare.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
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.