I have 2 PowerShell scripts I wanna combine I have this one for doing data dumps
我有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
and this one for copy pictures where I manually pasting in the path which I get from PathOnDrive in the query into the $imagesList
这个用于复制图片的地方,我在查询中从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
}
so my question is how do I get the all the records from the PathOnDrive column into my $imagesList
automatically
所以我的问题是如何自动将PathOnDrive列中的所有记录都放入我的$ imagesList中
1 个解决方案
#1
2
i figured it out
我想到了
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
}
}
#1
2
i figured it out
我想到了
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
}
}