Vbscript to powershell

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years, 5 months ago.

  • Author
    Posts
  • #11482

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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

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

  • #11489
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    [quote=11484]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.
    [/quote]

    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()
        }
    }
    

You must be logged in to reply to this topic.