I'm trying to find the max by using
function ExecuteSqlQuery ($SQLQuery) {
try
{
$Datatable = New-Object System.Data.DataTable
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = $connStr
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Command.CommandTimeout=$commandTimeout
$Reader = $Command.ExecuteReader()
$Datatable.Load($Reader)
return $Datatable
}
$data= ExecuteSqlQuery "Select col1,col2,col3, RowVersion from table"
$byteArray = [System.Collections.ArrayList]@()
foreach ($row in $data) {
$byteArray.Add($row.Item("RowVersion"))
}
$max=($byteArray | Measure-Object -Maximum).Maximum
Write-host $max
But I get an error:
ERROR: Error: Cannot compare "System.Byte[]" because it is not IComparable
Then I tried to converting to int64 and finding the max
$byteArray = [System.Collections.ArrayList]@()
foreach ($row in $data) {
$byteArray.Add([bitconverter]::ToInt64($row.Item("RowVersion"),0))
}
Write-Host ($byteArray | Measure-Object -Maximum).Maximum
$max=[Convert]::ToByte(($byteArray | Measure-Object -Maximum).Maximum)
Write-host $max
But still has an error, output:
9.1298706847202E+18
2017-10-25 21:00:51 ERROR: Error: Value was either too large or too small for >an Int32..
Are there any methods to solve this?
Converting a binary to an int32 retuns 0