不完全确定我在这里做错了什么,与Powershell没什么关系
我正试图让Powershell:
这是我到目前为止提出的代码
Get-ChildItem '\\FolderPath\*.txt'
%{copy-item -path '\\FolderPath\*.txt' -destination '\\FolderPath\Master.txt'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=MYSERVER;Database=MYDATABASE;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "MYPROCEDURE"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
Remove-Item '\\FolderPath\Master.txt'
}
但是一旦它击中第一个文件,它就会停止并且不再循环。
我想%
它应该循环??
答案 0 :(得分:0)
%
是PowerShell中Foreach
循环的别名。您需要将pipe
文件列表foreach
循环到|
。 Get-ChildItem '\\FolderPath\*.txt' |
%{copy-item -path '\\FolderPath\*.txt' -destination '\\FolderPath\Master.txt'
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=MYSERVER;Database=MYDATABASE;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "MYPROCEDURE"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0]
Remove-Item '\\FolderPath\Master.txt' }`
是Powershell中的管道运算符。你的代码看起来像这样 -
array