简体   繁体   English

如何使用powershell在二进制(8)数组中找到最大值?

[英]How to find the maximum in array of binary(8) using powershell?

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错误:错误:无法比较“System.Byte[]”,因为它不是 IComparable

Then I tried to converting to int64 and finding the max然后我尝试转换为 int64 并找到最大值

$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 9.1298706847202E+18
2017-10-25 21:00:51 ERROR: Error: Value was either too large or too small for >an Int32.. 2017-10-25 21:00:51 错误:错误:对于 > 一个 Int32 来说,值要么太大要么太小。

Are there any methods to solve this?有没有办法解决这个问题?
Converting a binary to an int32 retuns 0将二进制转换为 int32 返回 0

binary(8) from SQL should be represented as a [byte[]] in PowerShell, so you have an ArrayList of byte arrays.来自 SQL 的binary(8)应该在 PowerShell 中表示为[byte[]] ,所以你有一个字节数组的 ArrayList。 There's no built-in way to compare a byte array to other byte arrays in the .Net Framework for sorting.没有内置方法可以将字节数组与 .Net Framework 中的其他字节数组进行比较以进行排序。 That behavior is not defined, so you'll have to define it or otherwise define an algorithm.该行为未定义,因此您必须定义它或以其他方式定义算法。

Assuming the first byte is the most significant, the second is the next most significant, and so on, you should be able to get the "maximum" it by doing something like this:假设第一个字节是最重要的,第二个是次重要的,依此类推,您应该能够通过执行以下操作来获得“最大值”:

$byteArray | Sort-Object -Property {$_[0]},{$_[1]},{$_[2]},{$_[3]},{$_[4]},{$_[5]},{$_[6]},{$_[7]} -Descending | Select-Object -First 1

This only looks at the first 8 bytes, but, if the base type was really a binary(8) , there's only 8 bytes in the field.这仅查看前 8 个字节,但是,如果基本类型确实是binary(8) ,则该字段中只有 8 个字节。

If that doesn't work, then you'll have to iterate through the ArrayList and find the maximum yourself by comparing bytes and saving the maximum.如果这不起作用,那么您必须遍历 ArrayList 并通过比较字节并保存最大值来自己找到最大值。

If you want only the max, you can use sql for this :如果您只想要最大值,则可以为此使用 sql:

Select col1,col2,col3, RowVersion from table order by RowVersion desc

Then your first row contain the max然后你的第一行包含最大值

尝试这个

$byteArray.Add([System.Text.Encoding]::ASCII.GetString($row.Item("RowVersion")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM