2013-07-31 8 views
0

아래와 같이 하나의 파일을 포맷하려고합니다.powershell에서 파일 형식을 지정하는 방법

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 

당신은 내가 위의 결과를 얻기 위해 사용한다 whcih 기능을 제안 시겠어요 - :

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 

하지만은 아래와 같은 결과를해야합니까? 당신이 서식 다음 문자열을 시도 할 수있는 텍스트 파일로 출력 데이터를 문자열을 사용하고 여기에

스크립트가

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

     } 
+0

파일의 형식은 무엇입니까? 그리고 그 데이터를 PowerShell에서 파일로 출력하고 있습니까 아니면 PowerShell에서 파일을 읽으려고합니까? 당신이 작업 한 행동 강령을 가지고 있습니까? – Richard

+0

파일은 .txt이며 파워 쉘 스크립트에서 사용하려고합니다. Format-Table -Wrap과 같은 것 - 자동 축소 | Out-File -D : \ CEP \ testlogging.txt를 입력하십시오. – sharsour

+0

아래의 대답을 확인하십시오 :) – Richard

답변

1

일하고있다 -이 : 아래

는 스크립트입니다 문자열의 변수를 고르게 배치하십시오.

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

구문 :

$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) 

-f 연산자를 사용하여 동일한 동작을 수행하는 약식 방법이 간격 아래의 예에서, 각 값의 25 개 문자이다 :

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

시도해 볼 수 있습니다.

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 
관련 문제