Vbscript to powershell

Welcome Forums General PowerShell Q&A Vbscript to powershell

This topic contains 4 replies, has 3 voices, and was last updated by

 
Member
5 years, 1 month ago.

  • Author
    Posts
  • #11482

    Participant
    Points: 0
    Rank: Member

    how do i convert the below VB to powershell

    i got below commands
    gwmi win32_networkadapter -Filter 'netconnectionid = "pwc lan port1"' | select speed

    but im not sure how to get duplex ,speed, ip address , these 3 values
    I want to try to get these values for multiple servers at a time with the powershell

  • #11484

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    Use "select *" instead to see all available properties.

    You can provide multiple server names to the command:

    Get-WmiObject -Class Win32_NetworkAdapter -ComputerName SERVER1,SERVER2,SERVER3 -Filter "NetConnectionID = 'PWC LAN PORT1'" |
    Select-Object -Property *
    

    But IP address is not a property of the NetworkAdapter. It is a property of the NetworkAdapterConfiguration; look up the Win32_NetworkAdapterConfiguration class. That's because a given adapter can have multiple configurations, each with one or more IP addresses. That is how WMI has always worked; it isn't related to VBS or PowerShell.

  • #11485

    Member
    Points: 0
    Rank: Member

    Unless you have a good reason to, don't pipe to Select-Object like that. You lose access to a lot of functionality and information that way.

    Also, apparently the duplex information isn't exposed by WMI (see Richard's blog post on the subject here.)

    Ignoring duplex mode for now, here's one way you could output an object containing speed and IP addresses:

    $adapter = Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionID = "pwc lan port1"'
    
    if ($null -ne $adapter)
    {
        $config = $adapter.GetRelated('Win32_NetworkAdapterConfiguration')
    
        New-Object psobject -Property @{
            Speed = $adapter.Speed
            IPAddress = $config.IPAddress
        }
    }
    
  • #11488

    Participant
    Points: 0
    Rank: Member

    Thanks a lot Don and Dave. this really helps a lot.
    Im going to test and incorporate them right away

  • #11489

    Member
    Points: 0
    Rank: Member

    That’s because a given adapter can have multiple configurations, each with one or more IP addresses. That is how WMI has always worked; it isn’t related to VBS or PowerShell.

    That's a good point, and I didn't account for that in my sample code. Here's a version that's probably going to work better if there are multiple configuration instances for the adapter:

    $adapter = Get-WmiObject Win32_NetworkAdapter -Filter 'NetConnectionID = "pwc lan port1"'
    
    if ($null -ne $adapter)
    {
        $IPAddress = New-Object System.Collections.ArrayList
    
        foreach ($config in $adapter.GetRelated('Win32_NetworkAdapterConfiguration'))
        {
            $IPAddress.AddRange($config.IPAddress)
        }
    
        New-Object psobject -Property @{
            Speed = $adapter.Speed
            IPAddress = $IPAddress.ToArray()
        }
    }
    

The topic ‘Vbscript to powershell’ is closed to new replies.