从System.Data.SqlDbType无效转换为System.Data.DbType

时间:2019-03-06 11:54:03

标签: powershell sqlite powershell-v2.0

我正在编写一个脚本,以将cookie导入到Google chrome。

在Powershell 3上,我的脚本可以正常工作,但是在Powershell 2中,尝试插入字节数组时出现问题。错误是:

Cannot convert argument "1", with value: "Binary", for "Add" to type "System.Data.DbType":
"Cannot convert value "Binary" to type "System.Data.DbType".
Error: "Invalid cast from 'System.Data.SqlDbType' to System.Data.DbType'.""
At C:\Users\root\Desktop\processhacker-2.39-bin\1.ps1:90 char:22
+         $sql.Parameters.Add <<<< ("@encrypted_value", [System.Data.SqlDbType]"binary").value = $byte;

脚本如下:

Add-Type -AssemblyName System.Security #connect  DPAPI

$parent_dll = $MyInvocation.MyCommand.Path | Split-Path -Parent
$sqlite_library_path = $parent_dll+"\System.Data.SQLite.dll" #path to dll SQLite
[void][System.Reflection.Assembly]::LoadFrom($sqlite_library_path)

$con = New-Object -TypeName System.Data.SQLite.SQLiteConnection
$path = Get-ChildItem "C:\Users\$env:username\AppData\Local\Google\Chrome\User Data\Default\cookies" -Recurse

function decrypt_password ($enc_pass) {
    $bytes = $enc_pass.encrypted_value.ToCharArray() | % {[byte] $_}
    $decrypt_char=[System.Security.Cryptography.ProtectedData]::protect($bytes, $null, [Security.Cryptography.DataProtectionScope]::LocalMachine) #| % {[byte] $_}
    return $decrypt_char
}

$encrypted_array = import-Csv -Path .\c.txt
$con = New-Object -TypeName System.Data.SQLite.SQLiteConnection
$con.ConnectionString = "Data Source=$path"
$con.Open()

foreach ($item in $encrypted_array) {
    $item.encrypted_value=decrypt_password -enc_pass $item
    $sql = $con.CreateCommand();
    $sql.commandtext = "INSERT INTO cookies (encrypted_value) VALUES (@encrypted_value)";
    $sql.Parameters.Add("@encrypted_value", [System.Data.SqlDbType]"binary").value = [byte[]]$item.encrypted_value;
    $fff=$sql.ExecuteNonQuery()
    $sql.Dispose()
}

$con.Close()

0 个答案:

没有答案