Measure-Object
restituisce un oggetto TextMeasureInfo
, non un numero intero:
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
le informazioni che si desidera utilizzare è fornito dal Lines
proprietà di tale oggetto:
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...
Che proprietà restituisce un intero reale:
PS C:\> $lines.Lines.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Int32 System.ValueType
PS C:\> $lines.Lines
5
in modo da poter utilizzare che nel vostro ciclo:
PS C:\> for ($i = 0; $i -le $lines.Lines; $i++) { echo $i }
0
1
2
3
4
5
PS C:\> _
fonte
2012-11-18 13:50:06
Apparentemente la proprietà 'Lines' contiene il numero di righe non vuote (cioè linee con una lunghezza> 0). La documentazione non menziona questo, però. –