当我在Powershell ISE或常规PowerShell窗口中运行以下脚本时,脚本运行正常,并根据我的CSV(Tab Seperated)文件构建我的.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:\Users\First.Last\Documents\Working\terrorist525.sql' because it is
being used by another process.
At C:\Users\First.Last\Documents\Working\OSFI.PS1:233 char:202
+ ... ual,bname2 " | Out-File -filepath $target1 -append -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], IOException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.Ou
tFileCommand
在行$start | Out-File -filepath $target1 -append
之前不存在Terrorist525.SQl
当我检查来自$end1 | Out-File -filepath $target1 -append
的信息的SQL文件使其成为文件。
答案 0 :(得分:1)
$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