-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure_ip_address.ps1
95 lines (84 loc) · 11 KB
/
configure_ip_address.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
87
88
89
90
91
92
93
94
95
. "lib\init.ps1"
. "lib\finalize.ps1"
Initialize
foreach ($vm in $new_vms)
{
Write-Host "======================================================"
Write-Host ">>> $vm IP configuration started ..."
$ip_address = (nslookup -q=A $vm $pfsense_router | Select-String Address | Select -Last 1).ToString().Split(' ')[-1]
Write-Host ">>> $vm IP Address: $ip_address"
$ip_octets = $ip_address.Split('.')
$ip_gateway = $ip_octets[0] + '.' + $ip_octets[1] + '.' + $ip_octets[2] + '.' + '1'
Write-Host ">>> $vm IP Gateway: $ip_gateway"
if ($os_type -like "*window*")
{
$interface = (Invoke-VMScript -VM $vm -ScriptType PowerShell -ScriptText "(gwmi Win32_NetworkAdapter -filter 'netconnectionid is not null').netconnectionid" -GuestUser template -GuestPassword Password1).ScriptOutput.Trim()
Write-Host ">>> $vm Network Interface: $interface"
$set_ip_cmd = "c:\windows\system32\netsh.exe interface ip set address ""$interface"" static $ip_address $mgmt_netmask $ip_gateway"
$set_dns_cmd = "c:\windows\system32\netsh.exe interface ip set dnsservers ""$interface"" static $dns_server"
# *** -GuestUser & -GuestPassword this values need to replaced or get from ENV and set in init.ps1 library ***
Invoke-VMScript -VM $vm -ScriptType bat -ScriptText $set_ip_cmd -GuestUser <guesuser> -GuestPassword <guespassword>
Invoke-VMScript -VM $vm -ScriptType bat -ScriptText $set_dns_cmd -GuestUser <guesuser> -GuestPassword <guespassword>
}
elseif ($os_type -like "*centos*")
{
$guestOsUser = "root"
$guestOsPassword = "guestuserpassword" # update the guest os root password
$interface = (Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText 'ls /etc/sysconfig/network-scripts/ | grep ifcfg | grep -v lo | head -1' -GuestUser $guestOsUser -GuestPassword $guestOsPassword).ScriptOutput.Trim()
$intf_file = '/etc/sysconfig/network-scripts/' + $interface
Write-Host ">>> $vm Network Interface File: $intf_file"
Write-Host ">>> $vm Set BOOTPROTO: static"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "sed -i -r -e 's/BOOTPROTO=.*/BOOTPROTO\=static/g' $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm Set ONBOOT: yes"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "sed -i -r -e 's/ONBOOT=.*/ONBOOT\=yes/g' $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
$has_ipaddr = (Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "grep IPADDR $intf_file" -GuestUser $guestOsUser -GuestPassword ironport).ScriptOutput.Trim()
if ($has_ipaddr)
{
Write-Host ">>> $vm already has IP ADDRESS configured"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "sed -i -r -e 's/IPADDR=.*/IPADDR\=$ip_address/g' $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm IP ADDRESS updated"
}
else
{
Write-Host ">>> $vm does not have IP ADDRESS configured"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "echo IPADDR=$ip_address >> $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm IP ADDRESS added"
}
$has_netmask = (Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "grep NETMASK $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword).ScriptOutput.Trim()
if ($has_netmask)
{
Write-Host ">>> $vm already has NETMASK configured"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "sed -i -r -e 's/NETMASK=.*/NETMASK\=$mgmt_netmask/g' $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm NETMASK updated"
}
else
{
Write-Host ">>> $vm does not have NETMASK configured"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "echo NETMASK=$mgmt_netmask >> $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm NETMASK added"
}
$has_gateway = (Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "grep GATEWAY $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword).ScriptOutput.Trim()
if ($has_gateway)
{
Write-Host ">>> $vm already has GATEWAY configured"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "sed -i -r -e 's/GATEWAY=.*/GATEWAY\=$ip_gateway/g' $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm GATEWAY updated"
}
else
{
Write-Host ">>> $vm does not have GATEWAY configured"
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "echo GATEWAY=$ip_gateway >> $intf_file" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm GATEWAY added"
}
Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText "echo $vm > /etc/hostname" -GuestUser $guestOsUser -GuestPassword $guestOsPassword
Write-Host ">>> $vm HOSTNAME updated"
Write-Host ">>> $vm Reboot initiated"
Restart-VMGuest -VM $vm -Confirm:$false
Start-Sleep 10
Write-Host ">>> $vm Waiting for VMWare Tools to respond"
Wait-Tools -VM $vm -TimeoutSeconds 300
}
Write-Host ">>> $vm IP configuration completed"
Write-Host "======================================================\n\n"
}
Finalize