Link to home
Start Free TrialLog in
Avatar of SAM2009
SAM2009Flag for Canada

asked on

How to run a PS command for X min every Y sec?

Hi,

I run this PowerShell command in a script and lunched from different servers.

Example:

$env:computername | out-file "\\server\log\log.txt" -Append

The problem is if I lunch that script from many servers at the same time I got sometimes a msg says: The file is busy....

How can I make it try for 1 at very 5 sec or is there other way to code?

Thanks
SOLUTION
Avatar of bbao
bbao
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SAM2009

ASKER

Thanks but what this mean: Start-Sleep -Milliseconds Get-Random -Maximum 100 -Minimum 1
Wait for a random time between 1 and 100 milliseconds.
That line is missing parens:
      Start-Sleep -Milliseconds (Get-Random -Maximum 100 -Minimum 1)

Open in new window

EXPERT CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As a workaround, it will be simpler to have different log file names for each run script. is not it ?
Using sleep or any time based delays means you will always have the possibility of underestimating the duration needed. The suggestion for the retry is one approach but builds in a race condition.
A simpler approach to build into the script a method that tries to reserves a right to access before it is allowed access to the file (a built-in option should be that a file whose reservation was more than an expected duration for the script run, deals with a system shutdown after the access was reserved)

This incorporates the suggestion Michael made but in a different way with a different reason for the approach.

Depending on what it is you have as far as other resources and what it is you are doing, getting the data into a DB might be a better option if available.

A different option, is to have the servers copy their logs out to a central server that crunches the data .
Avatar of SAM2009

ASKER

Thanks to all!
you are welcome.