1

奇妙な動作が見られます。私のマシンでは、PowerShell がレコードセットを返し、レコードを問題なく反復処理できます。私の同僚のマシン (ファイルをコピーする必要があるファイル共有にアクセスできる人) では、実際のレコードではなく、レコード数が返されます。私は何か簡単なものを見逃しているに違いありません。この異なる動作が見られる理由は何ですか?

 $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
 $SqlConnection.ConnectionString = "Server = server; Database = db; Integrated Security = True"
 $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
 $SqlCmd.CommandText = "SELECT fileName from SomeTable"
 $SqlCmd.Connection = $SqlConnection
 $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
 $SqlAdapter.SelectCommand = $SqlCmd
 $DataSet = New-Object System.Data.DataSet
 $SqlAdapter.Fill($DataSet)
 $Table = new-object data.datatable
 $Table = $DataSet.tables[0]
 $SqlConnection.Close() 

 function Out-FileForce {
 PARAM($path)
 PROCESS
 {
     if(Test-Path $path)
     {
         Out-File -inputObject $_ -append -filepath $path
     }
     else
     {
         new-item -force -path $path -value $_ -type file
     }
 }
 }

foreach ($Row in $Table.Rows)
{
    $fullPath = $Row.FullFilePathWithName 
    $path = "\\server\folder\"
    $newPath = "C:\newFolder\"


    $newDestination = $fullPath -replace [regex]::Escape($path), $newPath

    #Write-Output $newDestination
    #Write-Output $fullPath

    # recurse should force the creation of the folder structure
    #Copy-Item $fullPath $newDestination -recurse
    Out-FileForce $newDestination
    Copy-Item $fullPath $newDestination -force
    Write-Output $newDestination " done"

}
4

2 に答える 2

1

この行:-

$SqlAdapter.Fill($DataSet)

後で何かに割り当てるために必要な場合は、行数を返します:-

$rowCount = $SqlAdapter.Fill($DataSet)

または、それを必要としない場合:-

[void]$SqlAdapter.Fill($DataSet)

上記の両方により、1をスキップする必要がなくなります

お役に立てれば

于 2013-08-13T07:47:59.557 に答える
0

修正を考え出しましたが、理由はまだわかりません。

$DataSetTableRows が問題の原因でした

投稿した元のスクリプトを修正します。

これを一番上に追加しました:

$Table = 新しいオブジェクト data.datatable $Table = $DataSet.tables[0]

次に、ループで $Table.Rows を使用しました

于 2013-04-26T20:36:12.890 に答える