Here is a fun script snippet that is possible with Windows 10:
functionaddNicWithIP([string]$VMName,$cred,[string]$Switch,[string]$IPaddress,[string]$subnetPrefixLength){
$newNetAdapter=Add-VMNetworkAdapter-VMName$VMName-SwitchName$Switch-Passthru
Write-Output"[$($VMName)]:: Wait for IP address and create virtual switch"
waitForPSDirect$VMName$cred
icm-VMName$VMName-Credential$cred {
param($IPAddress,$subnetPrefixLength,$MacAddress)
# Wait for the NIC to appear
do {sleep1} until (@(get-netadapter|?PermanentAddress-eq$MacAddress).Count -eq1)
# Setup IP Address
New-NetIPAddress-IPAddress$IPAddress `
-InterfaceAlias (get-netadapter|?PermanentAddress-eq$MacAddress).Name `
-PrefixLength$subnetPrefixLength
}-ArgumentList$IPAddress,$subnetPrefixLength,$newNetAdapter.MacAddress
}
What this does is:
- Adds a new network adapter to a running virtual machine
- Finds the new network adapter inside the virtual machine
- Assigns a static IP address to the new network adapter
You can call it like this:
addNicWithIP-VMName"PowerShell Container"-cred$localCred-Switch"Virtual Switch"-IPaddress"192.168.10.10"-subnetPrefixLength"24"
Cheers,
Ben