0

I have a script that collect network information for a list of server in text file and export to CSV. Everything works as expect but I need to get the host NETBIOS name from the the text file IP. The text file contains servers IP address as xxx.xxx.xxx.xxx. I'm really weak when it comes to scripting. Below is the script. I really appreciate for the help from the expert in here.

$InputFile = "C:\servers.txt"
$CsvFile = "C:\results.csv"
$report = @()
ForEach($Computer in (gc -Path $InputFile)){
  If(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
    $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
    ForEach($Network in $Networks) {
      $IPAddress = $Network.IpAddress[0]
      $SubnetMask = $Network.IPSubnet[0]
      $DefaultGateway = $Network.DefaultIPGateway
    }
    $MACAddress = $Network.MACAddress
    $OutputObj = New-Object -Type PSObject
    $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value 
    $Computer.ToUpper()
    $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value 
    $IPAddress
    $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value 
    $SubnetMask
    $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value 
    ($DefaultGateway -join ",")
    $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value 
    $MACAddress
    $Report += ,$OutputObj
  }
 }
 Write-Output $report

1 Answer 1

1

Add this to your loop :

$NetBiosName = Get-WmiObject  Win32_ComputerSystem -ComputerName $Computer | select -ExpandProperty name
$OutputObj | Add-Member -MemberType NoteProperty -Name "NetBiosName" -Value 
    $NetBiosName

PowerShell V2.0

    $InputFile = "c:\servers.txt"
    $CsvFile = "c:\results.csv"
    $report = @()
    ForEach($Computer in (gc -Path $InputFile)){
      If(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
        $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
        ForEach($Network in $Networks) {
          $IPAddress = $Network.IpAddress[0]
          $SubnetMask = $Network.IPSubnet[0]
          $DefaultGateway = $Network.DefaultIPGateway
        }

        $MACAddress = $Network.MACAddress
        $NetBiosName = Get-WmiObject  Win32_ComputerSystem -ComputerName $Computer | select -ExpandProperty name

        $OutputObj = New-Object -Type PSObject
        $OutputObj | Add-Member -MemberType NoteProperty -Name NetBios -Value $NetBiosName
        $OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
        $OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
        $OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
        $OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
        $OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
        $Report += ,$OutputObj
      }
     }
     Write-Output $report

Or this, but makes the list of properties in a random order... since PS v2.0 doesn't have the [ordered] thing.

    $InputFile = "c:\servers.txt"
    $CsvFile = "c:\results.csv"
    $report = @()
    ForEach($Computer in (gc -Path $InputFile)){
      If(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
        $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
        ForEach($Network in $Networks) {
          $IPAddress = $Network.IpAddress[0]
          $SubnetMask = $Network.IPSubnet[0]
          $DefaultGateway = $Network.DefaultIPGateway
        }

        $MACAddress = $Network.MACAddress
        $NetBiosName = Get-WmiObject  Win32_ComputerSystem -ComputerName $Computer | select -ExpandProperty name

        $Props = @{
                    NETBIOS      = $NetBiosName
                    ComputerName = $Computer.ToUpper()
                    IPAddress    = $IPAddress
                    SubnetMask   = $SubnetMask
                    Gateway      = ($DefaultGateway -join ",")
                    MACAddress   = $MACAddress
                }

        $OutputObj = New-Object -Type PSObject -Property $Props
        $Report += ,$OutputObj
      }
     }
     Write-Output $report

Or best if you have PowerShell v.3.0 or later.

    $InputFile = "c:\servers.txt"
    $CsvFile = "c:\results.csv"
    $report = @()
    ForEach($Computer in (gc -Path $InputFile)){
      If(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
        $Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
        ForEach($Network in $Networks) {
          $IPAddress = $Network.IpAddress[0]
          $SubnetMask = $Network.IPSubnet[0]
          $DefaultGateway = $Network.DefaultIPGateway
        }

        $MACAddress = $Network.MACAddress
        $NetBiosName = Get-WmiObject  Win32_ComputerSystem -ComputerName $Computer | select -ExpandProperty name

        $Props = [ordered]@{
                    NETBIOS      = $NetBiosName
                    ComputerName = $Computer.ToUpper()
                    IPAddress    = $IPAddress
                    SubnetMask   = $SubnetMask
                    Gateway      = ($DefaultGateway -join ",")
                    MACAddress   = $MACAddress
                }

        $OutputObj = New-Object -Type PSObject -Property $Props
        $Report += ,$OutputObj
      }
     }
     Write-Output $report

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you very much, Robbie. I added Get-WmiObject Win32_ComputerSystem -Computer $Computer | Select -ExpandProperty name as you suggested but it still return with IP not computer NetBIOS name :(
I might have put the code that you suggested in the wrong place. What should I put that line of code to the script? I'm learning and have a hard time. :(
@Nguyen, this is the solution.
Thanks a lot, JPBlanc. However, when I run the script I got an error:
"Unable to find [ordered]: make sure that the assembly containing this type is loaded. At C:\script_file: 16 char 27 $Prop = [Ordered] <<<< ${ + CategoryInfo : InvalidOperation: (ordering:String) [], RuntimException + FullyQualifiedErrorId: TypeNotFound
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.