Measure-Object
zwraca TextMeasureInfo
obiektu nie liczbą całkowitą:
PS C:\> $lines = Get-Content .\foo.txt | Measure-Object -Line
PS C:\> $lines.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False TextMeasureInfo Microsoft.PowerShell.Commands.MeasureInfo
informacje chcesz użyć jest przez Lines
własność tego obiektu:
PS C:\> $lines | Get-Member
TypeName: Microsoft.PowerShell.Commands.TextMeasureInfo
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Characters Property System.Nullable`1[[System.Int32, mscorlib, Vers...
Lines Property System.Nullable`1[[System.Int32, mscorlib, Vers...
Property Property System.String Property {get;set;}
Words Property System.Nullable`1[[System.Int32, mscorlib, Vers...
To właściwość zwraca rzeczywistą liczbę całkowitą:
PS C:\> $lines.Lines.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
PS C:\> $lines.Lines
5
więc można używać, że w pętli:
PS C:\> for ($i = 0; $i -le $lines.Lines; $i++) { echo $i }
0
1
2
3
4
5
PS C:\> _
Podobno własność 'Lines' zawiera liczbę niepustych linii (tj. Linii o długości> 0). Dokumentacja tego nie wspomina. –