如何计算powershell变量中的行数

时间:2017-09-07 15:27:52

标签: powershell

我有以下powershell查询:

$sql = "select name, lastName, City, Address from Persons"

$TotalPeople = Invoke-Sqlcmd -ServerInstance $ResultsInstance -Database $ResultsDatabase -Query $sql

Write-Output "Total number of people run is $TotalPeople  "

我希望能够计算$ TotalPeople变量中的行数。有没有办法在powershell中做到这一点?

1 个答案:

答案 0 :(得分:5)

Invoke-sqlCmd返回一个数组。

因此,您只需使用count方法即可。

$sql = "select name, lastName, City, Address from Persons"

$TotalPeople = Invoke-Sqlcmd -ServerInstance $ResultsInstance -Database $ResultsDatabase -Query $sql

Write-Output "Total number of people run is "$TotalPeople.count