2012-01-03 10 views
1

SQL Serverのエラー・ログを読み取る機能がありますが、その時点でサーバが使用しているエラー・ログを読み取ることができないという問題があります。私はgoogle-ingしていると、Fileshareの旗がpowershellのために働いていないようです。ファイルを開くときにFileshareフラグを設定する方法はありますか?バイナリ・リーダでオープン・ファイルを読み取ることができません

 
    function check_logs{ 
     param($logs) 
     $pos 
     foreach($log in $logpos){ 
      if($log.host -eq $logs.host){ 
       $currentLog = $log 
       break 
      } 
     } 
     if($currentLog -eq $null){ 
      $currentLog = @{} 
      $logpos.Add($currentLog) 
      $currentLog.host = $logs.host 
      $currentLog.event = $logs.type 
      $currentLog.lastpos = 0 
     } 
     $path = $logs.file 
     if($currentLog.lastpos -ne $null){$pos = $currentLog.lastpos} 
     else{$pos = 0} 
     if($logs.enc -eq $null){$br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open))} 
     else{ 
      $encoding = $logs.enc.toUpper().Replace('-','') 
      if($encoding -eq 'UTF16'){$encoding = 'Unicode'} 
      $br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open), [System.Text.Encoding]::$encoding) 
     } 
     $required = $br.BaseStream.Length - $pos 
     if($required -lt 0){ 
      $pos = 0 
      $required = $br.BaseStream.Length 
     } 
     if($required -eq 0){$br.close(); return $null} 
     $br.BaseStream.Seek($pos, [System.IO.SeekOrigin]::Begin)|Out-Null 
     $bytes = $br.ReadBytes($required) 
     $result = [System.Text.Encoding]::Unicode.GetString($bytes) 
     $split = $result.Split("`n") 
     foreach($s in $split) 
     { 
      if($s.contains(" Error:")) 
      { 
       $errorLine = [regex]::Split($s, "\s\s+") 
       $err = [regex]::Split($errorLine[1], "\s+") 
       if(log_filter $currentLog.event $err[1..$err.length]){$Script:events = $events+ [string]$s + "`n" }   
      } 
     } 
     $currentLog.lastpos = $br.BaseStream.Position 
     $br.close() 
    }

明らかに、ファイルを開こうとするとエラーが発生します。エラーメッセージは次のとおりです。

Exception calling "Open" with "2" argument(s): "The process cannot access the file 
'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Log\ERRORLOG' 
    because it is being used by another process." 

Gísli

答えて

0

は、だから私は答えを発見し、それはかなり簡単でした。

バイナリリーダーコンストラクタは入力としてストリームを受け取ります。私はストリームを別に定義しなかったので、ストリームのコンストラクタでFileShareフラグを設定していることに気付かなかったのです。これに

{$br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open))} 

::私がしなければならなかった何

これを変更することでした

{$br = New-Object System.IO.BinaryReader([System.IO.File]::Open($path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite))} 

そしてそれは魅力のように働きました。

ギスリ

関連する問題