2011-12-29 24 views
0

VBScriptを使用すると、複数のコンピュータからのテキストファイルへの複数の同時書き込みを許可する方法はありますか?私は複数のコンピュータから同時にスクリプトを実行する必要があります。スクリプトはDEFRAGを実行し、結果をテキストファイルに保存します。その後、スクリプトはDEFRAGログファイルから読み取り、断片化率を引き出し、エンタープライズ全体のすべてのコンピュータからのこれらの結果を含む別のMASTERログファイルに書き込みます。私が常に一度に1台のコンピュータからスクリプトを実行するだけであれば、すべてのものが見つかるし、素敵です。ただし、配布ポイントを使用して企業にスクリプトを送信すると、複数のコンピュータが同時にMASTERログファイルにアクセスしようとするまで、スクリプトは完全に機能します。それは私がアクセス拒否エラーなどに遭遇しているときです。ここに私がこれまで持っていたものがあります...複数のコンピュータからのテキストファイルへの同時書き込み

strDrivePreCheckStarted = (Year(Date) & "-" & AEZiR(Month(Date)) & "-" & AEZiR(Day(Date)) & " @ " & AEZiR(Hour(Time)) & ":" & AEZiR(Minute(Time)) & ":" & AEZiR(Second(Time))) 

a=("===================================================================================") 
b=(" Script started on " & strDrivePreCheckStarted) 

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 
Set colSettings = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem", , 48) 
For Each objComputer in colSettings 
    CompNam = objComputer.Name 
    If CompNam = "" Then 
     CompNam = "ComputerNameNotFound" 
    End If 
    CompMfr = replace(objComputer.Manufacturer,",","") 
    If CompMfr = "" Then 
     CompMfr = "ComputerMfrNotFound" 
    End If 
    CompMdl = objComputer.Model 
    If CompNam = "" Then 
     CompNam = "ComputerModelNotFound" 
    End If 
Next 

Set dClient = GetObject("winmgmts://" & CompNam & "/root/ccm:SMS_Client") 
Set result = dClient.ExecMethod_("GetAssignedSite") 
mClient = result.sSiteCode 
If mClient = "OLD" Then 
    mClient = "SMS" 
End If 
If mClient = "NEW" Then 
    mClient = "SCCM" 
End If 
If mClient = "" Then 
    mClient = "UNKNOWN" 
End If 

c=(" Computer: " & "[" & mClient & "] " & CompNam & " (" & CompMfr & " " & CompMdl & ")") 

Set FSO = CreateObject("Scripting.FileSystemObject") 
Set wshShell = CreateObject("WScript.Shell") 
Set objShell = WScript.CreateObject("WScript.Shell") 
Set strWinDir = FSO.GetSpecialFolder(0) 
Set strSys32 = FSO.GetSpecialFolder(1) 
Set strTempDir = FSO.GetSpecialFolder(2) 
strLogsDir = "\\fileserver\shared\logs\" 

Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Const OverwriteExisting = False 

If FSO.FileExists(strLogsDir & CompNam & ".TXT") Then 
    WScript.Quit 
Else 
    If FSO.FileExists(strLogsDir & CompNam & "_CHKDSK.LOG") Then 
     WScript.Quit 
    Else 
     If FSO.FileExists(strLogsDir & CompNam & "_DEFRAG.LOG") Then 
      WScript.Quit 
     Else 

      strCHKDSKStarted = (Year(Date) & "-" & AEZiR(Month(Date)) & "-" & AEZiR(Day(Date)) & " @ " & AEZiR(Hour(Time)) & ":" & AEZiR(Minute(Time)) & ":" & AEZiR(Second(Time))) 

      d=("  CHKDSK Started: " & strCHKDSKStarted) 

      If FSO.FileExists(strSys32 & "\chkdsk.exe") Then 
       strCHKDSKReturn = objShell.Run("%COMSPEC% /c chkdsk.exe C: > " & chr(34) & strLogsDir & CompNam & "_CHKDSK.LOG" & chr(34), 0, True) 
      End If 

      If FSO.FileExists(strLogsDir & CompNam & "_CHKDSK.LOG") Then 
       Set ChkDskLog = FSO.OpenTextFile(strLogsDir & CompNam & "_CHKDSK.LOG", ForReading, True) 
       Do While ChkDskLog.AtEndOfStream <> True 
        Curline = ChkDskLog.ReadLine 
        If InStr(Curline, "KB in bad sectors.") Then 
         Curline = Trim(Curline) 
         strKBpos = InStr(1, Curline, "KB")-2 
         strBadKB = Left(Curline, strKBpos) 
         strBadKB = Trim(strBadKB) 
         If strBadKB > 0 Then 
          ChkDskFail = "Failed" 
          e=("  " & strBadKB & "KB Of Bad Sectors Found In Used Space") 
          f=("  Drive has Failed Integrity Check") 
         Else 
          ChkDskFail = "Passed" 
          e=("  " & strBadKB & "KB Of Bad Sectors Found In Used Space") 
          f=("  Drive has Passed Integrity Check") 
         End If 
        End If 
       Loop 
       If strBadKB = "" Then 
        ChkDskFail = "Failed" 
        e=("  Check Disk Log Existed But Was Incomplete: " & Date & " @ " & Time) 
        f=("  Drive has Failed Integrity Check") 
       End If 
      Else 
       ChkDskFail = "Passed" 
       e=("  No Bad Sectors Found in Used Space") 
       f=("  Drive has Passed Integrity Check") 
      End If 
      ChkDskLog.Close 

      strCHKDSKFinished = (Year(Date) & "-" & AEZiR(Month(Date)) & "-" & AEZiR(Day(Date)) & " @ " & AEZiR(Hour(Time)) & ":" & AEZiR(Minute(Time)) & ":" & AEZiR(Second(Time))) 

      g=("  CHKDSK Finished: " & strCHKDSKFinished) 

      strDEFRAGStarted = (Year(Date) & "-" & AEZiR(Month(Date)) & "-" & AEZiR(Day(Date)) & " @ " & AEZiR(Hour(Time)) & ":" & AEZiR(Minute(Time)) & ":" & AEZiR(Second(Time))) 

      h=("  DEFRAG Started: " & strDEFRAGStarted) 

      If FSO.FileExists(strSys32 & "\defrag.exe") Then 
       strDEFRAGReturn = objShell.Run("%COMSPEC% /c defrag.exe C: -a -v > " & chr(34) & strLogsDir & CompNam & "_DEFRAG.LOG" & chr(34), 0, True) 
      End If 

      If FSO.FileExists(strLogsDir & CompNam & "_DEFRAG.LOG") Then 
       Set DefragLog = FSO.OpenTextFile(strLogsDir & CompNam & "_DEFRAG.LOG", ForReading, True) 
       Do While DefragLog.AtEndOfStream <> True 
        CurLine = DefragLog.ReadLine 
        If InStr(CurLine, "Total fragmentation") Then 
         FragPosition = InStr(1,CurLine," %",1)-2 
         CurLine = CurLine 
         strFragAmount = Right(CurLine,4) 
         strFragAmount = Left(strFragAmount,2) 
         strFragAmount = Ltrim(strFragAmount) 
        End If 
       Loop 
      Else 
       DefragFail = "Failed" 
       i=("  Log File Could not be Located. Please try Again.") 
       j=("  Drive has Passed Defragmentation Check") 
      End If 
      If strFragAmount = "" Then 
       DefragFail = "Failed" 
       i=("  An Unknown Error has Occured. Please try Again.") 
       j=("  Run 'DEFRAG -v' from this machine manually.") 
      Else 
       If strFragAmount < 30 Then 
        DefragFail = "Passed" 
        i=("  Drive is " & strFragAmount & "% Fragmented") 
        j=("  Drive has Passed Defragmentation Check") 
       End If 
       If strFragAmount >= 30 Then 
        DefragFail = "Failed" 
        i=("  Drive is " & strFragAmount & "% Fragmented") 
        j=("  Drive has Failed Defragmentation Check") 
       End If 
      End If 
      DefragLog.Close 

      strDEFRAGFinished = (Year(Date) & "-" & AEZiR(Month(Date)) & "-" & AEZiR(Day(Date)) & " @ " & AEZiR(Hour(Time)) & ":" & AEZiR(Minute(Time)) & ":" & AEZiR(Second(Time))) 

      k=("  DEFRAG Finished: " & strDEFRAGFinished) 

      strDrivePreCheckFinished = (Year(Date) & "-" & AEZiR(Month(Date)) & "-" & AEZiR(Day(Date)) & " @ " & AEZiR(Hour(Time)) & ":" & AEZiR(Minute(Time)) & ":" & AEZiR(Second(Time))) 

      l=(" Script finished on " & strDRIVEPreCheckFinished) 
      m=("===================================================================================") 

      Set TXTLog = FSO.CreateTextFile(strLogsDir & CompNam & ".TXT", True) 
      TXTLog.WriteLine(strDrivePreCheckStarted & "," & mClient & "," & CompNam & "," & CompMfr & "," & CompMdl & "," & strCHKDSKStarted & "," & strBadKB & "KB" & "," & ChkDskFail & "," & strCHKDSKFinished & "," & strDEFRAGStarted & "," & strFragAmount & "%" & "," & DefragFail & "," & strDEFRAGFinished & "," & strDrivePreCheckFinished) 
      TXTLog.Close 

      If FSO.FileExists(strLogsDir & "_FDE.CSV") Then 
       Set CSVLog = FSO.OpenTextFile(strLogsDir & "_FDE.CSV", ForAppending, True) 
       CSVLog.WriteLine(strDrivePreCheckStarted & "," & mClient & "," & CompNam & "," & CompMfr & "," & CompMdl & "," & strCHKDSKStarted & "," & strBadKB & "KB" & "," & ChkDskFail & "," & strCHKDSKFinished & "," & strDEFRAGStarted & "," & strFragAmount & "%" & "," & DefragFail & "," & strDEFRAGFinished & "," & strDrivePreCheckFinished) 
      Else 
       Set CSVLog = FSO.CreateTextFile(strLogsDir & "_FDE.CSV", True) 
       CSVLog.WriteLine("Pre-Check Started,Management Client,Asset Tag,Computer Manufacturer,Computer Model,CHKDSK Started,CHKDSK Bad Sectors,CHKDSK Results,CHKDSK Finished,DEFRAG Started,DEFRAG Amount,DEFRAG Results,DEFRAG Finished,Pre-Check Finished") 
       CSVLog.WriteLine(strDrivePreCheckStarted & "," & mClient & "," & CompNam & "," & CompMfr & "," & CompMdl & "," & strCHKDSKStarted & "," & strBadKB & "KB" & "," & ChkDskFail & "," & strCHKDSKFinished & "," & strDEFRAGStarted & "," & strFragAmount & "%" & "," & DefragFail & "," & strDEFRAGFinished & "," & strDrivePreCheckFinished) 
      End If 
      CSVLog.Close 

      If FSO.FileExists(strLogsDir & "_FDE.LOG") Then 
       Set InstallLog = FSO.OpenTextFile(strLogsDir & "_FDE.LOG", ForAppending, True) 
       InstallLog.WriteLine(b) 
       InstallLog.WriteLine(c) 
       InstallLog.WriteLine(d) 
       InstallLog.WriteLine(e) 
       InstallLog.WriteLine(f) 
       InstallLog.WriteLine(g) 
       InstallLog.WriteLine(h) 
       InstallLog.WriteLine(i) 
       InstallLog.WriteLine(j) 
       InstallLog.WriteLine(k) 
       InstallLog.WriteLine(l) 
       InstallLog.WriteLine(m) 
      Else 
       Set InstallLog = FSO.CreateTextFile(strLogsDir & "_FDE.LOG", True) 
       InstallLog.WriteLine(a) 
       InstallLog.WriteLine(b) 
       InstallLog.WriteLine(c) 
       InstallLog.WriteLine(d) 
       InstallLog.WriteLine(e) 
       InstallLog.WriteLine(f) 
       InstallLog.WriteLine(g) 
       InstallLog.WriteLine(h) 
       InstallLog.WriteLine(i) 
       InstallLog.WriteLine(j) 
       InstallLog.WriteLine(k) 
       InstallLog.WriteLine(l) 
       InstallLog.WriteLine(m) 
      End If 
      InstallLog.Close 

      FSO.DeleteFile(strLogsDir & CompNam & "_CHKDSK.LOG") 
      FSO.DeleteFile(strLogsDir & CompNam & "_DEFRAG.LOG") 

     End If 
    End If 
End If 

Function AEZiR(plngValue) 
    Dim pstrValue 
    Dim plngChars 
    Dim i 
    pstrValue = CStr(plngValue) 
    plngChars = Len(pstrValue) 
    If plngChars < 2 Then 
     For i = 1 to plngChars Step -1 
      pstrValue = "0" & pstrValue 
     Next 
    End If 
    AEZiR = pstrValue 
End Function 

私は何が欠けていますか?この作業はVBScriptでも可能ですか?前もって感謝します。

答えて

1

のVBScript、またFileSystemObjectオブジェクトでもないが、他の誰か/事はそれを行う必要があり、ロック、提供したよう:

  • に同時可能にする適切なファイルロックと[スクリプト]言語
  • データストア(DBMS)をアクセス
  • 私の意見では、ファイルアクセス

を制御するセマフォ(例えば名前を変更したファイル)を使用して、プログラマ、第三の選択肢はので、最悪ですプログラマはすべての作業を行い、全てのリスク/責任を に取らなければなりません。データベースを使用すると、すぐにアクセスコントロールの問題が解決されます。 適切な言語の を使用すると、標準/非ハッキングの ソリューションが許可されます。

しかし、あなたは面白い時代に生きたいならば - それは私が離れて取得しよう コードの構造です:

While it make sense to continue (# of tries, timeout, successfully written) 
    rename FileIsFree.log to FileIsLocked.log 
    If success 
    open FileIsLocked.log 
    write to FileIsLocked.log 
    close FileIsLocked.log 
    rename FileIsLocked.log to FileIsFree.log 
    break/exit 
    End If 
End While 
If Not successfully written 
    Panic 
End If 

追加: 思考のためのいくつかの食べ物: discussion code

結論に達することを願っています.DBMSを使用する方が良い考えです。

+0

まず、超高速応答に感謝します!したがって、あなたが言及した最後のオプションが最も簡単な方法であるかのように見えます。しかし、それでもスクリプトを実行してから待っていれば、現在開いているファイルに書き込んだ後に完了して終了するかどうか確認することができますか? –

+0

上記の構造をさらに詳細に拡張することができますか?私は本当にそれがVBScriptになると覚えている。ありがとうございました。 –

関連する問題