title: Powershell add-member calculated value
body: <p>I'm writing a script that takes unstructured input, reformats it and spits out a CSV.</p>
  
  <p>In one of the members, I want to do an NSLOOKUP of another member to get the DNS entry associated with the IP. However, a number of the entries have an IP of 0.0.0.0 e.g. it's a listening port, there's no client yet connected.</p>
  
  <p>These throw up errors from NSLOOKUP, quite rightly, because there's no DNS entry for it.</p>
  
  <p>So, I amended my code to only do the NSLOOKUP if the IP was not 0.0.0.0, otherwise return an empty string.</p>
  
  <p>But I can't make it work.</p>
  
  <p>The following throws up no errors:</p>
  
  <pre><code>$srcdata -split "`r`n"|foreach {
                      $obj = New-Object System.Object
                      $obj|Add-Member -MemberType NoteProperty -Name Connection_ID  -Value $_.Split(",")[0]
                      $obj|Add-Member -MemberType NoteProperty -Name Filename       -Value $_.Split(",")[1]
                      $obj|Add-Member -MemberType NoteProperty -Name MCP_Port       -Value $_.Split(",")[2]
                      $obj|Add-Member -MemberType NoteProperty -Name Client_Port    -Value $_.Split(",")[3]
                      $obj|Add-Member -MemberType NoteProperty -Name Port_State     -Value $_.Split(",")[4]
                      $obj|Add-Member -MemberType NoteProperty -Name Client_IP      -Value $_.Split(",")[5]
                      $obj|Add-Member -MemberType NoteProperty -Name Client_DNS     -value {If ($obj.Client_IP -ne "0.0.0.0") {(NSLOOKUP $obj.Client_IP|Select-String Name).Line.ToString().Replace(" ","").Split(":")[1]} else {""}}
                      $obj|Add-Member -MemberType NoteProperty -Name Protocol_Stack -Value $_.Split(",")[6]
                      $outfile += $obj
  }
  </code></pre>
  
  <p>But then if I inspect one of the objects in the array I see:</p>
  
  <pre><code>Connection_ID  : 1
  Filename       : CCF_MARC1
  MCP_Port       : 2001
  Client_Port    : 0
  Port_State     : LISTEN
  Client_IP      : 0.0.0.0
  Client_DNS     : If ($obj.Client_IP -ne "0.0.0.0") {(NSLOOKUP $obj.Client_IP|Select-String Name).Line.ToString().Replace(" ","").Split(":")[1]} else {""}
  Protocol_Stack : LEGACY
  </code></pre>
  
  <p>If I wrap in parentheses instead of in a scriptblock, so the line setting up the Client_DNS is as follows:</p>
  
  <pre><code>            $obj|Add-Member -MemberType NoteProperty -Name Client_DNS     -value (If ($obj.Client_IP -ne "0.0.0.0") {(NSLOOKUP $obj.Client_IP|Select-String Name).Line.ToString().Replace(" ","").Split(":")[1]} else {""})
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>If : The term 'If' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
  At K:\CodeSnippets\PowerShell\NW_CONN_TO_CSV.ps1:21 char:91
  + ... NS     -value (If ($obj.Client_IP -ne "0.0.0.0") {(NSLOOKUP $obj.Client_IP|Selec ...
  +                    ~~
      + CategoryInfo          : ObjectNotFound: (If:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException
  </code></pre>
  
  <p>I'm sure I could get it to work as two separate lines of code, so do the If statement and put the result in a variable, then use that variable as the value in the add-member command, but I'm sure what I'm trying to do is eminently possible, I'm just missing something!</p>
  
  <p>Any ideas?</p>