Powershell

對多使用者 Access 2003 數據庫執行文件大小監控 powershell 腳本是否安全?

  • August 20, 2012

我正在嘗試監視當大小達到 2GB 時損壞的 MS Access 數據庫文件的大小

我找到了以下監控文件大小的powershell 腳本,然後執行命令。

該腳本用於(Get-ChildItem $FilePath).length確定被監視文件的大小。雖然我想假設這樣做只是一個讀取操作,但它將監視多使用者 MS Access 數據庫的大小這一事實並沒有真正為假設只是一個讀取操作留下空間,所以我想我如果是這種情況,會詢問一些更有經驗的 Powershell 使用者。

我正在使用 Powershell 2,因為這是 shell $Host.Version輸出

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

為方便起見,這裡是 powershell 腳本的全文:

function Test-FileSizeUntil
{

 <#

.Synopsis
   Checks the file size of a given file until it reaches the specified size and creates an action
.Description
   Controls the size of a given file on user-specified intervals; and creates an action when the file size is equal or greater than the given size
.Parameter FilePath
   Path of the file that will be monitored
.Parameter Size
   File size is monitored against this value. When file size is equal or greater than this value, user-specified action is triggered
.Parameter ScriptString
   Specifies the action commands to run. Enclose the commands in double quotes ( " " ) to create a script string
.Parameter Interval
   The wait interval between the executions of the function. The value of this parameter is  in second and default value is 30 seconds
.Parameter AsJob
   Creates a background job for the cmdlet and echoes the jobID
.Example
Test-FileSizeUntil -FilePath C:\inetpub\logs\LogFiles\W3SVC1\u_ex091112.log -Size 500KB -ScriptString "c:\dosomething.exe"
.Example
Test-FileSizeUntil c:\test.txt 10MB -ScriptString "Start-Job -ScriptBlock { do something }"
You can omit -FilePath and -Size parameters and call the function as you can see above
.Example
Test-FileSizeUntil c:\test.txt -Size 1GB -Interval 3600 -AsJob $true -ScriptString "do something"
Wait interval between executions is one hour and function is called as a background job
.Link
   http://blogs.msdn.com/candede
.Notes
   Author: Can Dedeoglu
#>

 param
 (
   [Parameter(Mandatory = $true,Position = 0)]
   [string[]]$FilePath
   ,
   [Parameter(Mandatory = $true,Position = 1)]
   [int]$Size
   ,
   [Parameter(Mandatory = $true)]
   [string]$ScriptString
   ,
   [Parameter(Mandatory = $false)]
   [int]$Interval = 30
   ,
   [Parameter(Mandatory = $false)]
   [bool]$AsJob = $false
 )

 function control
 {
   function subControl
   {
     while ((Get-ChildItem $FilePath).length -le $Size)
     {
       Start-Sleep -Seconds $Interval
       subControl
     }
   } # end function subControl

   subControl

   Invoke-Command -ScriptBlock ([scriptblock]::Create($ScriptString))

 } # end function control

 if (!$AsJob)
 {
   if ((Test-Path $FilePath))
   {
     control
   } #end if Test-Path

   else { "File does not exist!" }

 } # end if AsJob

 else
 {
   Start-Job -ScriptBlock ([scriptblock]::Create(". c:\ps\library\Test-FileSizeUntil.ps1; Test-FileSizeUntil -FilePath $FilePath -Size $Size -ScriptString `"$ScriptString`" -Interval $Interval"))
 }

} # end function Test-FileSize

你知道,我沒有答案,但我會告訴你如何自己回答這個問題。針對您自己機器上的已知文件執行腳本。使用 Sysinternals 的 Process Monitor 並查看針對文件呼叫了哪些操作。

我會自己做,直接回答你的問題,但我必須玩弄我的 PS 安全設置——我沒有時間去做,因為我在 5 分鐘後要開會,需要先喝咖啡。另外,您將學到幾件事並建立性格:-)

引用自:https://serverfault.com/questions/417918