powershell net view 未显示网络上的所有计算机名称
Posted
技术标签:
【中文标题】powershell net view 未显示网络上的所有计算机名称【英文标题】:powershell net view not showing all computer names on network 【发布时间】:2020-10-06 04:45:18 【问题描述】:使用单行
switch -regex (NET.EXE VIEW) "^\\\\(?<Name>\S+)\s+" $matches.Name
获取网络上所有计算机的列表,但由于某种原因,这仅返回部分列表,这可能是与工作组相关的问题吗?
不过我可以在windows网络标签下看到网络上的所有电脑并成功登录。
【问题讨论】:
【参考方案1】:为此编写了一个脚本,通过 arp -a、主机名等获取所有网络计算机,并带有进度条,然后将它们提供给我可以使用的数组。
Add-Type -assembly System.Windows.Forms
## -- Create The Progress-Bar
$ObjForm = New-Object System.Windows.Forms.Form
$ObjForm.Text = "Initial Setup"
$ObjForm.Height = 100
$ObjForm.Width = 500
$ObjForm.BackColor = "#000000"
$ObjForm.FormBorderStyle = 'None'
$ObjForm.ControlBox = $False
$ObjForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$ObjForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$ObjForm.TopMost = $True
## -- Create The Label
$ObjLabel = New-Object System.Windows.Forms.Label
$ObjLabel.Text = "Starting. Please wait ... "
$ObjLabel.Left = 5
$ObjLabel.Top = 10
$ObjLabel.Width = 500 - 20
$ObjLabel.Height = 15
$ObjLabel.Font = "Tahoma"
$ObjLabel.ForeColor = "#FFFFFF"
## -- Add the label to the Form
$ObjForm.Controls.Add($ObjLabel)
$PB = New-Object System.Windows.Forms.ProgressBar
$PB.Name = "PowerShellProgressBar"
$PB.Value = 0
$PB.Style = "Continuous"
$PB.ForeColor = "#FFC20E"
$PB.BackColor = "#111111"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 500 - 40
$System_Drawing_Size.Height = 20
$PB.Size = $System_Drawing_Size
$PB.Left = 5
$PB.Top = 40
$ObjForm.Controls.Add($PB)
## -- Show the Progress-Bar and Start The PowerShell Script
$ObjForm.Show() | Out-Null
$ObjForm.Focus() | Out-NUll
$ObjLabel.Text = "Starting. Please wait ... "
$ObjForm.Refresh()
Start-Sleep -Seconds 1
## -- Execute The PowerShell Code and Update the Status of the Progress-Bar
$Activity = "Processing items"
$NameArray = arp -a
$ArpArray = @()
$FinalArray = @()
foreach ($object in $NameArray)
$ArpArray += ($object -split "\s+")[1] -replace "Internet", ""
$TotItems = $ArpArray.Count
$Count = 0
$ParseArray = $ArpArray | Where $_ -ne ''
foreach ($address in $ParseArray)
#write-host $address
$Stringer = $address
try $Name = [System.Net.Dns]::GetHostByAddress($Stringer).Hostname
catch [Exception] $Name = ""
$FinalArray += $Name
$Count++
$percentComplete = ($Count/$TotItems * 100)
$PB.Value = $percentComplete
$ObjLabel.Text = "Getting Network Computer Names"
$ObjForm.Refresh()
Start-Sleep -Milliseconds 150
$FinalArray = $FinalArray | Where $_ -ne ""
$FinalArray = $FinalArray | Select -Unique
#$FinalArray
$ObjForm.Close()
【讨论】:
以上是关于powershell net view 未显示网络上的所有计算机名称的主要内容,如果未能解决你的问题,请参考以下文章
如何在PowerShell中使用.NET Framework
powershell 这将显示有问题的计算器上的.net框架的所有版本