The 2011 Scripting Games Advanced Event 9: Use PowerShell to Create a File Name Based on Date and Username

The 2011 Scripting Games Advanced Event 9: Use PowerShell to Create a File Name Based on Date and Username

  • Comments 10
  • Likes

2011 Scripting Games badge

Summary: Advanced Event 9 in 2011 Scripting Games uses Windows PowerShell to create a file name that is based on the date and the user’s name.

About this event

Division

Advanced

Date of Event

4/14/2011 12:15 AM

Due Date

4/21/2011 12:15 AM

Event scenario

You need to add a logging routine to a script that runs daily on your workstation. For troubleshooting purposes, you would like to keep all of the log files in a single folder named HSGLogFiles. The exact location of this folder is up to you, but it should be stored on the local machine, and it should be in a location that is accessible to all users of the computer. Because the script runs once per day on a per user basis, the log files should be comprised of the year, month, day, and a logged on user name. You are not worried about concurrent users, or remote users—for the purposes of this scenario assume a single interactive user. If the script runs multiple times for the same user on the same day, do not append or overwrite the original log file (which is created during the first run of the day). In other words, if a file exists, the script should exit gracefully and silently. When you create the file, create it silently by not displaying return information to the Windows PowerShell console. An example of such a log file directory is shown in the following image.

Image of directory

Design points

  • For the purposes of this scenario, you can create the HSGLogFiles directory manually.
  • Extra points for automatically creating the HSGLogFiles directory.
  • Extra points for detecting if the HSGLogFiles directory exists. If it does not exist, extra points for creating it. If it does exist, extra points for allowing the script to respond gracefully.
  • Extra points for creating the HSGLogFiles directory in the current user’s MyDocuments folder instead of creating it off of the root.

2011 Scripting Games links

2011 Scripting Games: All Links on One Page

Submit your scripts on PoshCode

Support our Sponsors!

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Good luck as you compete in this year’s Scripting Games. We wish you well.

Ed Wilson, Microsoft Scripting Guy

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Another interesting one!  :)

    Just a quick question: you say that the folder "should be stored on the local machine, and it should be in a location that is accessible to all users of the computer" but you also say "Extra points for creating the HSGLogFiles directory in the current user’s MyDocuments". But the MyDocuments can't be accessed by everyone... Should we simply let the user chose?

    Thank you!

  • @Nuno Mota

    The purpose of the folder is for logging purposes ... you should think automation when you see this. Stopping a logon script, and asking a user where they want the log folder created is not a good idea -- therefore you should choose. I stated that for the purpose of the scenario you are to assume 1 interactive user of the computer -- therefore mydocuments would be accessible to all users of the computer, because there is only one user of the computer.

  • Further to Nuno's question, when you say "log files should be under a single folder", plus what you just explainec, if there are more than one user use same computer (not concurrently), can I assume that each user should have their own folder to hold logs? (still single folder per user)

    2nd question: for the purpose of this game, this log file can be empty, we just need to make sure the file name is in correct format, correct?

  • @John,  yes each user should have own log (in this scenario the computer only has one user) and the log file can certainly be empty.

  • I don't get where the content of the log file itself should come from. Is our script supposed to receive it as an input?

  • there are some confusions with the extra point questions. I’m hoping you can explain it.

    • For the purposes of this scenario, you can create the HSGLogFiles directory manually.

    Does it mean user can able to create directory manually from Windows explorer, or it is asking to create a switch that can create directory (ex: .\script.ps1 –createdir yes)

    • Extra points for creating the HSGLogFiles directory in the current user’s MyDocuments folder instead of creating it off of the root.

    What does it mean by current user? Does it mean the console “logged on” user, or user that launch the threat?

    and do i have admin rights to run the script?

    Thank you

  • @Pak

    I think that by "you can create the directory manually" they mean we can assume that the directory was created manually (from within the Explorer or any other means) before you run the script. Hence the extra points for the handling of this within the script instead.

    Re: the current user, IamMred already answered this by stating that we should consider there is only one user of the computer.

    That said, I am still personally uncomfortable with the notion of "logging" in this script. Since our script is supposed to create a log file, is it to take its input from another script, or are we just creating a "logging function" that can then be added to any other script that will need the logging functionality?

  • @Pak

    Manually - either the user can create the folder using explorer, or you can create the folder using explorer. For  the scenario you do not HAVE to create the folder from the script. For extra points you should test to see if the folder exists, if it does not then create the folder.

    Yes you can create it in the current user profile for extra points. Detect where the profile is stored, and use that location. YOu can see the Scripting Wife article for this event.

    Current user: the user who is currently logged on interactively to the machine. Not a remote connection to the workstation.

    YOU should NOT require admin rights to run this script. If you do, it will not work for the scenario.

  • @Jacques

    Logging in this scenario ... you are simply creating the file that will be used BY OTHER SCRIPTS. The point of the exercise is to test your ability to detect if a file exists, to create a file name based upon the date, and upon the user name. I also test your ability to convert a datetime object into a string, and to retrieve the current users name. FOr extra credit you must know how to test for a folders existence, how to create a folder, and how to detect the users profile location. THIS is what I am testing in this event. The logging thing, was simply an attempt to show you HOW you might use such a log file in real life. It is quite common for enterprises to need a log file that will be used as output for several different scripts.

  • thanks