admin管理员组

文章数量:1391960

When I run this from the PowerShell command line

Invoke-Sqlcmd -TrustServerCertificate -ServerInstance "HELLODANNY\inst0100" 
              -Database "come" -outputas datarows 
              -Query "select count(*) from andplaywithus"   

I get this output in the terminal.

Column1
-------
     74

However when I put the database invocation into a loop

# Define a condition to control the loop
$counter = 0
$maxIterations = 1005  # Number of times to run the query

while ($counter -lt $maxIterations) {
    $result = Invoke-Sqlcmd -TrustServerCertificate -ServerInstance "HELLODANNY\inst0100" -Database "come" -outputas datarows -Query "select count(*) from andplaywithus"
    Write-Host "Result: $($result)"

    # Increment the counter to avoid an infinite loop
    $counter++

    # Optionally add a delay between iterations
    Start-Sleep -Seconds 20
}

I get

Result: System.Data.DataRow

this might work

$result = Invoke-Sqlcmd -TrustServerCertificate -ServerInstance "HELLODANNY\inst0100" -Database "come" -outputas datarows -Query "select count(*)as count from andplaywithus"
    Write-Host "Result: $($result[0].count)"

本文标签: databasepowershell invokesqlcmd SystemDataDataRowStack Overflow