none
Scripting with Get-Content RRS feed

  • Question

  • I'm currently working on a script that utilizes:

    $file = Get-ChildItem -Path 'c:\path\to\file\logfilename*' | 
    	Sort-Object LastAccessTime -Descending | 
    	Select-Object -First 1
    	
    Get-Content -Path $file -Tail 1 -Wait |
    	ForEach-Object{
    		Send-MailMessage -Body $_ @mailprops
    	}

    The 'logfilename*' is directed to a log file that is generated automatically from a program that gets the timestamp in its name. Every time the service restarts it keeps creating a new log file with a new name and causes me to close my script and run the powershell script again. I keep trying to put it in a while loop or something to be able to 'restart' the script and grab the latest file that was created without having to close the powershell window and reopen it.

    The lines of text in the log file are then emailed to my personal email and when a new line is wrote to the log file then it sends a separate email. The script works normally but I kind of want to be able to have it run automatically without much direction from me if possible

    Is there something that I am missing?

    Thanks,

    • Moved by Bill_Stewart Wednesday, November 29, 2017 6:31 PM This is not "scripts on demand"
    Tuesday, October 10, 2017 10:18 PM

All replies

  • You will constantly have to check the service for a restart.  This would not be a one or two line solution.  My suggestion is to fix the service. Why does the service keep restarting?  Check with the vendor for help solving this issue.

    You could also use the FileSystemWatcher to constantly look for a new file and have it set a flag each time a new file is created.


    \_(ツ)_/

    Tuesday, October 10, 2017 10:33 PM
  • Well I should clarify all the details. I was trying to keep it short and sweet just in case it was a simple fix that I was missing.

    The "service" is actually a process that runs to keep a game server going. I have a program that manages the updates of the server and any mods installed. It checks for updates every hour and when it detects there is an update it installs the update and restarts the process/game server.

    Well every time the game server restarts it creates that new log file. I use my current script to read the log file and inform me of what happens while I am away. The program that manages the server has a built in email system that only informs me of when the server is updating so I know when to go in and restart my script.

    I was looking into having a scheduled task just restart the script every 15 minutes after the new hour but I would get repeat notifications of events in game. So I was hoping there was a way to have the script start again while it was already running to check and see if a new log file was created or that it was using the latest log file.

    Sorry I probably made this more confusing.

    Thanks,

    Wednesday, October 11, 2017 2:56 PM
  • Attach your script to the event log event when the service stops.  Stop the previous script.  Launch your script on the service start event and let it get the new file.


    \_(ツ)_/

    Wednesday, October 11, 2017 4:33 PM
  • I'll see if I can do that. So far I haven't been able to see if there is an event that registers when it does this auto update and restarts the game server.

    I forgot to add this detail earlier. The new log file doesn't get generated until something happens in the game world that is log worthy. Such as someone dying or saying something in global chat. So that is why I was hoping that there was a way that the powershell script could check every hour or so if a new file was created and if so then use that log file to run the get-content foreach loop.

    I have tried to suggesting to the dev team to allow us to set a static name for the log file so the log file name never changes even after the game server restarts. Unfortunately I don't know if they even see my suggestion or are working on implementing it.

    I'll keep trying to figure out a work around method that works until they do possibly allow setting a static log file name.

    Wednesday, October 11, 2017 4:48 PM