私は、構造化されていない入力を受け取り、再フォーマットしてCSVから吐き出すスクリプトを作成しています。Powershellの追加メンバー計算値
メンバーの1人に、IPに関連付けられたDNSエントリを取得するために、別のメンバーのNSLOOKUPを実行したいとします。しかしながら、多くのエントリは0.0.0.0のIPを有する。リスニングポートです。クライアントはまだ接続されていません。
NSLOOKUPからのエラーは、DNSエントリがないため、間違います。
したがって、IPが0.0.0.0でない場合はNSLOOKUPのみを行うようにコードを修正し、そうでない場合は空の文字列を返します。
しかし、私はそれを動作させることはできません。
次は、エラーをスローしません:その後、
$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
}
しかし、私は、私が見配列内のオブジェクトのいずれかを点検している場合:私は、括弧ではなく、スクリプトブロックでラップした場合
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
、次のようにClient_DNSを設定する行があるので:
$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 {""})
私が手:
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
私はその後、アドオンmemberコマンドで値としてその変数を使用して、Ifステートメントを実行し、結果を変数に入れて、私はそれがコードの二つの別々のラインとして機能するように得ることができると確信していますしかし、私は何をしようとしているのかは確かに可能です、私はちょうど何かを欠いている!
アイデア?
パーフェクト!その行を自分のコードの '$ obj | Add-Member -MemberType ScriptProperty -Name Client_DNS -value {(NSLOOKUP $ obj.Client_IP | Select-String Name).Line.ToString()。Replace(" "、" ") .Split( ":")[1]} 'それは完全に動作し、nslookupからエラーを押さえてIP用のDNSエントリを見つけられず、その後も自分のコードからDNS名だけを抜き出すnslookupの結果。私は他のメンバータイプにブラッシュアップする必要があると思います。これまではnotepropertyを使っていました。 –
私は 'NSLOOKUP $ obj.Client_IP'を' NSLOOKUP $ This.Client_IP'に変更しなければなりませんでした。さもなければ、export-csvへのパイプが空で、それが時代を取る時に作成されたcsvのClient_IP列。また、その変更は出力を修正するものの、時代がかかりそうです - 配列が移入され、export-csvによって再度計算されるので計算されていると思われます! –
現在、私は次のようになってきました - きれいではなく、 '$ ErrorActionPreference =" Stop "; $ Client_DNS_V = If($ obj.Port_State -ne "LISTEN"){Try {(NSLOOKUP $ obj.Client_IP | Select-String Name).Line.ToString()。Replace( ""、 "")。分割( ":")[1]}キャッチ{}} else {""}; $ ErrorActionPreference = "SilentlyContinue"; $ obj |メンバーの追加-MemberType NoteProperty -Name Client_DNS -Value $ Client_DNS_V; ' –