我有一个脚本,用于收集文本文件中服务器列表的网络信息并导出为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
答案 0 :(得分:1)
将此添加到您的循环中:
$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没有[有序]的东西。
$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