Initial VM Configurations
Once VMs are built and the OS is installed, there are some common things that need to be done all of them - renaming the system, setting a static, IP, correcting the time zone, etc. This is again a great task for some PowerShell.
# References -
# https://www.pdq.com/blog/how-to-use-powershell-to-set-static-and-dhcp-ip-addresses/
# https://www.thewindowsclub.com/enable-remote-desktop-using-command-line
#Set Variables per device
$ComputerName = "DC01"
$IP = "xxx.xxx.xxx.xxx"
$Domain = "domainname"
#These shouldn't need to change
$MaskBits = 24 # This CIDR notation means subnet mask = 255.255.255.0
$Gateway = "xxx.xxx.xxx.xxx"
$Dns = "xxx.xxx.xxx.xxx"
$IPType = "IPv4"
#Rename Computer
Rename-Computer -NewName $ComputerName
#Configure Static IP
# Retrieve the network adapter that you want to configure
$adapter = Get-NetAdapter | Where-Object {$_.Status -eq "up"}
# Remove any existing IP, gateway from our ipv4 adapter
If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) {
$adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false
}
If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) {
$adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false
}
# Configure the IP address and default gateway
$adapter | New-NetIPAddress `
-AddressFamily $IPType `
-IPAddress $IP `
-PrefixLength $MaskBits `
-DefaultGateway $Gateway
# Configure the DNS client server IP addresses
$adapter | Set-DnsClientServerAddress -ServerAddresses $DNS
# Set Time Zone
Set-TimeZone “Eastern Standard Time”
# Validate Server and if so disable IEESC
$ComputerInfo = Get-ComputerInfo
If ($ComputerInfo.OSProductType -eq 'Server')
{
$AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0 -Force}
#Enable Remote Desktop
Set-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Terminal Server'-name "fDenyTSConnections" -Value 0
Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
#Add Devices to domain (Assuming the Domain Controller has been configured)
Add-Computer -DomainName $Domain -Restart