How to format the file in powershell -
am trying format 1 file 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 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 please suggest whcih function should use above results?
below script:-
here script working on
"ipaddresss hostname result" | out-file -append d:\cep\testlogging.txt $lines = get-content myfile.txt | {$_ -notmatch "((^#)|(^\s+$))"} foreach ($line in $lines) { $fields = $line -split '\s+' $ip = $fields[0] ($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 } } }
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
Comments
Post a Comment