Skip to content

Instantly share code, notes, and snippets.

@andrewbbrown
Last active January 29, 2018 03:51
Show Gist options
  • Save andrewbbrown/e37b890ba2819e35647567515cac87e3 to your computer and use it in GitHub Desktop.
Save andrewbbrown/e37b890ba2819e35647567515cac87e3 to your computer and use it in GitHub Desktop.
try {
# Boxstarter options
$Boxstarter.RebootOk=$true
$Boxstarter.NoPassword=$false # Is this a machine with no logon password?
$Boxstarter.AutoLogin=$true
# Rename Computer
Write-BoxstarterMessage "Ensuring Server is named correctly"
$ServerName = "HL1-ISCSI-01"
$HostName = $($env:computername).ToUpper()
if ($HostName -ne $ServerName) { Rename-Computer -NewName $ServerName}
if (Test-PendingReboot) { Invoke-Reboot }
Write-BoxstarterMessage "Ensuring iSCSI Target Server Feature is installed."
Get-WindowsFeature FS-iSCSITarget-Server | Install-WindowsFeature -IncludeAllSubFeature -IncludeManagementTools
if (Test-PendingReboot) { Invoke-Reboot }
Write-BoxstarterMessage "Ensuring Virtual Disk exists."
if(-not(Get-IscsiVirtualDisk -Path "C:\iSCSIDisks\ESXIVirtual4Disk1.vhdx" -ErrorAction SilentlyContinue))
{
New-IscsiVirtualDisk -Path "C:\iSCSIDisks\ESXIVirtualDisk1.vhdx" -SizeBytes 2TB
}
Write-BoxstarterMessage "Ensuring iSCSI Targets Exist."
if(-not(Get-IscsiServerTarget -TargetName "ESXIVirtualDisk1" -ErrorAction SilentlyContinue))
{
New-IscsiServerTarget -TargetName "ESXIVirtualDisk1" -InitiatorIds @("iqn:initior01", "iqn:initior02")
}
Write-BoxstarterMessage "Ensuring iSCSI Targets are mapped to disk correctly."
#Since there is no Get-IscsiVirtualDiskTargetMapping cmdlet, this will check if the map exists and if not create it.
if(-not(Get-IscsiServerTarget -Path "C:\iSCSIDisks\ESXIVirtualDisk1.vhdx" -ErrorAction SilentlyContinue | where {$_.TargetName -eq "ESXIVirtualDisk1"} -ErrorAction SilentlyContinue))
{
#Map iscsi disk to target
Add-IscsiVirtualDiskTargetMapping -TargetName "ESXIVirtualDisk1" -Path "C:\iSCSIDisks\ESXIVirtualDisk1.vhdx"
}
Function Set-IPAddress {
param (
[string]$Name = "Ethernet0",
[IPAddress]$IP = "10.67.36.30",
[string] $CIDR = 24, # This means subnet mask = 255.255.255.0,
[string]$Gateway = "10.67.36.10",
[string]$Dns = "10.67.36.4",
[string]$IPType = "IPv4",
[string]$Type = "Static",
[string]$NewName = "Management"
)
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | ? {$_.Name -eq $Name}
if ($Type -eq "Static") {
# 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 $Gateway
# Configure the DNS client server IP addresses
Write-Host "Configuring new gateway"
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
}
else {
$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
}
}
Write-Host "Restarting adapter"
$adapter | Restart-NetAdapter
$adapter | Rename-NetAdapter -NewName $NewName
$adapter = Get-NetAdapter | ? {$_.Name -eq $NewName}
$adapter | Restart-NetAdapter
}
Write-BoxstarterMessage "Ensuring IP Address is correct"
Set-IPAddress -Name "Ethernet0" -NewName "Management" -IP "10.67.36.30" -Gateway "10.67.36.10" -Dns "10.67.36.4" -Type "Static" -IPType "IPv4"
Write-BoxstarterMessage "Machine is complete!"
}
catch {
Write-ChocolateyFailure 'Boxstarter Error: ' $($_.Exception.Message)
throw
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment