This repository contains a set of PowerShell scripts and configuration files designed to automate the creation and configuration of a Hyper-V virtual machine (VM) environment. The scripts set up a Windows 11 VM connected to an internal virtual switch with NAT configured on the host for internet access. Additionally, it includes post-installation scripts for configuring networking within the VM, including installing certificates and setting proxy settings.
- Automated Hyper-V VM Setup with NAT and Proxy Configuration
-
Operating System: Windows 10 or Windows Server with Hyper-V installed and enabled.
-
Permissions: Administrative privileges to execute PowerShell scripts and modify system settings.
-
PowerShell Execution Policy: Set to allow script execution. You can set it using:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
-
Golden Image VHDX: A Windows 11 VHDX file located at
C:\temp\HyperVSandbox\VHDX\Win11_23H2.vhdx
or modify the path in1_VMs.psd1
. -
Certificate File: A certificate file to be installed on the VM, placed at
C:\CertStore\<CERT-NAME>.cer
. -
Proxy Server Details: Proxy server address and port to be configured in the VM.
The automation process involves:
-
Configuring the Host Network:
- Creating an internal virtual switch named
Internal
. - Assigning an IP address to the host's virtual network adapter.
- Configuring NAT on the host to allow VMs on the internal network to access the internet.
- Creating an internal virtual switch named
-
Creating and Configuring the VM:
- Defining VM settings in
1_VMs.psd1
. - Setting up unattended installation settings in
2_UnattendSettings.psd1
. - Executing post-installation scripts defined in
3_PostInstallScripts.psd1
.
- Defining VM settings in
-
Running Post-Installation Scripts on the VM:
- Updating Windows.
- Installing certificates.
- Configuring proxy settings.
Defines the VM configuration, including:
- VM name and path.
- Memory and processor count.
- Network adapters connected to the internal switch.
Contains unattended installation settings for the VM, such as:
- Computer name.
- Localization settings.
- Static IP configuration.
Lists the post-installation scripts to be executed on the VM in order.
Stores host network configuration settings, including:
- Virtual switch name.
- Interface alias.
- IP addressing for the host's virtual adapter.
- NAT configuration.
A PowerShell script that:
- Creates the internal virtual switch.
- Assigns IP addresses to the host's virtual adapter.
- Configures NAT for internet access.
A script executed on the VM to:
- Search for and install Windows updates.
- Log the update process.
A script executed on the VM to:
- Install a certificate.
- Configure proxy settings for all users.
- Log the configuration process.
Run the Configure-HostNetwork.ps1
script to set up the host's network configuration.
# Open PowerShell as Administrator
.\Configure-HostNetwork.ps1
This script will:
- Create the internal virtual switch if it doesn't exist.
- Assign the specified IP address to the host's virtual network adapter.
- Configure NAT to allow internet access from the internal network.
Edit the 1_VMs.psd1
file to define your VM settings.
@{
'VM0' = @{
vmName = "Sandbox"
vmPath = ""
GoldenImagePath = "C:\temp\HyperVSandbox\VHDX\Win11_23H2.vhdx"
vmMemory = 4GB
vmGeneration = 2
vmProcCount = 2
vmAutomaticStopAction = "ShutDown"
vmNics = @{
"aMGMT" = @{"Switch" = "Internal"; "VLANID" = "" }
}
vmDataDisks = @()
}
}
Ensure that:
- The
GoldenImagePath
points to your Windows 11 VHDX file. - The network adapter is connected only to the internal switch.
Edit the 2_UnattendSettings.psd1
file to set up unattended installation parameters.
@{
VM0 = @{
ComputerName = 'Sandbox'
Organization = 'myavd'
Owner = 'myavd'
Timezone = 'W. Europe Standard Time'
InputLocale = 'de-DE'
SystemLocale = 'en-US'
UserLocale = 'en-US'
IPAddress = "192.168.0.10"
IPMask = "24"
IPGateway = "192.168.0.1"
DNSIP = "8.8.8.8"
}
}
Make sure:
- The
IPAddress
does not conflict with the host's IP. - The
IPGateway
is set to the host's virtual adapter IP (192.168.0.1
). - DNS servers are reachable.
Edit 3_PostInstallScripts.psd1
to specify scripts to run after the VM is set up.
@{
VM0 = @{
vmPostInstallSteps = @(
@{
stepHeadline = 'Step0 - TimeStamp'
scriptFilePath = 'step_AddDateTimeToLog.ps1'
requiresRestart = $false
}
@{
stepHeadline = 'Step1 - WindowsUpdate'
scriptFilePath = 'step_DoWindowsUpdates.ps1'
requiresRestart = $true
}
@{
stepHeadline = 'Step2 - ConfigureVMNetworking'
scriptFilePath = 'step_ConfigureVMNetworking.ps1'
requiresRestart = $true
}
)
}
}
Run your VM deployment script or process that uses the above configuration files to create and configure the VM. Ensure that the scripts step_DoWindowsUpdates.ps1
and step_ConfigureVMNetworking.ps1
are accessible to the VM during post-installation.
Automates the creation of the internal virtual switch and configures NAT on the host to provide internet access to VMs connected to the internal network.
Defines the VM's hardware settings and network configuration for creation in Hyper-V.
Provides settings for an unattended Windows installation, automating the setup of the operating system within the VM.
Lists the scripts to be executed inside the VM after the OS installation, specifying the order and whether a restart is required.
Contains configuration data for the host network settings used by Configure-HostNetwork.ps1
.
Executed within the VM to search for, download, and install all available Windows updates, logging the process.
Executed within the VM to:
- Install a specified certificate into the local machine's root certificate store.
- Configure system-wide proxy settings for all users.
- Log the actions taken.
-
Administrative Privileges: Ensure all scripts are run with administrative privileges to allow for system configuration changes.
-
Execution Policy: If you encounter issues running scripts due to execution policies, adjust the policy using:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
-
Certificate Installation:
- Place your certificate file at
C:\CertStore\<CERT-NAME>.cer
. - Replace
<CERT-NAME>
instep_ConfigureVMNetworking.ps1
with your actual certificate file name.
- Place your certificate file at
-
Proxy Configuration:
- Update the
$proxyServer
variable instep_ConfigureVMNetworking.ps1
with your actual proxy server details. - Ensure the proxy settings are compatible with your network environment.
- Update the
-
VM Network Connectivity:
- If the VM cannot access the internet, verify that:
- The NAT configuration on the host is correct.
- The VM's network settings are properly configured.
- Firewall settings are not blocking traffic.
- If the VM cannot access the internet, verify that:
-
Logging:
- Logs for post-installation scripts are stored in
C:\temp
within the VM. - Review these logs to troubleshoot any issues with script execution.
- Logs for post-installation scripts are stored in
-
Order of Execution:
- Steps in
3_PostInstallScripts.psd1
are executed based on thestepHeadline
in alphabetical order. - Adjust numbering (
Step0
,Step1
,Step2
, etc.) to control execution order.
- Steps in
-
Testing:
- Before deploying to production environments, test the entire setup in a controlled environment to ensure all scripts perform as expected.
This project is licensed under the MIT License - see the LICENSE file for details.
Disclaimer: Use these scripts at your own risk. Always ensure you have backups and have tested the scripts in a non-production environment before deploying.