最近我一直在努力parsing通过一系列服务器的DNS信息。 我似乎无法正确地将该variables传递给函数。 调用函数本身并传递一个variables就可以了。 我错过了什么? 请帮忙。
Function Get-DnsEntry($computer) { If($computer -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") { [System.Net.Dns]::GetHostEntry($computer).HostName } ElseIf( $computer -match "^.*\.\.*") {[System.Net.Dns]::GetHostEntry($computer).AddressList[0].IPAddressToString} ELSE { Throw "Specify either an IP V4 address or a hostname" } } $computer = '"abc01.somenetwork.net"' Get-DnsEntry $computer
所以,上面的代码,如果我只是运行Get-DnsEntry“abc01.somenetwork.net”它的工作原理。 如果我尝试像上面那样向它传递一个variables,它找不到主机。
请避免使用单引号和双引号, 例如 ''string''
$computer = 'abc01.somenetwork.net' Get-DnsEntry $computer
要么
$computer = "abc01.somenetwork.net" Get-DnsEntry $computer
两者都应该工作正常。
最后得到它的工作….
Function Get-DnsEntry {[cmdletbinding()]param([string]$computer) if($computer -match "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") { [string]$hostname = $computer [Net.Dns]::GetHostEntry($hostname).HostName } elseif( $computer -match "^.*\.\.*") { [string]$hostname = $computer [Net.Dns]::resolve($hostname).AddressList[0].IPAddressToString } else{ Throw "Specify either an IP V4 address or a hostname" } } [string]$hostname = 'abc01.somenetwork.net' Get-DnsEntry $server -Verbose