Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all 3333 articles
Browse latest View live

2012 Scripting Games Beginner Event 3: Create a File in a Folder

$
0
0

 2012 Scripting Games badge

Summary: Beginner Event 3 of the 2012 Scripting Games requires you to create a file in a folder that may or may not exist.

About this event

Division

Beginner

Date of Event

4/4/2012 12:01 AM

Due Date

4/11/2012 12:01 AM

 

Event scenario

You are a local user, and you want to document the processes that are running on your computer. You decide that you would like to list the name and process ID of each process running on your computer. You want to save this information in a nested folder. Because you use a number of different computers, you are never certain that the folder will exist. The folder is nested off the root directory in the following location:

C:\2012SG\event3

The following image illustrates the folder/file arrangement.

Image of folder

The image that follows illustrates the contents of the Process3.txt file.

Image of command output

Design points

  • If you do not have permission off the root, create the nested folder where you have permissions. Keep in mind that the folder MUST be nested, and it must be named 2012sg\event3. The file must be named Process3.txt.
  • Your code should check to see if the folder exists. If it does not exist, it should be created, and then the Process3.txt file should be created. If it does exist, the Process3.txt file should be created.
  • Your script should not generate errors (that are ignored), even if it does create the Process3.txt.
  • Your script does not have to have command-line parameters, but if you want to include them (with a default path that solves this problem), you will gather additional points.
  • If you choose to add command-line parameters, you should include comment-based Help that describes the parameters, and a sample example of use.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 


Scripting Wife's suggestions for Beginner Event 2

$
0
0
Summary: The Scripting Wife offers her suggestions for Beginner Event 2 in the 2012 PowerShell Scripting Games. Hi everyone. Sorry I have not gotten to blog earlier today, I have been really tied up with Beginner Event 3, and I wanted to get it submitted...(read more)

2012 Scripting Games Advanced Event 4: Determine Folder Space

$
0
0

 2012 Scripting Games badge

Summary: In Advanced Event 4, you are required to determine which folders consume the most space.

 

About this event

Division

Advanced

Date of Event

4/5/2012 12:01 AM

Due Date

4/12/2012 12:01 AM

 

Event scenario

You are a freelance journalist, and as a result, you travel to various locations around the world in search of the latest and greatest news. You create a folder for each story you work on. The folder holds the Word document that contains the basic story, numerous picture files, and an Excel spreadsheet that details location, contacts, expenses, and publications that have been offered the story. If you recast a story for additional markets, that story appears in a subfolder. All of the folders are grouped by year.

The folder arrangement works great, and it simplifies data backup, but the problem is that your laptop appears to be running out of disk space. It might be possible to archive story folders from previous years, but the problem with that idea is that you often like to use pictures that were not published in the earlier stories in later articles. Therefore, a simple archive by date might not be the most effective solution.

You decide to write a Windows PowerShell script that reports folder size so that you can make a more informed decision to reclaim the most disk space and yet maintain access to the most useful content from previous years. An appropriate output is shown in the image that follows.

Image of command output

Design points

  • You do not need to check for admin rights in this script. Assume you have permissions to access the folders you want to check.
  • You should return a custom object to permit further analysis and sorting.
  • You should display folder size in the most appropriate unit of measurement. Therefore, do not report 1073741824 bytes when you could just as easily report 1 Gigabyte.
  • You should report sizes accurate to two decimal places. Do not report 1.23456794 bytes; instead, round it to 1.23 bytes.
  • You should accept a command-line parameter that permits you to supply a specific folder. You should recurse by default.
  • You should consider adding comment-based Help.
  • You should consider creating reusable functions as part of this script.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 

2012 Scripting Games Beginner Event 4: Compare Two Folders

$
0
0

 2012 Scripting Games badge

Summary: In Beginner Event 4, you are required to compare two folders after completing to a backup.

About this event

Division

Beginner

Date of Event

4/5/2012 12:01 AM

Due Date

4/12/2012 12:01 AM

 

Event scenario

You are a network administrator for a small company. Each night you use a script to perform a backup of critical data files. This backup copies the contents of the data directory to a networked location. This is a file-based backup, and each night a complete backup occurs. You are not doing a differential or an incremental backup. Recently, your boss has been expressing doubts about this procedure.

Your task is to use Windows PowerShell to illustrate to your boss that the content of the network backup folder is the same as the content of the data folder. Because file sizes and write times change all the time, you only need to prove to your boss that files with the same names exist in both locations. To meet your bosses’ requirements, your solution merely needs to point out files in either folder that do not exist in the other folder. The output that is shown here is acceptable.

Image of command output

The following script should be used to create two folders for you to compare.

SetupBeginnerEvent4.ps1

# This script creates two folders off of the root.

# It then creates 20 empty files in each folder.

# Next it deletes one random file from each folder.

Push-Location

$path = "C:\"

1..2 |

% {

 Set-Location -Path (md -path "$path\$_").fullname

 1..20 | % {New-Item -Name "$_.txt" -ItemType file}

 Remove-Item -Path ("{0}.txt" -f (Get-Random -Minimum 1 -Maximum 20))

 }

Pop-Location

Design points

  • You should return file information objects as the result of your comparison.
  • You only need to compare file names, not file sizes, dates, or contents.
  • Your output should list files that exist in one folder, but not in the other folder.
  • Because the object is to prove to your boss that your backup works, you do not need to write a complicated script. Unnecessary complexity will cost you points.
  • Extra points for using native Windows PowerShell cmdlets, and for simplicity of code.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 

Scripting Wife’s hints for Beginner Event 3 2012

$
0
0
Summary: The Scripting Wife offers her suggestions for completing the PowerShell Scripting Games Beginner Event 3 Wow. This year, there are nearly twice the number of people competing in the 2012 Scripting Games. There are a few more judges than last...(read more)

2012 Scripting Games Advanced Event 5: List Errors

$
0
0

 2012 Scripting Games badge

Summary: In Advanced Event 5, you will produce a report that lists the number of errors from all the traditional logs on a particular server.

About this event

Division

Advanced

Date of Event

4/6/2012 12:01 AM

Due Date

4/13/2012 12:01 AM

Event scenario

You are an analyst on the server team of a medium-sized organization. You are studying the performance and reliability of various servers on the network, and you decide to produce a report that lists the number of errors from all the traditional logs on a particular server. The code you use should be capable of running against a local computer or against an arbitrary number of remote computers. An acceptable output is shown here.

 Image of command output

Design points

  • You will use impersonation for all remote connections, so you do not need to be able to supply credentials.
  • You should not display errors due to permissions or due to no events matching your filter.
  • You should not make changes to the users’ environment. If you do, you should change them back at the end of the script. Modification to the users’ environment following script completion will cost you points.
  • Your script should run without prompting against the local computer.
  • Your output should be organized such that the largest source of errors appears at the top of the output.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 

2012 Scripting Games Beginner Event 5: Provide a Source and Errors

$
0
0

 2012 Scripting Games badge

Summary: In Beginner Event 5, you are required to provide the source and number of errors from the application log.

 About this event

Division

Beginner

Date of Event

4/6/2012 12:01 AM

Due Date

4/13/2012 12:01 AM

Event scenario

You are a junior server admin on a newly formed team at a medium-sized enterprise company. Your manager is attempting to gain an understanding of which applications are causing problems on your servers. He therefore tasks you to produce a report from each server. The report should list the source and the number of errors from the application log on each server. An appropriate output is shown in the following image.

Image of command output

Design points

  • Your solution should be capable of running against a remote machine, but you do not need to do that for this scenario.
  • You do not need to check for admin rights. Assume that you will have permissions to query the application log on the computer.
  • You should use standard Windows PowerShell cmdlets.
  • Your solution does not need to be long, involved, or complicated. It should be simple and straightforward.
  • You do not need to write to a text file, but your solution should be easily redirectable to a file when needed.
  • You do not need to add comment-based Help.
  • Extra points for sorting the output so that the source with the greatest number of errors appears on the top of the list.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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

Scripting Wife’s Suggestions for Beginner Event 4

$
0
0

Summary: The Scripting Wife offers her suggestions for completing the PowerShell Scripting Games Beginner Event 4.

I want to get this done now. I am meeting some friends for breakfast, and I do not know when I will be back. Sometimes, at least with some friends, it takes nearly all day to have breakfast. Not that we eat all the time, but we go shopping afterwards. Being in the middle of the Scripting Games, I do not think we will go shopping all day, but then that is the great thing about having a week for an event—it lets me take a day off if I want to do so.

One thing I will say is that I like this year's events better than last year's. It is still hard, but I do not have to write a 100-line script (not that I ever did write a 100-line script...just saying). This event is no exception. My answer was a one-line script. That is right, I was able to do this in one line of code. And I actually thought this event was pretty easy. Here are my specific comments.

1. There is a cmdlet that specializes in comparing two different things. Use the Get-Command cmdlet and find that cmdlet. Next use Get-Help to show you how to use that cmdlet. Here is my super secret trick: When you use the Get-Help cmdletuse the –Online parameter. It will open up the latest Help from the TechNet site. This includes all the latest changes and fixes. The Help on the computer only gets updated with Service Packs, so it is not as up-to-date as the Help on the web.

2. We only need to compare file names. This means that you need to get a directory listing. Because the folders are not nested, you do not need to make your cmdlet that gets directory listings do a recursive search.

3. My one-line solution was pretty simple, so I meet the not very complex requirement. I also used native Windows PowerShell cmdlets. I hope I get five stars for my entry, because I do not know anything I missed. But you never can really tell.

Good luck to you. I need to run, so I will talk to you tomorrow. I hope you are enjoying the Scripting Games as much as I am.

~Scripting Wife


Six Observations about 2012 Scripting Games Early Entries

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, discusses some of the early entries to the 2012 Windows PowerShell Scripting Games.

Microsoft Scripting Guy, Ed Wilson, is here. One of the things that is always a challenge in creating the Scripting Games is trying to come up with the right balance between challenge, practicality, and appropriateness. In some respects, it is like attempting to walk a six inch I-beam 100 stories up in the air. It looks more precarious that it really is. But like one sage told Harry Potter, “You best keep your wits about you.” And so it is with the Scripting Games—you best keep your wits about you.

Complexity is not always required

This year, I tried. I really did try to reduce complexity. One of my goals for this year’s Scripting Games is to force you into the documentation (think about using the Get-Help cmdlet on a regular basis) so that you will discover some hidden gem hidden amongst the thousands of parameters that exist for the hundreds of Windows PowerShell cmdlets. Obviously, I was not entirely successful.

The point is this: In Windows PowerShell, lots of tasks are ridiculously simple when you have figured out the secret. Of course, when a parameter exists and it is documented in Get-Help, I am not sure that it really qualifies as a secret—but hey, who am I to argue?

Some things really are easy

It has become nearly a cliché since I first wrote that there are three important cmdlets in Windows PowerShell. These three cmdlets are Get-Help, Get-Command, and Get-Member. It was really important in Windows PowerShell 1.0 when I first set this down. For beginners, in Windows PowerShell 2.0 (and especially in Windows PowerShell 3.0), I do not think that Get-Member is quite as important as the other two discovery cmdlets. This is because each new version adds new cmdlets and additional parameters that bring interesting members to the foreground, therefore make discovering obscure members less important. Even with Windows PowerShell 3.0, using Get-Member is important for the more advanced scripter. But each new version makes it easier to perform common tasks, so digging into the object model becomes less of a daily task. I think that these days, I use Select-Object and Measure-Object more often than I use Get-Member.

Scripting is not required

I know they are called the Scripting Games, but this is more a legacy artifact of the VBScript world that spawned the first Scripting Guys and consequently the first Scripting Games. Nearly all of the Beginner events in this year’s Scripting Games (and many of the Advanced events) can be completed with a one-liner. To some, these famously (or infamously) powerful commands epitomize the elegance of the Windows PowerShell language. You can save a one-liner in a .ps1 file and voila, you have a script. Of course, you can also save your one-liner in a text file if you wish. In fact, I actually do both. I have lots of .txt files that contain various one-line Windows PowerShell commands that I spent a decent amount of time creating; and therefore, I wanted a way to save the product of my brilliance.

Keep in mind that at its most basic element, a Windows PowerShell script is simply a collection of one or more Windows PowerShell commands. If I can type it in the Windows PowerShell console, I can place it in a .ps1 file, and I have a Windows PowerShell script. This is one of the great things about Windows PowerShell—it is a shell. It is also a scripting language. It is also a shell. It is also a scripting language. It is like a chocolate peanut butter cup—it is chocolate, but it is also peanut butter. When I teach a Windows PowerShell class, I generally spend the first two days working exclusively inside the Windows PowerShell console. But, I make it really clear to the students that everything we are doing applies to Windows PowerShell scripting. I love a tweet Jeffrey Hicks made when he showed dumping the commands from command history to a .ps1 file and automatically creating a script from his Windows PowerShell console command history. This is the perfect example that working interactively at the Windows PowerShell console is, in a real sense, scripting.

Now, let’s get back to my Windows PowerShell classes. Even after I finally introduce the Windows PowerShell ISE, I still end up doing many examples directly in the Windows PowerShell console. One reason is because the Windows PowerShell console has a built in transcription tool, and the Windows PowerShell ISE does not. Being able to hand out a transcript of everything that I typed in a class makes a great teaching tool. It also prevents writer’s cramp on behalf of the students.

Sometimes it seems that students are disappointed they do not do more traditional scripting in my Windows PowerShell classes. But the simple fact is that in the Windows PowerShell era, quite often scripting is not required. Many traditional tasks that required lengthy and complex scripts in the VBScript world, are simple one-liners in Windows PowerShell. Like I said, if you miss scripting, go ahead and save your one-liners as .ps1 files.

A great work ethic does not require lengthy scripts

In Windows PowerShell, often you do not need to write a big, long, complex script. In fact, I would say that on many occasions, this is true. It has quickly become the rule, rather than the exception. I can also say from experience, at times I am still too quick to fire up the Windows PowerShell ISE. After all these years of exclusively writing Windows PowerShell on nearly a daily basis, I am still surprised at how easy things can be accomplished. So what am I really saying? In Windows PowerShell, often more work goes into producing a one-liner than in creating a 15-line script. When I am making presentations to a Windows PowerShell User Group, I am often asked questions, and I will respond by firing up the Windows PowerShell ISE and dashing off a quick script to illustrate the technique I am trying to explain. At times, someone in the group will ask why I did not do XYZ. Often the answer is that I did not think of doing XYZ. Windows PowerShell is so powerful, that there is often more than one way of accomplishing the same task.

In the end, it is about getting work accomplished

In the real world, and in the real world of the 2012 Scripting Games, the end game is to accomplish work. As long as a script or a command accomplishes the task at hand, it works. Jeffrey Snover once remarked that no one learns Windows PowerShell simply to learn Windows PowerShell. Rather, they use Windows PowerShell to solve a specific problem. This is one thing I try to reflect in the Scripting Games—real-world problems seeking real-world solutions. But I do disagree with Jeffrey in one important respect. I know lots of people who have absolutely fallen in love with Windows PowerShell simply because Windows PowerShell is fun, powerful, and a great tool. Many people, including myself, find ourselves playing with Windows PowerShell and exploring the intricacies of Windows PowerShell because of this fun factor. The person who wrote Space Invaders in Windows PowerShell did not do so because his boss asked him for a Windows PowerShell port of Space Invaders. Rather he did it because Windows PowerShell is so fun to play with, and it is great that it has the ability to create fun games like Space Invaders.

Great solutions to regular problems

One reason I love the Scripting Games is that I also view myself as a learner. Whereas I write solutions to each of the tasks, I am often surprised at the sheer elegance of some of the solutions that come in from the participants. In many cases, I look at code and wonder, “Does that work?” I stare in amazement when it really does. This is one of the most exciting things about Windows PowerShell—there are many many many ways of accomplishing the same tasks. The great thing about the 2012 Scripting Games is that you work out solutions to real-world problems. I have had numerous emails from participants in prior Scripting Games where they thanked me for the games and stated that they had already had occasion to use a script at work that they wrote for the games. Cool. Learning that is fun, and useful—a great concept.

Join me tomorrow when I will continue to talk about some of the concerns I saw while grading scripts.

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. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Scripting Wife hints for Beginner Event 5

$
0
0
Well I don’t know about where you live, but it is an absolutely perfect day today in Charlotte, North Carolina. I hope you are enjoying the 2012 Scripting Games, and that you are finding them both challenging and fun. That is, at least so far, the way...(read more)

Eight Top Tricks for Stellar Success in the Scripting Games

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, shares his top eight tricks for achieving stellar success in this year’s Windows PowerShell Scripting Games.

Microsoft Scripting Guy, Ed Wilson, is here. OK. Today I figure you are all busy working on catching up with the first five events of the 2012 Scripting Games. You are not, you say? Well then, it is not too late. You have until 11:59 PM tomorrow night (Pacific Time (-8 GMT)) to complete Event 1. While you are at it, go ahead and knock out Events 2 through 5. Completing all five events (at the beginner level) should not take you more than a few hours of concentrated, diligent work.

Top suggestions for success in the 2012 Scripting Games

If you have never competed in the Scripting Games, you are in for a treat. They are amazingly fun, but they can also be a real challenge. Here are some suggestions to help you succeed in this year’s games.

  1. Read the entire scenario. If a scenario states that you are running all Windows 2000 computers, it means you will be limited to using the Get-WmiObject cmdlet and running remote WMI commands. On the other hand, if the scenario states that all servers are running Windows Server 2008 R2, all workstations are running Windows 7, and Windows PowerShell remoting has been enabled on all systems, you will be able to use the full resources of Windows PowerShell 2.0 in your solution.
  2. Pay close attention to the Design points, but do not ignore the scenario. If the Design points say that you do not need to write a script—you can use a single Windows PowerShell command, then do not submit a 200-line script.
  3. Open Notepad and outline what you want to accomplish. Specify inputs to the script and desired outputs from the script. Write down the steps that you know how to accomplish, and make notes of things that will require you to perform research to answer. This file, when completed, serves as a great blueprint for your answer.
  4. Open the Windows PowerShell console and the Windows PowerShell ISE and begin writing the solution. Make sure that what you thought would work does in fact work. Next add comments for the portions that will need additional information and development.
  5. Pay close attention to the resources in the 2012 Scripting Games Study Guide. I know the Study Guide is considered a prep tool, but it is also a great resource for learning and for researching solutions to problems. After all, the Scripting Games, at its most basic level, is simply a collection of problems.
  6. Pay close attention to the resources that are provided by running Help cmdlet name  -full | more. It is the extended Help that comes from using the –Full parameter that will often provide just the information you need to help you come up with an exceedingly elegant solution to a problem.
  7. Don’t feel bad about searching through previous Hey, Scripting Guy! Blogs in search of solutions to problems. Pay particular attention to the Scripting Wife blogs when it is time to solve basic problems. The Scripting Wife is a beginner; therefore, it is likely that she has faced similar situations to those that you might see.
  8. Don’t be shy when it comes to searching the Scripting Guys Script Repository. The Script Repository is a great place to find answers to common problems.

These suggestions also work for the real world

The above seven suggestions are listed in the context of the 2012 Scripting Games, but they also apply to any scripting problem that you may face at home or at work. In particular, it is important to understand the needs of the script, and how the script will work in your environment. I have found that writing down what I know and what I need to know is a great place to start when writing a custom solution. When it comes to searching for a piece of missing information that I need for a solution, I generally begin by searching the Hey, Scripting Guy!  Blog. (Hey, I cannot remember everything I write). Next, I search MSDN. Finally, I will perform a search using Bing. Generally, my search takes the form of “PowerShell” plus whatever I need.

Last minute suggestions for the 2012 Scripting Games

Remember, the primary goal of the 2012 Scripting Games is for you to improve your skills with Windows PowerShell. If you are already an advanced scripter when it comes to Windows PowerShell, I hope you become even more advanced. If you cannot even spell Windows PowerShell, I hope you will become familiar with Windows PowerShell and learn the basics. To do this, you may want to watch the following five-part series:

After you watch the videos, take the Windows PowerShell quizzes. The 2012 PowerShell Scripting Games Quiz relates to the Windows PowerShell for the Busy Admin series. The Windows PowerShell Basics quiz relates to the Windows PowerShell: Learn It Now Before It’s an Emergency series.

Well, that is all for today. I am going to get back to grading the scripts that have been submitted, and you can use the extra time to work on your answers or watch the videos. Join me tomorrow when the 2012 Scripting Games returns with Event 6.

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. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

2012 Scripting Games Beginner Event 6: Compute Uptime for Local Computer

$
0
0

 2012 Scripting Games badge

Summary: In Beginner Event 6, you are required to compute the uptime for the local computer.

About this event

Division

Beginner

Date of Event

4/9/2012 12:01 AM

Due Date

4/16/2012 12:01 AM 

Event scenario

You are the main network administrator for a small company. As such, your duties consist of a variety of functions, including assisting the Help Desk when they get behind. Recently, your manager has become concerned about server uptime. He wants you to write a script that will display how long a server has been “up.” He said that he is not concerned with anything fancy—he just wants a general idea. The only real guidance he provided for the task is, “Use WMI. There is a class called Win32_OperatingSystem that should do the trick for you. Tell me the server name, and how many days, hours, and minutes the server has been up.”

The output that is shown in the image that follows meets the boss’s requirements.

Image of command output

Design points

  • Your solution only needs to run locally.
  • Your script should be easy to read and easy to understand.
  • Your script is only concerned with the uptime for the local server, calculated with the current time. Do not worry about time zones or about comparing up times with different computers.
  • Pay attention to the formatting of the output and the script itself.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 

2012 Scripting Games Advanced Event 6: Compute Uptime for Multiple Servers

$
0
0

 2012 Scripting Games badge

Summary: In Advanced Event 6, you are required to compute the uptime for multiple servers.

About this event

Division

Advanced

Date of Event

4/9/2012 12:01 AM

Due Date

4/16/2012 12:01 AM

Event scenario

You are the server lead for a medium-sized enterprise, and your boss has tasked you with computing the uptime of all servers on the network on a daily basis. This information will be stored in a Microsoft Excel spreadsheet for ease of analysis. Because of the large number of servers, the script will take some time to run; therefore, your boss has decided to compute all uptime as of 8:00 AM local time. If a server reboots after 8:00 AM, but before the report runs, the uptime is zero, and it will be factored into the report for the next day. Create a new report file each day for all the servers on the list. An acceptable output is shown in the image that follows.

Image of command output

Design points

  • Your script should be capable of running more than once per day. If it does, it should not generate an error.
  • Create the CSV file in the logged-on users Documents special folder.
  • The file name should use the year, month, and day that the report runs, with the addition of _Uptime to the file. It will appear like the following: 20120409_Uptime.csv
  • The column headers should appear on the first row only. The headings should be ComputerName, Days, Hours, Minutes, Seconds, and Date.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 

So, how are the 2012 Scripting Games going thus far …

$
0
0
Summary: Microsoft Scripting Guy Ed Wilson provides a look from inside the trenches about how the 2012 Scripting Games are progressing. Wow! That is about all I have got to say. Perhaps that is because I am struggling to catch my breath. How are the 2012...(read more)

2012 Scripting Games Advanced Event 7: Search Windows Logs

$
0
0

 2012 Scripting Games badge

Summary: In Advanced Event 7, you are required to search all Windows logs for the most recent event.

About this event

Division

Advanced

Date of Event

4/10/2012 12:01 AM

Due Date

4/17/2012 12:01 AM 

Event scenario

You are troubleshooting a problem with your Windows 7 laptop, and you hope to find some clues to the recent performance issues by examining recent entries from various Windows logs. You have recently become aware that there are nearly 500 logs available in a standard Windows 7 installation, but you do not feel like manually searching through all of the logs by using the Event Viewer utility. You decide to use Windows PowerShell to come to the rescue. You want to write a command that will display the most recent one-event log entry from each event log and troubleshooting log that is enabled and has at least one entry in it. Crucial information for this process includes the log name, time of the event, the event ID, and the event message. An acceptable output is shown in the image that follows.

Image of command output

Design points

  • Your code should not display any errors.
  • Your code should query hidden logs if they are enabled and they contain at least one entry.
  • You should display only the most recent entry from each log.
  • The event log entries should be sorted so that the most recent entry appears first.
  • You must display the following required properties: time of the event, the name of the log, number of the event ID, and the event details.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 


2012 Scripting Games Beginner Event 7: Display a List of Enabled Logs

$
0
0

 2012 Scripting Games badge

Summary: In Beginner Event 7, you are required to display a list of all enabled logs on the computer that contain at least one entry.

 

About this event

Division

Beginner

Date of Event

4/10/2012 12:01 AM

Due Date

4/17/2012 12:01 AM

 

Event scenario

On a Windows 7 computer, nearly 500 logs provide auditing and troubleshooting capabilities. Many of these logs do not record any information unless an administrator enables them. You were recently discussing this information with your boss, and he asked a rather logical question:

“What logs actively record information on a Windows 7 computer at any given time?”

You were, of course, somewhat taken aback, and headed off to TechNet to find the answer. After about fifteen minutes of searching and clicking around, you were no closer to the answer than when you began. Your boss came over to you and suggested that you use Windows PowerShell to find the answer. The following image represents an acceptable type of output.

Image of command output

Design points

  • Your code should not display any errors when run.
  • Your code should display all logs that have entries in them.
  • Your code should display only logs that are enabled.
  • Your code should display any enabled hidden logs that contain entries in them.
  • You should display the complete log name, and the number of entries in the log.
  • The number of entries in the logs should be displayed in descending order (the log with the most entries in it should appear on the first line of the output).
  • You do not need to display a total count of the number of enabled logs that have entries.
  • The requirements for this scenario can be met with a “one liner” (a one line logical command). Depending on the width of your Windows PowerShell console and the screen resolution, it may occupy more than one physical line).
  • You do not need to write comment-based Help or accept command-line parameters (or anything like this). Your goal is simply to provide a bit of information to your boss—a “one liner” is perfectly acceptable.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 

Scripting Wife Tips for 2012 Scripting Games Beginner Event 6

$
0
0

Summary: The Scripting Wife provides tips and hints for the 2012 Scripting Games Beginner Event 6.

These past couple of days, Ed has been tied up, so I have free reign of the house. This means I have a lot of projects I want to complete. For some reason, when he is around, it seems like he often gets under foot–a situation especially problematic when I have a detailed game plan. Anyway, this means that I am up early so I can maximize my time. I completed Beginner Event 6, and I want to provide you with some good hints.

First of all, this is the most difficult event (at least for me) so far of the 2012 Scripting Games (on the beginner track). This means that you should take your time, and it also means that you will probably need to do some research before you write your script.

The first thing I needed to do was to find what property would tell me when the server would boot. I used the Get-WMIObject cmdlet and piped the results from the WMI class the boss told me to use to Get-Member. It did not take very long at all to find the property I needed. I then needed to change the format of that date value.

One of the things you will need to know is how to convert a WMI date into something more usable. I looked at all of the blogs about dates listed in the 2012 Scripting Games Study Guide, and I did not really find what I was wanted. But I then did a search (by using the Search This Blog feature on the Hey, Scripting Guy! Blog), and I was able to find exactly the information that I needed. The exact search words I used were wmi date, and it returned several hits. The one I needed was called Manipulating Dates Returned by Windows Management Instrumentation (WMI).

The next thing I had to look up was was how to compute the amount of time between two dates. I found this information in the Date section of the 2011 Scripting Games Study Guide...come to find out, it was a Scripting Wife blog! Oh yeah, I remember that now.

The last part, formatting the output, required me to mess around with substituting values for parameters. This can get a little tricky, but there are lots of ways to get the same result.

Hope this helps. Remember, you have until 12:01 AM on April 16, 2012 (-8 Pacific time) (or basically midnight on the night of April 15, 2012) to submit your entry. This one would definitely be a good one to set aside and come back to later. Good luck, and happy scripting.

~Scripting Wife

Randomly Drawn Prize Winners: 2012 Scripting Games Day 1

$
0
0
Thank you for entering the 2012 Scripting Games . A special thank you to our wonderful sponsors for making these prizes possible. Please be sure to visit the sponsors page for more information about these awesome companies. We will send the prize winners...(read more)

2012 Scripting Games blog roll

$
0
0
Summary: The Scripting Wife reveals the blogs she has been reading to stay current for the 2012 Scripting Games Good afternoon, I had some free time this afternoon and wanted to say a few words about the 2012 Scripting Games. This seems like...(read more)

2012 Scripting Games Advanced Event 8: Enable Network Adapters

$
0
0

 2012 Scripting Games badge

Summary: In Advanced Event 8, you are required to enable and disable network adapters.

 

About this event

Division

Advanced

Date of Event

4/11/2012 12:01 AM

Due Date

4/18/2012 12:01 AM

Event scenario

You are a network consultant. As a result, you travel extensively. Your main computer is your laptop. At times, you may use a wireless network adapter to connect to wireless networks. When you are using wireless, you disable the wired network connection. And at times, you use a wired network connection—when you use a wired network connection you disable the wireless network adapter. You do this to prevent bridging. Currently you use the graphical interface, and it seems to take nearly five minutes to make these changes. You would like to use Windows PowerShell to toggle your network connections. Your task in this scenario is to write a simple Windows PowerShell command to disable all enabled network connections, and enable all disabled network connections. The default output from running the script is sufficient.

Design points

  • This task requires admin rights; therefore, your script should check to ensure that it is running as an admin. If the script is not launched with proper credentials, display a message that admin rights are required.
  • For the purposes of this scenario, you want one adapter enabled and the other disabled. If more than one adapter is enabled or disabled, you should display information about the adapters, and prompt the user for which one to enable or disable. You do not want the wired network adapter and the wireless network adapter enabled at the same time.
  • This scenario is appropriate for a laptop, but not for a desktop computer. Therefore, your script should detect if the computer is a laptop or a desktop machine. If the computer is a desktop, the script should display a message that it only runs on laptop computers.
  • The script should run without prompting if the default condition of one enabled network adapter and one disabled network adapter is met.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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 

Viewing all 3333 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>