-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIP Change Script.ps1
86 lines (69 loc) · 3.86 KB
/
IP Change Script.ps1
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
<#
This scripts purpose is to change IPs when configuring a VM for Private networking.
Under normal conditions, it should allow the selection of an Ethernet adapter, and change the IP and DNS for it
Made by Edwin Brett, Please reach out via https://Edwinbrett.com if you have any issues.
#>
Write-Host " IP Change Script
===================" -ForegroundColor Cyan
Write-Host "Searching for Adapters..." -ForegroundColor Yellow
try{
$pattern = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
$subnetPattern = '\b([0-9]|[1-2][0-9]|3[0-2])\b'
$Adapters = Get-NetAdapter
if($null -ne $interface){
foreach ($interface in $Adapters) {
Write-Host "$($interface.Name) found"
$response = Read-Host "Is this the adpater you want to work with? : Y or N"
if($response -eq 'Y' -or $response -eq 'y'){
Write-Host "We will work on this adapter:" $interface.Name
do {
$ipaddress = Read-Host "What would you like to set the IP to?"
$gateway = Read-Host "What would you like to set the gateway to?"
$dnsserver = Read-Host "What would you like to set the DNS to?"
$subnetmask = Read-Host "What would you like to set the Subnet mask to? Please submit the CIDR (/25, /32, etc.) with out the slash, aka '25'"
$ipCheck = $ipaddress -match $pattern
if ($ipCheck -eq $false) {
Write-Warning ("'{0}' is not a valid IP Address." -f $ipaddress)
}
$gatewayCheck = $gateway -match $pattern
if ($gatewayCheck -eq $false) {
Write-Warning ("'{0}' is not a valid Gateway Address." -f $gateway)
}
$DNSCheck = $dnsserver -match $pattern
if ($DNSCheck -eq $false) {
Write-Warning ("'{0}' is not a valid DNS Address." -f $dnsserver)
}
$subnetCheck = $subnetmask -match $subnetPattern
if($subnetCheck -eq $false){
Write-Warning ("'{0}' is not a valid Subnet Mask." -f $subnetmask)
}
} until ( $ipCheck -and $gatewayCheck -and $DNSCheck -and $subnetPattern)
Write-Host "Setting the Interface up now"
Remove-NetIPAddress -AddressFamily IPv4 -Confirm:$false -ErrorAction SilentlyContinue
Remove-NetRoute -AddressFamily IPv4 -Confirm:$false -ErrorAction SilentlyContinue
New-NetIPAddress -IPAddress $ipaddress -InterfaceAlias $($interface.ifAlias) -PrefixLength $subnetmask -DefaultGateway $gateway -AddressFamily IPv4
Start-Sleep -Seconds 2.5
Set-DnsClientServerAddress -ServerAddresses $dnsserver -InterfaceAlias $($interface.ifAlias)
Start-Sleep -Seconds 2.5
Write-Host "Ping Test to 1.1.1.1"
ping 1.1.1.1
break;
} elseif ($response -eq 'N' -or $response -eq 'n') {
Write-Host "Checking for Another Adapter"
} else{
Write-Host "LEARN TO READ"
exit;
}
}
Write-Host "There are no other adapters. Closing script"
} catch {
$exception = $_.Exception
while ($null -ne $exception.InnerException){
$exception = $exception.InnerException
}
$exception | Format-List * -Force
Write-Host "ERROR" $exception
}
}finally {
Write-Host "-----------------------------------JOB COMPLETED-------------------------------------" -ForegroundColor Green
}