none
Difference in WMI between Powershell and .Net. Can I get the sourceCode for the Powershell Method !

    Domanda

  • In Poweshell below:
    get-wmiobject -Query "SELECT * FROM Win32_PingStatus WHERE Address='ComputerName'"

    gives me:
     IPV4Address      IPV6Address

    But when asking the same question from VB.net
    The only Ip-address I can get comes from the ProtocolAddress.

    Below the code in VB.Net which returns the IP:

    Friend Function PingStatus(computer As String) As String
        Dim searcher As New ManagementObjectSearcher("SELECT ProtocolAddress FROM Win32_Pingstatus WHERE Address = '" & computer & "'")
        For Each wmiObj As ManagementObject In searcher.Get()
            For Each prop As PropertyData In wmiObj.Properties
                If prop.Name.ToString.ToUpper = "PROTOCOLADDRESS" Then Return wmiObj(prop.Name.ToString).ToString
            Next
        Next
        Return ""
    End Function


    My problem is that I want the IPV4-address but I get the IPV6-address (when it is an Windows 7).
    And I cant use powershell because it is running on a client where powershell perhaps isn't enabled.

    So I want to see the sourcecode from Powershell to see how they have done their Enhancement of the WMI-method.
    Is that possible ?
    Or does anybody have another solution or a can suggest another forum?

    (I must point out that i Know of "ping -4" but it is to slow and to messy)

    Regards/Lars

     

    domenica 24 giugno 2012 14:08

Risposte

  • You could use WMIC, which is installed on all Windows computers:

    wmic NICCONFIG WHERE IPEnabled=true GET IPAddress


    Grant Ward, a.k.a. Bigteddy

    • Contrassegnato come risposta Lars mercoledì 27 giugno 2012 14:38
    domenica 24 giugno 2012 14:29

Tutte le risposte

  • You could use WMIC, which is installed on all Windows computers:

    wmic NICCONFIG WHERE IPEnabled=true GET IPAddress


    Grant Ward, a.k.a. Bigteddy

    • Contrassegnato come risposta Lars mercoledì 27 giugno 2012 14:38
    domenica 24 giugno 2012 14:29
  • The source code is proprietry, not open source, so you can forget that angle.  Otherwise ask in the VB.net forum:

    http://social.msdn.microsoft.com/Forums/en/vbgeneral/threads


    Grant Ward, a.k.a. Bigteddy

    domenica 24 giugno 2012 14:36
  • Thanx for replies !

    I tried to use wmic in a triptrap-solution (don't know if that is an english word :-)
    1) First I see if the address I got - via the PingStausfunction -  is an ipV6-address
    2) Then I use your command with a little addition:

    wmic NICCONFIG WHERE IPEnabled=true GET IPAddress | Find /I "FE80::2AA:FF:FE9A:4CA2"

    (The ipaddress above isn't real or at least not mine ...)

    BTW I found a little "bug" in powershell.
    The result for the IPV6-address from powershell was not my computers IPV6-addres but that of an WMWare !!

    Perhaps this bug is because I am connected via VPN to my work when i tested this.
    Actually the WMI-function (accessed from .Net) gives me only the ComputerName and not the IP-address.

    I have to test this on my workplace without VPN .

     

    I have to try wnic-example on the clients to see if it can be used in production.

    I will wait a little to see if I can get any more answers !

    Regards/Lars

    domenica 24 giugno 2012 15:31
  • Well, source code is there, on this script property.... :)

    There is no magic here, result of this query was modified to make output of Test-Connection more ping-like.

    get-wmiobject -Query "SELECT * FROM Win32_PingStatus WHERE Address='DC'" | 
        Get-Member -MemberType ScriptProperty |
        select -ExpandProperty Definition
    See? Code at your disposal. ;) Probably also explains why it lies at times... :)
    martedì 26 giugno 2012 06:57
  • get-wmiobject -Query "SELECT * FROM Win32_PingStatus WHERE Address='DC'" | 
        Get-Member -MemberType ScriptProperty |
        select -ExpandProperty Definition
    See? Code at your disposal. ;) Probably also explains why it lies at times... :)

    Thanks Bartek !

    I tried below at Work :

    get-wmiobject -Query "SELECT * FROM Win32_PingStatus WHERE Address='computerName'"
    

    And this time it worked ! But ofcourse with an IPV6-twist - the IPV6Address ended in a %11

    I found the reason behind %11 from a Mark Minasi post:

    http://www.minasi.com/forum/topic.asp?TOPIC_ID=35033

    The FE80-address is not garanteed to be unique. So the %11 represents the NIC.

    Well I think that the best solutiion is with the WMIC-commando .

    Thanks for answers !

    /Lars

    mercoledì 27 giugno 2012 14:38