首页 > 解决方案 > How can I parse an IP address

问题描述

I have to parse to get an IP address.

$Currentpath = Split-Path -Parent $MyInvocation.MyCommand.Definition 
$CheminCSV = $currentpath + '\LesIP.csv'

$csv = Import-Csv $CheminCSV

$MyIPS = $csv | select IP

$MyHostnames = $csv | select HostName

Write-Host($MyIPS[1])

The actual results are:

@{IP=192.168.1.10}

and I expect:

192.168.1.10

标签: powershellcsvparsing

解决方案


This is not a parsing issue. Your CSV has a header for "IP". If you import this CSV, you get an object with a property "IP", which you assign to $MyIPS.

You can get your expected output by $MyIPS[1].IP. Or you simple change your select (i.e. select-object -Property) to Select-Object -ExpandProperty IP.


推荐阅读