基于SQL查询的Powershell重命名文件

时间:2019-09-13 03:10:02

标签: sql powershell sql-server-2012

我们为产品图像拍了很多照片。我找到了一个软件,该软件可以读取嵌入在图像中的条形码,并将图像重命名为图像中的条形码,我现在要执行的操作是使用Powershell在SQL查询中用<< / p>

    declare @$CliRef nvarchar(max)
set @$CliRef = '5015807416980'

Select   convert(nvarchar(max), productid) as ID FROM products WHERE products.Barcode = @$CliRef

我在网上找到了Powershell脚本,但由于将文件重命名为空白而不是productid,我必须做错什么了,我真的是Powershell的新手,所以有点卡住了,所以任何帮助都值得赞赏

    $Database = "training"
$Server = "ART-WM01"

##Not sure i need this bit 

function Select-Info($CliRef) 
   {

  $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security = True"
$SqlConnection.Open()

    $conn.open()
    $query = “Select convert(varchar,producdid )as ID FROM products WHERE barcode = '$CliRef'”
    $cmd = New-Object System.Data.SqlClient.SqlCommand($sql,$conn)
    $cmd.connection = $SqlConnection
    $cmd.commandtext = $query
    $result = $cmd.executenonquery()
    $SqlConnection.Close()

    return $query
    }

## Return ID from Database
 function Return-Info($CliRef)
{

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security = True"
$SqlConnection.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.commandtext = “Select convert(varchar, productid) as ID FROM products WHERE barcode = '$CliRef'”
$cmd.connection = $SqlConnection
$result = $cmd.ExecuteScalar()
$SqlConnection.Close()

return $result

}

 ## Collect the image names
 $FiNms = Get-ChildItem C:\JJC\Test_Dump -Name

 ## Loop through each Image name
 foreach ($FiNm in $FiNms)

{

 ## Variable for current File path
 $file = “C:\JJC\Test_Dump\” + $FiNm
 ##  Variable for new File path
 $newFile = “C:\JJC\Test_Dump\renamed” + $FiNm

$ID = Return-Info $FiNm

$ID = $ID + “.tiff”

##Copy-Item $file -Destination $newFile 

##Copy-Item $file -Destination = “C:\JJC\\Test_Dump\renamed\”
Rename-Item $file $ID -force

}

enter image description here

0 个答案:

没有答案