I'm try to monitor the size of an MS Access Database file which corrupts when the size reaches 2GB.
I found the following powershell script which monitors the size of a file, and then runs a command.
The script uses (Get-ChildItem $FilePath).length
to determine the size of the file being monitored. While I'd like to assume doing this is only a read operation, the fact that it will be monitoring the size of a multi-user MS Access database doesn't really leave room for assuming just a read-operation, so I thought I would ask some more experienced Powershell users if that is the case.
I'm using Powershell 2 as this is shells $Host.Version output:
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
And for convenience here is the full text of the powershell script:
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