Skip to main content

Practice Exercises for PowerShell Intermediate

 


Dear Friend,

If you have already gone through    practice exercises for PowerShell beginner,   This is another opportunity to take your Scripting knowledge to next level.

Please go through the problems, develop a clear understanding, make a rough sketch and apply your PowerShell knowledge to solve the problems.  Use PowerShell's help as much possible in case you need, but don't directly search on the internet. Trust me, solving these problems will definitely boost your confidence.



Problem 1: Service restart on multiple computers and logging.

Write a PowerShell script to read the computer names from a text file Then,

  1. Stop a given service (say Print Spooler service ) and wait for 30 seconds after logging the status into a dedicated log file.
  2. Ensure no child process is alive so that graceful stop of service can be confirmed
  3. If there is any child process, kill it forcefully and log the information into the log file
  4. After waiting for 30 seconds, start the service
  5. Check for the service status, log into the log file and come out.

Next level,  You can see the above process might take around 2 minutes of time for gracefully restarting a service on 1 server.  using a single thread, It is not a good solution if we have to gracefully restart 5 services on 1000 machines(ETA: 10,000 minutes).

So, Use multithreading to improvise the solution. (hint: Invoke-Command or Start-Job might help you here)

 

 

Problem 2:   Event Viewer

Write a quick PowerShell script which,

-> Read multiple server names from a text file

-> Ask the user to specify which log they want to scan- like Application, System etc

-> Upon providing input, ask for event ID which they are looking for

Once this information is provided, Script should scan all the computers event logs for provided eventID and generate a nicely formatted CSV report whose headers should be:

 "MachineName","TimeGenerated","Source","Message"

 

Problem 3: Task Scheduler

There are scheduled tasks Important_Service_Restart and Important_Job_Processing running on 100 machines(you have their names in a text file). Tasks are scheduled to run on the daily basis and they are critical for your business.

You need to write a PowerShell script to run after 15 minutes of task scheduled time and collect the status into a CSV file(each task, each computer).

Next, Read the CSV file and get the name of servers where the last run of the task was failed.

Servers on which one task was failed, action should be sending an email to the support asking them to look into this urgently.

Servers on which both tasks were found in failed status, send an email to support and send a separate email to management informing them about the severity of the situation.

Next, Once your standard solution is ready, improvise your solution to decrease the overall script execution time(hint:; use multi threading)

 

Problem 4: Daily Backup

You have a folder in your computer(say C:\Important\My Coding Practice ).

 

Since you do some code changes on daily basis, you want to set up a daily backup using PowerShell.  Preferred time for backup is 10PM.  The format of backup should be .zip with the date appended in the name.

 

When you are in office, your preferred backup location is a shared folder which is accessible to you by UNC path(say \\XYZ_CORPS\share\associates\personal\).

 

But not all the time, you stay in office till 10 PM, and UNC path is not available outside of office environment. In that case, you have to take a local backup to C:\Archives. Whenever you are in office at the time of scheduled backup, your local backup should also be moved to UNC path as it is safer.

 

Your space in shared folder at work is limited, so you want to ensure that no more than last 30 backups are available at the backup directory.

Please find a solution using Windows PowerShell and Windows task Scheduler

Comments

  1. I read this article, it is really informative one. Your way of writing and making things clear is very impressive. physical therapy fairborn.Thanking you for such an informative article.

    ReplyDelete
  2. Wonderful post. your post is very well written and unique. Thank you for sharing this post here. pls visit our website best accident lawyers in phoenix az

    ReplyDelete

Post a Comment

Popular posts from this blog

Practice Exercises for PowerShell Beginner

  Practice Exercise1: File Operations/Searching/Basic PowerShell Operators 1.) Create a folder  TestingPurpose  and 3 Subfolders inside it SubFolder1, SubFolder2 2.) Create some test files inside these folders: TypeATest1.txt, TypeATest2.txt  … TypeATest50.txt into SubFolder1 TypeBTest51.txt, Purpose52Test2.txt … TypeBTest100 into SubFolder2 Needless to say that you have to use logic for creating these files. Not one by one 3.) Move all files which have an odd number in its name to SubFolder2 4.) Move all files which have even number in its name to SubFolder1 5.) Rename folder SubFolder1 to EvenFilesContainer and SubFolder2 to OddFilesContainer 5.) Prepare a list of all files currently existing inside folder  TestingPurpose      Example:  MasterFile.txt: As of YYYYMMDD HH: MM files inside Testing Purpose are: C:\testingPurpose\EvenFilesContainer\TypeBTest2.txt . . C:\testingPurpose\OddFilesContainer\TypeATest99.txt 6.) Delete all files which start with TypeA  

PSScriptAnalyzer to Analyze your Scripts

When you write a PowerShell script, you might constantly think about whether you are following best practices or no.  Do you know you have a tool that is very handy to use and can you in analyzing your PowerShell scripts? It is PSScriptAnalyze First of all you need to install the module.  Install-Module -Name PSScriptAnalyzer  If your Powershell version is less than 5.1( First run below command); Install-PackageProvider Nuget -MinimumVersion 2.8.5.201 –Force And make sure you are running PowerShell in admin mode and you are running PowerShell in admin mode. & That’s it.  Now your good friend PSScriptAnalyzer will help you in analyzing your scripts.   Import-Module PSScriptAnalyzer  Invoke-ScriptAnalyzer -Path .\test.ps1  How cool is that :)  Go ahead and analyze your script and let e know how many p For more information https://github.com/PowerShell/PSScriptAnalyzer