none
Network order query using powershell RRS feed

  • Question

  • Hi, 

    I have written a powershell script to query NIC binding order and the below script will look NIC binding order in the format Vethernet,storage if the order is correct then it will print output in green color else it will print in yellow color.

    I have added one more condition here,In few machines there are no storage NIC. Hence, if there is no storage NIC found in the binding order it should see Vethernet  NIC is the first  in order and if correct it should print as green or else it should be yellow.But there it gives me problem.It is not printing proper color when the order is correct and not correct.

    Can anyone help me correcting this script ?

    $results = Invoke-Command -ComputerName $computername {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").Bind | ForEach-Object {
      $guid = ($_ -split '\\')[2]
      Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE GUID='$guid'" |
        Select-Object -ExpandProperty NetConnectionID
    }}
    $adapteresult = $results -join","
    
    if (($adapteresult -contains '*storage*') -and ($adapteresult -match 'vEthernet\s+\([^)]+\),storage')) 
    
    {
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    
    elseif (($adapteresult -notcontains '*storage*') -and ($adapteresult -match 'vEthernet\s+\([^)]+\)'))
    
    {
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    else
    {
    Add-Content $report "<td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td>"
    }

    • Edited by Shimith Tuesday, November 7, 2017 12:19 PM
    • Moved by Bill_Stewart Thursday, January 25, 2018 10:41 PM Unanswerable drive-by question
    Tuesday, November 7, 2017 6:30 AM

All replies

  • Hi,

    The -Contains operator doesn't do substring comparisons and the match must be on a complete string and is used to search collections.

    As per the documentation

    -Contains
    Description: Containment operator. Tells whether a collection of reference values includes a single test value.

    Examples:

    PS C:\> "abc", "def" -Contains "def"
    True
    
    PS C:\> "Windows", "PowerShell" -Contains "Shell"
    False  #Not an exact match

    The best thing will be using contains in this format.

    $adapteresult.Contains('Storage')

    Dont use any wildcard '*'. 

    (or)

    $adapteresult.Contains('Storage') -eq $true
    
    $adapteresult.Contains('Storage') -eq $false

    Try it.

    Please upvote if it helped you 


    Naveen Basati


    • Edited by BASATI Wednesday, November 8, 2017 6:58 AM
    Wednesday, November 8, 2017 6:57 AM
  • I tried . but it's not helped me
    Wednesday, November 8, 2017 10:29 AM
  • Could you please post the $results output and what is the content available in $report, so I can recheck the code 

    Basati

    Wednesday, November 8, 2017 10:41 AM
  • here is the output from two servers.one server have storage NIC and one server dont have storage NIC

    PS C:\> $results
    vEthernet (Intel(R) Ethernet Controller X540-AT2 #2 - Virtual Switch)
    Storage
    Ethernet
    Ethernet 4
    Ethernet 3

    PS C:\> $results
    vEthernet (10.0.0.1)
    Ethernet
    Ethernet 2
    Ethernet

    PS C:\> $report
    .\HVReport.htm


    What I am trying to check here is, if NIC binding order is Vethernet,storage like that then display the results with green color.Even though storage nic available then the order is not proper then it should display it in yellow color.

    Also the second server doesnt have stoareg NIC, in that case it should only check Vethernet is first in order, if that is correct it should print the result in green color else it should print in yellow color


    Currently my script display the results, but it doesn't change the color as we needed.
    • Edited by Shimith Wednesday, November 8, 2017 11:10 AM
    Wednesday, November 8, 2017 11:09 AM
  • Thanks for the information.

    Try this code now

    $results = Invoke-Command -ComputerName $computername {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").Bind | ForEach-Object {
      $guid = ($_ -split '\\')[2]
      Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE GUID='$guid'" |
        Select-Object -ExpandProperty NetConnectionID
    }}
    $adapteresult = $results -join","
    
    if (($adapteresult.Contains('Storage') -and ($adapteresult -match 'vEthernet\s+\([^)]+\),storage')) )
    
    {
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File .\HVReport.htm
    #Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    
    elseif (($adapteresult.contains('Storage') -eq $false) -and ($adapteresult -match 'vEthernet\s+\([^)]+\)'))
    
    {
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File .\HVReport.htm
    #Add-Content $report  -body "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    else
    {
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File .\HVReport.htm
    #Add-Content $report "<td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td>"
    }


    Basati

    Upvote if this helped you
    • Edited by BASATI Wednesday, November 8, 2017 11:41 AM
    Wednesday, November 8, 2017 11:41 AM
  • the color always come as same(green) even if the order is correct and not correct.
    Thursday, November 9, 2017 6:09 AM
  • Ist case : I have hard coded values of $results

    $results :

    vEthernet (Intel(R) Ethernet Controller X540-AT2 #2 - Virtual Switch)
    Storage
    Ethernet
    Ethernet 4
    Ethernet 3

    $results = 'vEthernet (Intel(R) Ethernet Controller X540-AT2 #2 - Virtual Switch)','Storage','Ethernet 4','Ethernet','Ethernet 3'
    $adapteresult = $results -join","
    $adapteresult
    $s2 = $adapteresult -match 'vEthernet\s+\([^)]+\),storage'
    $s2
    $s3 = $adapteresult -match 'vEthernet\s+\([^)]+\)'
    $s3
    if (($adapteresult.Contains('Storage') -and ($adapteresult -match 'vEthernet\s+\([^)]+\),storage')) )
    
    {
    $s = 0
    
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File D:\1.html
    #Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    
    elseif (($adapteresult.contains('Storage') -eq $false) -and ($adapteresult -match 'vEthernet\s+\([^)]+\)'))
    
    {
    $s = 1
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File D:\1.html
    #Add-Content $report  -body "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    else
    {
    $s = 2
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File D:\1.html
    #Add-Content $report "<td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td>"
    }
    $s
    D:\1.html
    Output : Yellow color is coming 

    II case

    $results:

    vEthernet (10.0.0.1)
    Ethernet
    Ethernet 2
    Ethernet

    $results = 'vEthernet (10.0.0.1)','Ethernet','Ethernet 2','Ethernet'
    $adapteresult = $results -join","
    $adapteresult
    $s2 = $adapteresult -match 'vEthernet\s+\([^)]+\),storage'
    $s2
    $s3 = $adapteresult -match 'vEthernet\s+\([^)]+\)'
    $s3
    if (($adapteresult.Contains('Storage') -and ($adapteresult -match 'vEthernet\s+\([^)]+\),storage')) )
    
    {
    $s = 0
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File D:\1.html
    #Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    
    elseif (($adapteresult.contains('Storage') -eq $false) -and ($adapteresult -match 'vEthernet\s+\([^)]+\)'))
    
    {
    $s = 1
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File D:\1.html
    #Add-Content $report  -body "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    else
    {
    $s = 2
    ConvertTo-Html -body "<table><tr><td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td></tr></table>" | Out-File D:\1.html
    #Add-Content $report "<td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td>"
    }
    $s
    D:\1.html
    Output is coming is green. Check once and revert me


    (66,65,83,65,84,73|%{[char]$_})-join''

    Thursday, November 9, 2017 6:56 AM
  • I tried like below and it didn't work for me .The first if condition line work perfect, but the second else if condition doesn't work well. I have tried changing the NIC order ,even after changing order it print the color as green.

    $results = Invoke-Command -ComputerName $ComputerName {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").Bind | ForEach-Object {
      $guid = ($_ -split '\\')[2]
      Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE GUID='$guid'" |
        Select-Object -ExpandProperty NetConnectionID
    }}
    
    $adapteresult = $results -join","
    
    if (($adapteresult.Contains('Storage') -and ($adapteresult -match 'vEthernet\s+\([^)]+\),storage')))
    {
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    elseif ($adapteresult -NotContains "Storage" -and ($adapteresult -match 'vEthernet\s+\([^)]+\),'))
    {
    Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
    }
    else
    {
    Add-Content $report "<td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td>"
    }

    • Edited by Shimith Wednesday, November 22, 2017 11:06 AM
    Tuesday, November 21, 2017 3:57 PM