locked
PowerShell & Batch script variables RRS feed

  • Question

  • I'm running this from a command prompt....

    PowerShell Get-WmiObject Win32_NetworkAdapterConfiguration ^| where{$_.DefaultIPGateway -ne $null} ^| Format-Table IPAddress -HideTableHeaders & for /f "skip=4 usebackq tokens=2" %a in (`nslookup %computername%`) do echo %a

    and getting this output...
    ----------------------------------------------------

    {172.20.5.162}

    C:\Users\bill>echo 172.20.5.162
    172.20.5.162

    ----------------------------------------------------

    I need to compare these, probably putting them each in a variable.  Any help is appreciated.

    Friday, February 19, 2016 5:55 PM

Answers

  • Here's a sample PowerShell script:


    $hostName = [Net.Dns]::GetHostName()
    
    $ipInDNS = ([Net.Dns]::GetHostEntry($hostName).AddressList |
      Where-Object { $_.AddressFamily -eq "InterNetwork" }).IPAddressToString
    
    $ipInNIC = (Get-WMIObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE" |
      Where-Object { $_.DefaultIPGateway }).IPAddress[0]
    
    if ( $ipInDNS -ne $ipInNIC ) {
      Write-Host "IP addresses do not match"
    }
    

    Notice the lack of ugly string parsing or attempts at output formatting. We just get properties of objects.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Friday, March 18, 2016 6:59 PM
    • Marked as answer by Bill_Stewart Tuesday, April 19, 2016 8:44 PM
    Friday, February 19, 2016 7:40 PM

All replies

  • Don't run a PowerShell script from a batch file.

    Just run the command directly in PowerShell.

    What specifically are you trying to do?


    -- Bill Stewart [Bill_Stewart]

    Friday, February 19, 2016 6:37 PM
  • I can only run command line code in my application (Tripwire).  I'm trying to verify that the IP address on the NIC that has the default gateway matches what's in DNS (nslookup).
    Friday, February 19, 2016 6:46 PM
  • I can only run command line code in my application (Tripwire).  I'm trying to verify that the IP address on the NIC that has the default gateway matches what's in DNS (nslookup).

    Obviously you can run PowerShell.  Just place all code I a file an run it.  Use exit codes to return match or not match


    \_(ツ)_/

    Friday, February 19, 2016 6:50 PM
  • Here's a sample PowerShell script:


    $hostName = [Net.Dns]::GetHostName()
    
    $ipInDNS = ([Net.Dns]::GetHostEntry($hostName).AddressList |
      Where-Object { $_.AddressFamily -eq "InterNetwork" }).IPAddressToString
    
    $ipInNIC = (Get-WMIObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled=TRUE" |
      Where-Object { $_.DefaultIPGateway }).IPAddress[0]
    
    if ( $ipInDNS -ne $ipInNIC ) {
      Write-Host "IP addresses do not match"
    }
    

    Notice the lack of ugly string parsing or attempts at output formatting. We just get properties of objects.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Bill_Stewart Friday, March 18, 2016 6:59 PM
    • Marked as answer by Bill_Stewart Tuesday, April 19, 2016 8:44 PM
    Friday, February 19, 2016 7:40 PM