0

am trying to format one file which is like below.

IPAddresss    HostName                  Result
none sinuiy01.infra.go2uti.com                NotValid
none  sinuid20.devtst.go2uti.com                NotValid
172.21.40.204  USEM9999.essilor.res                  Success
172.21.40.204  webmail.nscorp.com                NotValid
172.21.40.204  nsc.nscorp.com                Unsuccess
172.21.40.204  bp-nsc.nscorp.com                NotValid

But need result like below:--

IPAddresss         HostName                                Result
none               sinuiy01.infra.go2uti.com               NotValid
none               sinuid20.devtst.go2uti.com              NotValid
172.21.40.204      USEM9999.essilor.res                    Success
172.21.40.204      webmail.nscorp.com                      NotValid
172.21.40.204      nsc.nscorp.com                          Unsuccess
172.21.40.204      bp-nsc.nscorp.com                       NotValid

Could you please suggest whcih function I should use to get the above results?

Below is the script:-

Here is the script am working on

"IPAddresss    HostName                  Result" | Out-File -Append D:\CEP\testlogging.txt 
$lines = Get-Content myfile.txt | Where {$_ -notmatch "((^#)|(^\s+$))"}
foreach ($line in $lines) {
    $fields = $line -split '\s+'
    $ip = $fields[0]
    for ($i = 1; $i -lt $fields.Length; $i++) {
        $ESXHost = $fields[$i]
    echo "Host $ESXHOST"
    try
        {
        $currentConnection = Test-Connection $ESXHost -ErrorAction stop

        if($currentConnection.Count -gt 0)
            {
            $hostIP = ($currentConnection)[$i].IPV4Address.IPAddressToString

            echo "hostIp $hostIP"
                if ($hostIP -eq $ip) 
                { 

                "$hostIP  $ESXHost                  Success" | Format-Table -Wrap -AutoSize | Out-File -Append D:\CEP\testlogging.txt 



                }
                else 
                {

                "$hostIP  $ESXHost                  Unsuccessful" | Format-Table -Wrap -AutoSize | Out-File -Append D:\CEP\testlogging.txt
                }
            }

        }
4
  • What format is the file? And are you outputting that data from PowerShell to the file or are you reading the file in to then be formatted by PowerShell? Do you have the Code that you have been working on? Commented Jul 31, 2013 at 10:26
  • file is .txt, and am trying it in the power shell script. Something like Format-Table -Wrap -AutoSize | Out-File -Append D:\CEP\testlogging.txt Commented Jul 31, 2013 at 10:30
  • Check out the answer below :) Commented Jul 31, 2013 at 12:43
  • Are you trying to format by position (fixed # of characters) or with a specific delimiter? Commented Jul 31, 2013 at 12:57

2 Answers 2

1

you can try :

Get-Content C:\temp\t1.txt | % {$_ -replace ' +',' ' }| % { $parts = $_.split(' '); Write-output ("{0,-15}{1,-30}{2,-8}" -f $parts[0],$parts[1],$parts[2])} | Out-File  C:\temp\t2.txt
Sign up to request clarification or add additional context in comments.

Comments

1

As you are using a string to output the data to the text file you could try the following String formatting to space out the variable in the string evenly. In the example below the spacing is 25 characters between each value:

[string]::Format("{0,-25}{1,-25}{2,-25}",$hostIP,$ESXHost,"Success") | Out-File -Append D:\CEP\testlogging.txt

Syntax:

$I - This is the parameter number to be inserted (0 or more)
$C - The number of total characters before the next variable.
(Negative numbers make the word start from the left)

[string]::Format("{$I,$C}",Parameter1,Parameter2)

There is also a shorthand way of performing the same action using the -f operator:

"{0,-25}{1,-25}{2,-25}" -f $hostIP,$ESXHost,"Success" | Out-File -Append D:\CEP\testlogging.txt

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.