Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 从CMD运行PS1时,输出文件无法访问文件_File_Powershell_Out - Fatal编程技术网

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

File 从CMD运行PS1时,输出文件无法访问文件,file,powershell,out,File,Powershell,Out,当我在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 $sou

当我在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:\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
Terrorist525.SQl在
$start | Out File-filepath$target1-append
当我从
$end1>中检查信息上的SQL文件时,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

您在试图运行脚本时没有打开文件,是吗?哈哈,绝对没有。事实上如果存在的话。脚本要做的第一件事就是删除它。直接从powershell运行时,脚本也可以正常运行。对于单次运行,您可以尝试其他文件名吗?您的文件似乎仍然可以写入。请尝试添加$writer.Close();直到你的脚本结束