如何通过WMI读取physicalhostname registrykey [英] How do I read the physicalhostname registrykey via WMI

查看:117
本文介绍了如何通过WMI读取physicalhostname registrykey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读

SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters\PhysicalHostName

注册表项以获取Hyper的主机名V VM。



我可以连接并通过程序的其他部分通过wmi检索很多信息,但是如果要阅读注册表密钥我会发现:(



我知道有一种方法可以通过/ virtualization / namespace检索所有vm名称的名称。但是这不需要我需要,因为我必须直接定位vms。该代码不必在vb.net中我也可以翻译c#。



其他调试信息:

isConnected状态ManagementScope是真的

OutParams(sValue)什么都不返回



如果有人可以帮我解决这个问题会很好xx!



我的尝试:



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
                'Connect to the specified computer registry
                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

                'Retrieve the required value from the registry

                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

推荐答案

尝试使用 Microsoft.Win32 命名空间中的注册表类:

Try using the registry classes in the Microsoft.Win32 namespace:
Using hive = RegistryKey.OpenRemoveBaseKey(RegistryHive.LocalMachine, m_ComputerName, RegistryView.Default)
    Using key = hive.OpenSubKey("SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters", False)
        If key IsNot Nothing Then
            strStringValue = CStr(key.GetValue("PhysicalHostName"))
        End If
    End Using
End Using



RegistryKey Class | Microsoft Docs [ ^ ]


由于似乎没有人愿意回答我关闭了这个问题。到目前为止我没有找到我的错误。我可以阅读所有的regKeys,但我想读的那个。
Since nobody seems to be willing to answer i close the question. I didnt found my error so far. I can read all regKeys but the one i want to read.


这篇关于如何通过WMI读取physicalhostname registrykey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆