如何将列记录放入列表中

时间:2017-09-18 13:39:54

标签: sql-server powershell

我有2个PowerShell脚本我想要合并我有这个用于进行数据转储

    $query = "use [ISTABLocalDB]
    SELECT  
        Item.[ID] as PartIdDB
        ,Item.[ItemNumber] as Varenummer

        ,ImageFile.[ResourceFile_ID] as ImageID
        ,ImageFile.[Description] as ImageName
            , CASE WHEN ImageFile.[ResourceFile_ID] is null 
            THEN ''
            ELSE 
                CONCAT('F:\App\ISTAB.Data\pictures_global\P\', 
                SUBSTRING(CONVERT(varchar, Item.[ID]), 1, 3), '\', 
                SUBSTRING(CONVERT(varchar, Item.[ID]), 4, 3), '\', 
                SUBSTRING(CONVERT(varchar, Item.[ID]), 7, 3), '\', 
                ImageFile.[ResourceFile_ID],'-g')
            END as PathOnDrive
        ,Item.[ItemType_ID]"

   $extractFile = "$path $date.csv"


$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $query
$command.Connection = $connection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()

$DataSet.Tables[0] | Export-Csv $extractFile  -NoTypeInformation

enter image description here

这个用于复制图片的地方,我在查询中从PathOnDrive到$imagesList

的路径中手动粘贴
$targetFolderName = "C:\test"
$sourceFolderName = "F:\App\ISTAB.Data\pictures_global\P"

$imagesList = (
"F:\App\ISTAB.Data\pictures\P\122\338\7\1326647",
"F:\App\ISTAB.Data\pictures\P\179\924\0\1678117"
)

foreach ($itemToCopy in $imagesList)
{
    $targetPathAndFile =  $itemToCopy.Replace( $sourceFolderName , $targetFolderName )
    $targetfolder = Split-Path $targetPathAndFile -Parent

    if (!(Test-Path $targetfolder -PathType Container)) {
        New-Item -Path $targetfolder -ItemType Directory -Force
    }

    Copy-Item -Path $itemToCopy -Destination   $targetPathAndFile 
}

所以我的问题是如何自动将PathOnDrive列中的所有记录都放入我的$imagesList

1 个答案:

答案 0 :(得分:2)

我想通了

foreach ($Columns in $DataSet.Tables[0].Rows) {       
      $imagesList = "$($Columns.PathOnDrive)"
      write-Output "$($imagesList)"


$targetFolderName = "D:\DataFeed\Pictures\Parts"
$sourceFolderName = "F:\App\ISTAB.Data\pictures_global\P"


foreach ($itemToCopy in $imagesList)
{
    $targetPathAndFile =  $itemToCopy.Replace( $sourceFolderName , $targetFolderName )
    $targetfolder = Split-Path $targetPathAndFile -Parent

    if (!(Test-Path $targetfolder -PathType Container)) {
        New-Item -Path $targetfolder -ItemType Directory -Force
    }

    Copy-Item -Path $itemToCopy -Destination   $targetPathAndFile 
}
}