3

i'm attempting to write the result of the $x = [System.Net.Dns]::GetHostAddresses($name) statement into my write-host string but am encounter some issues with getting the result from the function into the output.

here's the relevant code:

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)"""}

currently it just outputs the string as is, but when it reaches the fu subexpression seems to not properly perform the logic and only outputs "$x" literally, where my intent was to have it output the IP of the current obj in the foreach-object statement.

6
  • [System.Net.Dns]::GetHostAddresses(<<hostname>>) | Get-Member does not show any value property ? You may simply try running this - [System.Net.Dns]::GetHostAddresses(<<hostname>>) first in your ISE / console. Commented Jun 28, 2012 at 15:51
  • Did you intend something like this instead - [System.Net.Dns]::GetHostAddresses("<<hostname>>")[0].ToString() in function fu() ? Commented Jun 28, 2012 at 16:00
  • Alter $x to something like: $x = [System.Net.Dns]::GetHostAddresses($name)|select-object IPAddressToString -expandproperty IPAddressToString ? Commented Jun 28, 2012 at 16:01
  • Yea, but it will return multiple values. Right ? Commented Jun 28, 2012 at 16:03
  • well i wanted to call the fu function from within the foreach-object cmd and supply it with a the current objs $_.Name, so i thought it would return a single IP address connected with the obj specified by its $_.Name? Commented Jun 28, 2012 at 16:08

3 Answers 3

1

It's because you put $x in curly brackets {}.

Just do Write-Host $x

Sign up to request clarification or add additional context in comments.

1 Comment

Wow... I've been in to many meetings today lol, Thanks SpellingD
1

I expand a bit the code for the explanation but try this :

function fu($name)
{
  $res = $null
  $x = [System.Net.Dns]::GetHostAddresses($name)
  if ($x -ne $null)
  {
     $res = $x
  }
  return $res
}

$a = fu "localhost"
$a
$a.gettype().fullname

It does what you want, $a is an array of data. But you have to understand that the following functions gives different results

function fu($name)
{
  $res = $null
  $x = [System.Net.Dns]::GetHostAddresses($name)
  if ($x -ne $null)
  {
     $res = $x
  }
  Write-Host $res
}

Clear-Host
$a = fu "localhost"
$a
$a | Get-Member

The las one give again the good result. return and write-out both write data in the output of the function. Write-host just write to the host.

function fu($name)
{
  $res = $null
  $x = [System.Net.Dns]::GetHostAddresses($name)
  if ($x -ne $null)
  {
     $res = $x
  }
  Write-output $res
}

Clear-Host
$a = fu "localhost"
$a
$a | Get-Member

Comments

0

[System.Net.Dns]::GetHostAddresses(<<hostname>>) | Get-Member does not show any value property ?

You may try this function instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.