从CMD输出文件运行PS1时无法访问文件



当我在Powershell ISE或常规powershell窗口中运行以下脚本时,脚本运行良好并构建了我的.基于我的 CSV(制表符分隔(文件的 SQL 查询文件。 但是,如果我从CMD包装器运行相同的脚本,它将失败。

$site1.rawcontent | out-file $source1 ASCII -Width 9999
$site2.rawcontent | out-file $source2 ASCII -Width 9999
$site3.rawcontent | out-file $source3 ASCII -Width 9999
$site4.rawcontent | out-file $source4 ASCII -Width 9999
$site5.rawcontent | out-file $source5 ASCII -Width 9999
$start | Out-File -filepath $target1 -append
$infile = $source1
$reader = [System.IO.File]::OpenText($infile)
$writer = New-Object System.IO.StreamWriter $file1;
$counter = 1
try {
while (($line = $reader.ReadLine()) -ne $null)
{
$myarray=$line -split "t" | foreach {$_.Trim()}
if ($myarray[0] -Match "d{1,4}.d{1,3}" -and $myarray[1] -ne {$null}){
$myarray[1] = $myarray[1] -replace "'","''"
$myarray[2] = $myarray[2] -replace "'","''"
$myarray[3] = $myarray[3] -replace "'","''"
$myarray[4] = $myarray[4] -replace "'","''"
$myarray[5] = $myarray[5] -replace "'","''"
"Insert into #terrorist Select convert(varchar(60),replace('OSFI Name: "+$myarray[1],$myarray[2],$myarray[3],$myarray[4],$myarray[5]+,"','''''','''')), no_,branch,name,surname,midname,usual,bname2 " | Out-File -filepath $target1 -append -force
If ($myarray[1] -eq "") {$myarray[1]="~"}
If ($myarray[2] -eq "") {$myarray[2]="~"}
If ($myarray[3] -eq "") {$myarray[3]="~"}
If ($myarray[4] -eq "") {$myarray[4]="~"}
If ($myarray[5] -eq "") {$myarray[5]="~"}
"from cust where cust.surname in ('"+$myarray[2]+,"','"+$myarray[1]+,"','"+$myarray[3]+,"','"+$myarray[4]+,"','"+$myarray[5]+,"') and ( name in ('"+$myarray[1]+,"', '"+$myarray[2]+,"', '"+$myarray[3]+,"', '"+$myarray[4]+,"', '"+$myarray[5]+,"') or 
midname in ('"+$myarray[1]+,"', '"+$myarray[2]+,"', '"+$myarray[3]+,"', '"+$myarray[4]+,"', '"+$myarray[5]+,"') or 
usual in ('"+$myarray[1]+,"', '"+$myarray[2]+,"', '"+$myarray[3]+,"', '"+$myarray[4]+,"', '"+$myarray[5]+,"') or 
bname2 in ('"+$myarray[1]+,"', '"+$myarray[2]+,"', '"+$myarray[3]+,"', '"+$myarray[4]+,"', '"+$myarray[5]+,"') ) 
go" | Out-File -filepath $target1 -append -force
}   
#$writer.WriteLine($original);
#Write-Output  $original;
#Write-Output  $newlin
}
}
finally {
$reader.Close()
$writer.Close()
}
$end1 | Out-File -filepath $target1 -append

并给出以下错误。

Out-File : The process cannot access the file
'C:UsersFirst.LastDocumentsWorkingterrorist525.sql' because it is
being used by another process.
At C:UsersFirst.LastDocumentsWorkingOSFI.PS1:233 char:202
+ ... ual,bname2 " | Out-File -filepath $target1 -append -force
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OpenError: (:) [Out-File], IOException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
tFileCommand

恐怖分子525.SQl在线路$start | Out-File -filepath $target1 -append之前不存在 当我检查SQL文件时,$end1 | Out-File -filepath $target1 -append的信息会进入文件。

$start | Out-File -filepath $target1 -append
$infile = $source1
$reader = [System.IO.File]::OpenText($infile)
$writer = New-Object System.IO.StreamWriter $file1;
$writer.Close()
$counter = 1
try {

通过在此特定位置添加 $writer.close((,它解决了这个问题。 谢谢@UnhandledExcepSean

最新更新