-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03IPNAMECONF.ps1
93 lines (73 loc) · 3.52 KB
/
03IPNAMECONF.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
$VMConfigurations = @{}
# Load the configuration from config.json
$configFilePath = ".\\config.json"
if (-Not (Test-Path $configFilePath)) {
Write-Error "Configuration file not found at $configFilePath. Please ensure itW exists."
exit
}
$config = Get-Content -Path $configFilePath | ConvertFrom-Json
# Extract the LocalPassword from the configuration
$localPassword = $config.LocalPassword
$secureLocalPassword = ConvertTo-SecureString $localPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("Administrator", $secureLocalPassword)
function Convert-SubnetMaskToPrefixLength {
param ([string]$SubnetMask)
$binarySubnet = ($SubnetMask -split '\.' | ForEach-Object { [convert]::ToString([int]$_, 2).PadLeft(8, '0') })
return ($binarySubnet -join '' -split '1').Length - 1
}
function Set-VMStaticIPAndRename {
param (
[string]$VMName,
[string]$NewComputerName,
[string]$IPAddress,
[string]$Subnet,
[string]$Gateway,
[string[]]$DNS
)
try {
Start-Sleep -Seconds 30
# Remote execution to set IP and rename computer
Invoke-Command -VMName $VMName -Credential $credential -ScriptBlock {
param ($NewComputerName, $IPAddress, $Subnet, $Gateway, $DNS)
# Rename the computer
Write-Host "Renaming computer to $NewComputerName..."
Rename-Computer -NewName $NewComputerName -Force
# Configure static IP
Write-Host "Configuring static IP $IPAddress..."
$interface = Get-NetAdapter | Where-Object { $_.Status -eq "Up" }
if (!$interface) {
throw "No active network adapter found."
}
$existingIP = Get-NetIPAddress -InterfaceIndex $interface.IfIndex | Where-Object { $_.IPAddress -eq $IPAddress }
if ($existingIP) {
Write-Host "IP Address $IPAddress already exists. Skipping configuration."
} else {
# Convert subnet to prefix length
function Convert-SubnetMaskToPrefixLength {
param ([string]$SubnetMask)
$binarySubnet = ($SubnetMask -split '\.' | ForEach-Object { [convert]::ToString([int]$_, 2).PadLeft(8, '0') })
return ($binarySubnet -join '' -split '1').Length - 1
}
$prefixLength = Convert-SubnetMaskToPrefixLength -SubnetMask $Subnet
New-NetIPAddress -IPAddress $IPAddress -PrefixLength $prefixLength -DefaultGateway $Gateway -InterfaceIndex $interface.IfIndex
Set-DnsClientServerAddress -InterfaceIndex $interface.IfIndex -ServerAddresses $DNS
}
# Restart the computer to apply the new name
Write-Host "Restarting the computer to apply changes..."
Restart-Computer -Force
} -ArgumentList $NewComputerName, $IPAddress, $Subnet, $Gateway, $DNS
} catch {
Write-Error "Failed to configure $VMName : $_"
}
}
# Loop through configurations and rename computers while setting static IP
foreach ($VMName in $VMConfigurations.Keys) {
$config = $VMConfigurations[$VMName]
Set-VMStaticIPAndRename -VMName $VMName `
-NewComputerName $VMName `
-IPAddress $config.IPAddress `
-Subnet $config.Subnet `
-Gateway $config.Gateway `
-DNS $config.DNS
}
Write-Host "Static IP and computer name configuration complete."