I need to read the
SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters\PhysicalHostName
Registry Key to get the Host Name of a Hyper V VM.
I can connect and also retrieve alot of information trough wmi in other parts of the program, but i stumble if it comes to reading a registry Key :(
Im aware that theres a way to retrieve the names of all vm-names through the /virtualization/ namespace. However thats not want i need since i have to target the vms directly. The Code doesn't have to be in vb.net i can also translate c#.
Additional Debugging Information:
The isConnected state of the ManagementScope is true
The OutParams("sValue") returns nothing
It would be sooo nice if someone could help me in this matter x.x !
What I have tried:
Dim strStringValue As String = ""
Dim objManagementScope As ManagementScope
Dim objManagementClass As ManagementClass
Dim objManagementBaseObject As ManagementBaseObject
Dim intRegistryHive As Integer = 80000002
Dim strSubKeyName As String = "SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters"
Dim strValueName As String = "PhysicalHostName"
strSubKeyName = strSubKeyName.Trim
strValueName = strValueName.Trim
If (strSubKeyName.Length > 0) Then
objManagementScope = New ManagementScope
With objManagementScope
With .Path
.Server = m_ComputerName
.NamespacePath = "root\default"
End With
With .Options
.EnablePrivileges = True
.Impersonation = ImpersonationLevel.Impersonate
.Username = frmMain.txtUserName.Text
.Password = frmMain.txtPassword.Text
End With
.Connect()
End With
If objManagementScope.IsConnected Then
objManagementClass = New ManagementClass("StdRegProv")
With objManagementClass
.Scope = objManagementScope
objManagementBaseObject = .GetMethodParameters("GetStringValue")
With objManagementBaseObject
.SetPropertyValue("hDefKey", CType("&H" & Hex(intRegistryHive), Long))
.SetPropertyValue("sSubKeyName", strSubKeyName)
.SetPropertyValue("sValueName", strValueName)
End With
Dim OutParams As ManagementBaseObject = .InvokeMethod("GetStringValue", objManagementBaseObject, Nothing)
strStringValue = CType(OutParams("sValue"), String)
If strStringValue = Nothing Then strStringValue = ""
End With
End If
End If
Return strStringValue
Catch ex As Exception
Return ""
End Try
The problem is probably somewhere in
If objManagementScope.IsConnected Then
objManagementClass = New ManagementClass("StdRegProv")
With objManagementClass
.Scope = objManagementScope
objManagementBaseObject = .GetMethodParameters("GetStringValue")
With objManagementBaseObject
.SetPropertyValue("hDefKey", CType("&H" & Hex(intRegistryHive), Long))
.SetPropertyValue("sSubKeyName", strSubKeyName)
.SetPropertyValue("sValueName", strValueName)
End With
Dim OutParams As ManagementBaseObject = .InvokeMethod("GetStringValue", objManagementBaseObject, Nothing)
Dim tst As Object = OutParams("sValue")
strStringValue = CType(OutParams("sValue"), String)
If strStringValue = Nothing Then strStringValue = ""
End With
End If