如何结合此代码来检查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条目。
答案 0 :(得分:1)
这是一个奇怪的构造,因为我不确定您为什么以这种方式使用该函数。意思是,运行带有必需的强制参数的功能,然后检查是否确定是否输入了一个,是否发送了一个读主机,直到用户这样做为止。强制性手段,强制性。除非输入某些内容,否则不要继续。
如果您只是为了确保用户输入正确的IPA而进行检查,请在验证参数中进行检查。含义仅允许使用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
#>