我创建了一个小型 powershell 脚本,用于将图像从 SQL 服务器数据库(存储在 varbinary 中)提取到服务器中的文件中。它完美地工作!问题是我想根据数据库中的信息向这些 .jpeg 文件添加标签。我没有在网上找到任何解决方案。那里有 Powershell 专家吗,我需要你的帮助:-)
谢谢,
萨布丽娜
$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");
最佳答案
已经有些挣扎,我已经实现了以下解决方案。我希望它能在某个时候对某人有所帮助:-)
基于库 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
就是这样:-)
关于image - 如何使用 Powershell 为图像添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291487/