我试图将 $x = [System.Net.Dns]::GetHostAddresses($name)
语句的结果写入我的 write-host 字符串中,但是在将函数的结果输出时遇到了一些问题。
以下是相关代码:
Import-Module activedirectory
function fu($name)
{
$x = [System.Net.Dns]::GetHostAddresses($name).value
if ($x -ne $null){
Write-Host{ $x }
}
else{
Write-Host{"Null"}
}
}
Get-ADComputer -SearchBase 'OU=CorpServers,DC=corp,DC=com,DC=net' -Server "corp.com.net" -Filter * -Properties * |ForEach-Object{write-host "add filter filterlist=""L2-Windows Servers"" srcaddr=any dstaddr=$(fu $_.Name) description=""$($_.Name)"""}
目前它只是直接输出字符串,但当它到达"fu"子表达式时,似乎不能正确执行逻辑,而只是字面上输出"$x",我的意图是使其输出foreach-object语句中当前对象的IP。
[System.Net.Dns]::GetHostAddresses(<<hostname>>) | Get-Member
没有显示任何value
属性?你可以先在你的 ISE / 控制台中运行[System.Net.Dns]::GetHostAddresses(<<hostname>>)
来尝试。 - Angshuman Agarwalfunction fu()
中的内容? - Angshuman Agarwal$x = [System.Net.Dns] :: GetHostAddresses($name) | select-object IPAddressToString -expandproperty IPAddressToString
的内容? - AA11oAKas