powershell - 检查IP地址输入,然后导出到Out-GridView
问题描述
如何结合此代码检查 IP 地址并将结果列出到 Gridview 中?
function Test-IP
{
param
(
[Parameter(Mandatory = $true)]
[ValidateScript({ $_ -match [IPAddress]$_ })]
[String]$ip
)
$ip
Write-Host "$($ip) is resolved to $([System.Net.Dns]::GetHostbyAddress($($IP)))"
}
while (!Test-IP -ip "$($Input)")
{
$input = Read-Host -Prompt 'Input your IP address'
}
$zones = Get-DnsServerZone - Server PRDDNS05-VM | Where-Object { !$_.IsReverseLookupZone -and $_.ZoneType -eq 'Primary' }
$output = foreach ($zone in $zones)
{
Get-DnsServerResourceRecord -ZoneName $zone.ZoneName |
Where { $_.RecordData.Ipv4Address.IPAddressToString -contains $Input } |
Select IPV4Address, HostName, RecordType, Type, RecordData, Timestamp, TimeToLive, @{ n = 'Zone'; e = { $zone.ZoneName } }
}
$output | Out-GridView
上面的脚本用于转储包含用户输入的特定 IP 地址的所有 DNS 条目。
解决方案
这是一种奇怪的结构,因为我不确定您为什么要使用该功能,就像您现在的方式一样。意思是,运行一个带有必需的强制参数的函数,然后检查是否确定是否输入了一个,如果没有发送一个读取主机,直到用户这样做。强制的意思,强制的。除非输入内容,否则不要继续。
如果您只是为了确保用户输入正确的 IPA 进行检查,请在 validate 参数中进行检查。意思是只允许 IPA 格式。
还有,这...
Get-DnsServerZone - Server PRDDNS05-VM
... 不是有效的语法。该 cmdlet 没有称为 -Server 的参数。只有 ...
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-DnsServerZone).Parameters
(Get-Command -Name Get-DnsServerZone).Parameters.Keys
# Results
<#
Name
ComputerName
VirtualizationInstance
CimSession
ThrottleLimit
AsJob
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>
Get-help -Name Get-DnsServerZone -Examples
Get-help -Name Get-DnsServerZone -Full
Get-help -Name Get-DnsServerZone -Online
...并且破折号和 ParameterName 之间永远没有空格。我认为这只是帖子中的一个错字,但只是说。
一次迈出这一步,只需询问基础知识。
function Test-IPaddress
{
[CmdletBinding(SupportsShouldProcess)]
Param
(
[Parameter(Mandatory = $true,
ValueFromPipelineByPropertyName = $true,Position = 0)]
[ValidateScript({$_ -match [IPAddress]$_ })]
[string]$IPAddress
)
Process{[ipaddress]$IPAddress}
}
Try
{
$IPAddress = $((Test-IPaddress -IPAddress (Read-Host -Prompt 'Input a valid IP address')).IPAddressToString)
Out-GridView -InputObject $IPAddress -Title "IPAddress details for $IPAddress"
}
Catch
{
Add-Type -AssemblyName System.Drawing,
PresentationCore,
PresentationFramework,
System.Windows.Forms,
microsoft.VisualBasic
[System.Windows.Forms.Application]::EnableVisualStyles()
[System.Windows.Forms.MessageBox]::Show("Warning message for $IPAddress`n
$($PSItem.Exception.Message)" , 'Error', 'OK', 'Error')
}
一旦我们知道非常基本的调用按预期工作并输出到 OGV,您就可以在 try 块中添加其他代码并格式化以适应 OGV。
根据我的评论更新
db-ip.com/all/113.67.32
Test-Connection -ComputerName db-ip.com | Format-Table -AutoSize
# Results
<#
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
L... db-ip.com 104.26.5.15 32 15
L... db-ip.com 104.26.5.15 32 12
L... db-ip.com 104.26.5.15 32 12
L... db-ip.com 104.26.5.15 32 11
#>
Test-Connection -ComputerName 104.26.5.15 | Format-Table -AutoSize
# Results
<#
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
L... 104.26.5.15 32 15
L... 104.26.5.15 32 14
L... 104.26.5.15 32 15
L... 104.26.5.15 32 13
#>
Test-Connection -ComputerName 113.67.32.221 | Format-Table -AutoSize
$Error[0] | Format-List -Force
# Results
<#
writeErrorStream : True
Exception : System.Net.NetworkInformation.PingException: Testing connection to computer '113.67.32.221' failed: Error due to lack of
resources ---> System.ComponentModel.Win32Exception: Error due to lack of resources
--- End of inner exception stack trace ---
TargetObject : 113.67.32.221
CategoryInfo : ResourceUnavailable: (113.67.32.221:String) [Test-Connection], PingException
FullyQualifiedErrorId : TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1, 0}
PSMessageDetails :
#>
Test-NetConnection -ComputerName 113.67.32.221 -TraceRoute
# Results
<#
WARNING: Ping to 113.67.32.221 failed with status: TimedOut
WARNING: Trace route to destination 113.67.32.221 did not complete. Trace terminated :: 0.0.0.0
ComputerName : 113.67.32.221
RemoteAddress : 113.67.32.221
...
PingSucceeded : False
PingReplyDetails (RTT) : 0 ms
...
#>
Ping 113.67.32.221
# Results
<#
Pinging 113.67.32.221 with 32 bytes of data:
Request timed out.
...
Ping statistics for 113.67.32.221:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
#>
tracert 113.67.32.221
# Results
<#
Tracing route to 113.67.32.221 over a maximum of 30 hops
1 1 ms 1 ms 2 ms ...
2 24 ms 22 ms 122 ms ...
3 14 ms 41 ms 19 ms ...
...
12 * * * Request timed out.
13 * * * Request timed out.
#>
telnet 113.67.32.221
# Results
<#
Connecting To 113.67.32.221...Could not open connection to the host, on port 23:
Connect failed
#>
telnet 113.67.32.221 80
# Results
<#
Connecting To 113.67.32.221...Could not open connection to the host, on port 80:
Connect failed
#>
推荐阅读
- google-plus - 是否仍然可以在 Google+ 中呈现带有描述的全出血预览?
- c# - 使用 C# selenium web 驱动程序从 excel 读取数据时出现异常
- postgis - 为什么我的图像马赛克中的自定义维度在 geoserver getmap 请求中不起作用?
- android - 活动恢复时 RecyclerView 返回 null
- android - 我的蓝牙应用程序无法正常工作,只有在打开我的蓝牙设置活动后才能工作
- c - 将 C 程序转换为 nasm 程序集 x86
- pagespeed - Pagespeed Insight 和 GTmetrix 之间的 FCP 和 DLC 差异
- javascript - 如何确保数组已填充 Fetch API?
- react-native - NativeBase 抽屉道具
- python-2.7 - 我的 from sklearn.decomposition import PCA 有错误