簡體   English   中英

PowerShell腳本以NetBIOS名稱導出CSV

[英]PowerShell script to export CSV with netBIOS name

我有一個腳本,用於收集文本文件中服務器列表的網絡信息並導出為CSV。 一切正常,但我需要從文本文件IP中獲取主機NETBIOS名稱。 文本文件包含的服務器IP地址為xxx.xxx.xxx.xxx。 在腳本方面,我真的很虛弱。 下面是腳本。 我非常感謝這里專家的幫助。

$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

將此添加到您的循環中:

$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

或這樣,但是使屬性列表以隨機順序出現……因為PS v2.0沒有[ordered]內容。

$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

如果您擁有PowerShell v.3.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

        $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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM