尝试将get-netadaptor中的某些属性与get-netipaddress结合起来
尝试了我所知道的一切,但是变量$ getnet-name的结果全部在一行上
${getnet-name} = New-Object psobject -Property @{
Status = $null
AdminStatus = $null
LinkLayerAddress = $null
}
${getnet-name}.Status = (Get-NetAdapter -Physical).Status
${getnet-name}.AdminStatus = (Get-NetAdapter -Physical).AdminStatus
${getnet-name}.LinkLayerAddress = (Get-NetAdapter -Physical).LinkLayerAddress
Get-NetAdapter | Get-NetIPAddress | ft interfacealias , ipaddress , prefixorigin ${getnet-name}.AdminStatus -AutoSize
PS C:\Users\username> ${getnet-name}
Status AdminStatus LinkLayerAddress
------ ----------- ----------------
{Disconnected, Up, Disconnected, Up} {Up, Up, Up, Up} {14-xF-8A-xC-73-00, 0x-50-B6-xx-F0-EB, 10-x5-30-x1-56-B7, 02-00-xC-4x-4F-50}
这是我陷入循环的地方
${getnet-name} = New-Object psobject -Property @{
Status = $null
AdminStatus = $null
LinkLayerAddress = $null
}
${getnet-name}.Status = (Get-NetAdapter).Status
${getnet-name}.AdminStatus = (Get-NetAdapter).AdminStatus
${getnet-name}.LinkLayerAddress = (Get-NetAdapter).LinkLayerAddress
${getnet-obj} = Get-NetAdapter -Physical | Select-Object -Property {${getnet-name}.AdminStatus} , {${getnet-name}.Status} , {${getnet-name}.LinkLayerAddress}
$getnetobj = Get-NetAdapter -Physical
foreach ($InterfaceIndex in $getnetobj)
{
Get-NetIPAddress | ft interfacealias , ipaddress , prefixorigin , {${getnet-name}.AdminStatus} , {${getnet-name}.Status} , {${getnet-name}.LinkLayerAddress} -AutoSize
}
每个接口重复相同的结果
希望最终也可以仅使用Get -AddressFamily过滤器来过滤IPv4地址
当前正在工作,但这可能不是实现此目的的好方法。如果您要添加其他更优雅的解决方案,则将很有帮助!
$results = @()
foreach ($adapter in (Get-NetAdapter -Physical))
{
$ipaddresses = $adapter | Get-NetIPAddress -AddressFamily IPv4
foreach ($ipaddress in $ipaddresses)
{
#build the object for reach record you want
$netadapteraddress = New-Object psobject -Property @{
Status = $null
AdminStatus = $null
LinkLayerAddress = $null
InterfaceAlias = $null
IPAddress = $null
PrefixOrigin = $null
}
#properties for the adapter
$netadapteraddress.Status = $adapter.Status
$netadapteraddress.AdminStatus = $adapter.AdminStatus
$netadapteraddress.LinkLayerAddress = $adapter.LinkLayerAddress
#properties for the ipaddress
$netadapteraddress.InterfaceAlias = $ipaddress.InterfaceAlias
$netadapteraddress.IPAddress = $ipaddress.IPAddress
$netadapteraddress.PrefixOrigin = $ipaddress.PrefixOrigin
#add to results
$results += $netadapteraddress
}
}
$results | ft -wrap
谢谢!
答案 0 :(得分:2)
也许有更好的方法,但这就是我要解决的方法。遍历所有物理适配器,遍历该适配器的每个ipaddress,创建一个对象来保存所需的数据,然后填充数据。将对象添加到数组中,然后返回数组。
$results = @()
foreach ($adapter in (Get-NetAdapter -Physical))
{
$ipaddresses = $adapter | Get-NetIPAddress
foreach ($ipaddress in $ipaddresses)
{
#build the object for reach record you want
$netadapteraddress = New-Object psobject -Property @{
Status = $null
AdminStatus = $null
LinkLayerAddress = $null
InterfaceAlias = $null
IPAddress = $null
PrefixOrigin = $null
}
#properties for the adapter
$netadapteraddress.Status = $adapter.Status
$netadapteraddress.AdminStatus = $adapter.AdminStatus
$netadapteraddress.LinkLayerAddress = $adapter.LinkLayerAddress
#properties for the ipaddress
$netadapteraddress.InterfaceAlias = $ipaddress.InterfaceAlias
$netadapteraddress.IPAddress = $ipaddress.IPAddress
$netadapteraddress.PrefixOrigin = $ipaddress.PrefixOrigin
#add to results
$results += $netadapteraddress
}
}
$results | format-table -AutoSize