# Rewrited for PowerShell 7 with the help of ChatGPH param( [Parameter(Mandatory)] [string]$VMName ) function ConvertTo-SubnetMask { param( [string]$PrefixLength ) if ($PrefixLength -notmatch '^\d+$' -or [int]$PrefixLength -lt 0 -or [int]$PrefixLength -gt 32) { return $PrefixLength } $mask = [uint32]::MaxValue -shl (32 - [int]$PrefixLength) $bytes = [BitConverter]::GetBytes($mask) [Array]::Reverse($bytes) return ($bytes -join '.') } $vm = Get-VM -Name $VMName -ErrorAction Stop if ($vm.State -ne 'Running') { Write-Host "VM $VMName is not Running." exit 1 } $ns = "root\virtualization\v2" # Virtual System Management Service $vsvms = Get-CimInstance ` -Namespace $ns ` -ClassName Msvm_VirtualSystemManagementService # VM $vmcs = Get-CimInstance ` -Namespace $ns ` -ClassName Msvm_ComputerSystem ` -Filter "ElementName='$VMName'" if (-not $vmcs) { Write-Host "Error: VM $VMName is missing." exit 1 } # Msvm_ComputerSystem # -> Msvm_VirtualSystemSettingData $vs_settingdata = Get-CimAssociatedInstance ` -InputObject $vmcs ` -Association Msvm_SettingsDefineState ` -ResultClassName Msvm_VirtualSystemSettingData # Msvm_VirtualSystemSettingData # -> Msvm_SyntheticEthernetPortSettingData $et_settingdata = Get-CimAssociatedInstance ` -InputObject $vs_settingdata ` -ResultClassName Msvm_SyntheticEthernetPortSettingData # Msvm_SyntheticEthernetPortSettingData # -> Msvm_GuestNetworkAdapterConfiguration $vm_netconf = Get-CimAssociatedInstance ` -InputObject $et_settingdata ` -Association Msvm_SettingDataComponent ` -ResultClassName Msvm_GuestNetworkAdapterConfiguration $yesno = Read-Host "Enable DHCP? (y/n[n])" if ($yesno.ToLower() -eq "y") { $vm_netconf.PSBase.CimInstanceProperties["DHCPEnabled"].Value = $true } else { $vm_netconf.PSBase.CimInstanceProperties["DHCPEnabled"].Value = $false $vm_netconf.PSBase.CimInstanceProperties["IPAddresses"].Value = @((Read-Host "Specify static ipv4 address")) $vm_netconf.PSBase.CimInstanceProperties["Subnets"].Value = @(ConvertTo-SubnetMask((Read-Host "Specify subnetmask"))) $vm_netconf.PSBase.CimInstanceProperties["DefaultGateways"].Value = @((Read-Host "Specify default gateway")) $vm_netconf.PSBase.CimInstanceProperties["DNSServers"].Value = @((Read-Host "Specify preferred dns server")) } # CIMインスタンスを embedded instance 文字列に変換 $cimSerializer = [Microsoft.Management.Infrastructure.Serialization.CimSerializer]::Create() $serialized = $cimSerializer.Serialize( $vm_netconf, [Microsoft.Management.Infrastructure.Serialization.InstanceSerializationOptions]::None ) $networkConfiguration = [System.Text.Encoding]::Unicode.GetString($serialized) # 設定をVMへ適用 $ret = Invoke-CimMethod ` -InputObject $vsvms ` -MethodName SetGuestNetworkAdapterConfiguration ` -Arguments @{ ComputerSystem = $vmcs NetworkConfiguration = @($networkConfiguration) } if ($ret.ReturnValue -ne 0) { Write-Host "Error: something went wrong. ReturnValue=$($ret.ReturnValue)" } else { Write-Host "Success" Get-CimInstance -Namespace $ns -ClassName Msvm_GuestNetworkAdapterConfiguration | Where { $_.InstanceID -like "*$($vm.vmId)*" }|Select DHCPEnabled,IPAddresses,Subnets,DefaultGateways,DNSServers }