如何使用Powershell

时间:2018-07-11 17:44:38

标签: image powershell tags

我创建了一个小的powershell脚本,用于将SQL Server DB(存储在varbinary中)中的图像提取到服务器中的文件中。它完美地工作!问题是我想根据数据库中的信息向这些.jpeg文件添加标签。我在网上找不到任何解决方案。有Powershell专家吗,我需要您的帮助:-)

谢谢

Sabrina

$SqlBH = "MYSQLQUERY"
# Open DB Connection 
$con = New-Object Data.SqlClient.SqlConnection;
$con.ConnectionString = "Data Source=$Server;" +
"Integrated Security=False;" +
"Initial Catalog=$Database; User ID = $uid; Password = $pwd;";
$con.Open();

# New Command and Reader
$cmd = New-Object Data.SqlClient.SqlCommand $SqlBH, $con;
$rd = $cmd.ExecuteReader();

# Create a byte array for the stream.
$out = [array]::CreateInstance('Byte', $bufferSize)

# Looping through records
While ($rd.Read())
{
Write-Output ("Exporting file: {0} ParentID: {1}" -f 
$rd.GetString(0),$rd.GetString(2));
# New BinaryWriter

# Create parent folder if not present
$fl = $rd.GetString(2)
$Destfl = $Dest + $fl
$FileExists = Test-Path $Destfl
If ($FileExists -eq $False) {new-item $Destfl -itemtype directory}

$fs = New-Object System.IO.FileStream ($Destfl + $rd.GetString(0)), Create, 
Write;
$bw = New-Object System.IO.BinaryWriter $fs;

$start = 0;
$received = $rd.GetBytes(1, $start, $out, 0, $bufferSize - 1);
While ($received -gt 0)
{
$bw.Write($out, 0, $received);
$bw.Flush();
$start += $received;
$received = $rd.GetBytes(1, $start, $out, 0, $bufferSize - 1);
}

$bw.Close();
$fs.Close();
}

# Closing & Disposing all objects
$fs.Dispose();
$rd.Close();
$cmd.Dispose();
$con.Close();

Write-Output ("Finished");

1 个答案:

答案 0 :(得分:0)

已经有些挣扎了,我已经实现了以下解决方案。我希望它能在某个时候帮助某人:-)

基于库Exiv 2(http://www.exiv2.org/),我在第70行($ fs.Close();)之后添加了以下代码:

# Tag images
$cmd_exe '.\exiv2.exe'
$arg1 = '-M'
$arg2_BH = """add Iptc.Application2.Keywords String " + $rd.GetString(3) + """"
$arg2_LSD = """add Iptc.Application2.Keywords String " + $rd.GetString(4) + """"
$arg2_logger = """add Iptc.Application2.Keywords String " + $rd.GetString(5) + """"
$arg3 = $Destfl + $rd.GetString(0)

& $cmd_exe $arg1 $arg2_BH $arg1 $arg2_LSD $arg1 $arg2_logger $arg3   

就这样:-)