我试图监控一个MS Access 数据库文件大小达到 2GB 时会损坏。
我发现以下内容powershell 脚本监视文件的大小,然后运行命令。
该脚本用于(Get-ChildItem $FilePath).length
确定被监视文件的大小。虽然我想假设这只是一个读取操作,但事实上它将监视多用户 MS Access 数据库的大小,这实际上并没有留下假设只是读取操作的空间,所以我想我会问一些更有经验的 Powershell 用户是否是这种情况。
我正在使用 Powershell 2,因为这是 shells $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
答案1
你知道,我没有答案,但我会告诉你如何自己回答这个问题。在你自己机器上针对已知文件运行脚本。使用 Sysinternals 的 Process Monitor,查看针对文件调用了哪些操作。
我会自己做并直接回答你的问题,但我必须调整我的 PS 安全设置——我没有时间做这件事,因为我 5 分钟后要开会,需要先喝杯咖啡。此外,你会学到很多东西,也会培养性格 :-)