我试图监视大小达到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使用进程监视器,看看有什么操作被称为文件。
我会自己做,并直接回答你的问题,但我不得不玩我的PS安全设置 – 我没有时间做,因为我有一个会议在5分钟内,首先需要咖啡。 另外,你会学到几个东西,也build立字符:-)