我一直在尝试创建一个输出文件,该文件写入多个执行脚本,从数组列表中获取某个参数。到目前为止,我得到了混乱的重复输出。如何在每一行上获得一个执行命令?这就是我所拥有的。
$myArray = @(1,2,3)
foreach ($element in $myArray) {
$myobj = "EXECUTE [masterdb].[dbo].[update_rows] @row_num=" +"'"+$element+"'"+","+ "@status = 'Fail'"
$myprocedure += $myobj
$myobj = $null
}
Out-file -filepath $path -inputobject $myprocedure -width 50 -force
答案 0 :(得分:1)
$myprocedure
永远不会被初始化为数组,所以它变成了一个字符串,您只需添加更多文本即可。您需要在执行行的末尾添加换行符:
$myobj = "EXECUTE [masterdb].[dbo].[update_rows] @row_num=" +"'"+$element+"'"+","+ "@status = 'Fail'" + [System.Environment]::NewLine
首先创建一个名为$myprocedure
的空数组:
$myArray = @(1,2,3)
$myprocedure = @()
$path = "test.txt"
foreach ($element in $myArray) {
$myobj = "EXECUTE [masterdb].[dbo].[update_rows] @row_num=" +"'"+$element+"'"+","+ "@status = 'Fail'"
$myprocedure += $myobj
$myobj = $null
}
Out-file -filepath $path -inputobject $myprocedure -width 50 -force
或者在文件中附加3次:
$myArray = @(1,2,3)
$path = "test.txt"
#remove-item $path if necessary
foreach ($element in $myArray) {
"EXECUTE [masterdb].[dbo].[update_rows] @row_num=" +"'"+$element+"'"+","+ "@status = 'Fail'" | Out-file -filepath $path -width 50 -force -Append
}