We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x
Private

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

Medium Priority
65 Views
Last Modified: 2020-05-16
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
Comment
Watch Question

bbaoIT Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Thanks but what this mean: Start-Sleep -Milliseconds Get-Random -Maximum 100 -Minimum 1
CERTIFIED EXPERT

Commented:
Wait for a random time between 1 and 100 milliseconds.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
That line is missing parens:
      Start-Sleep -Milliseconds (Get-Random -Maximum 100 -Minimum 1)
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Louis LIETAERSystem Infrastructure Architect
CERTIFIED EXPERT

Commented:
As a workaround, it will be simpler to have different log file names for each run script. is not it ?
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
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 .

Author

Commented:
Thanks to all!
bbaoIT Consultant
CERTIFIED EXPERT

Commented:
you are welcome.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.