Nguyen
Nguyen

Reputation: 13

PowerShell script to export CSV with netBIOS name

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

Upvotes: 0

Views: 1132

Answers (1)

Robbie Courtney
Robbie Courtney

Reputation: 81

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

Upvotes: 1

Related Questions