Skip to content

Instantly share code, notes, and snippets.

@47star
Created April 5, 2019 15:23
Show Gist options
  • Save 47star/95b9a28aadea8a1448e19af59fa8460e to your computer and use it in GitHub Desktop.
Save 47star/95b9a28aadea8a1448e19af59fa8460e to your computer and use it in GitHub Desktop.
Assign static IP address for Hyper-V Guest.
param( [String]$VMName="",
[String]$Address="",
[String]$Gateway="",
[String]$SubnetMask="",
[String]$DNS1="",
[String]$DNS2="")
$Msvm_VirtualSystemManagementService = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_VirtualSystemManagementService
$Msvm_ComputerSystem = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$VMName'"
$Msvm_VirtualSystemSettingData = ($Msvm_ComputerSystem.GetRelated("Msvm_VirtualSystemSettingData", "Msvm_SettingsDefineState", $null, $null, "SettingData", "ManagedElement", $false, $null) | % {$_})
$Msvm_SyntheticEthernetPortSettingData = $Msvm_VirtualSystemSettingData.GetRelated("Msvm_SyntheticEthernetPortSettingData")
$Msvm_GuestNetworkAdapterConfiguration = ($Msvm_SyntheticEthernetPortSettingData.GetRelated("Msvm_GuestNetworkAdapterConfiguration", "Msvm_SettingDataComponent", $null, $null, "PartComponent", "GroupComponent", $false, $null) | % {$_})
$Msvm_GuestNetworkAdapterConfiguration.DHCPEnabled = $false
$Msvm_GuestNetworkAdapterConfiguration.IPAddresses = @($Address)
$Msvm_GuestNetworkAdapterConfiguration.Subnets = @($SubnetMask)
$Msvm_GuestNetworkAdapterConfiguration.DefaultGateways = @($Gateway)
$Msvm_GuestNetworkAdapterConfiguration.DNSServers = @($DNS1, $DNS2)
$Msvm_VirtualSystemManagementService.SetGuestNetworkAdapterConfiguration($Msvm_ComputerSystem.Path, $Msvm_GuestNetworkAdapterConfiguration.GetText(1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment