25 pts - Operating Systems Q&A VII
Visit this course in D2L and complete Quiz07 by the due date / time for this assignment.
(25 pts) Text file handling with PowerShell
Ref: (Video) Develop an 'intermediate' PowerShell script in Windows
Do this development in your xxxxWS1. You will develop a 'real' PowerShell script needed for work by a former student who asked ...
I'm trying to write a PowerShell script that searches through a folder containing various text documents and I want to search for a specific string in lines of the text files. For example, if any of the .txt files in a folder I'm searching through has a line with the string "hotdog" I want the script to print the complete line from text document line containing the specified string.
Begin your development in your Windows Server by creating a folder named (suggested) SampleTextFiles within your Documents folder. Add the following text files into this folder (but the last line of File1.txt should contain your JagID, not mine, so change tschultz to your login).
File1.txt
First line of text
A real hotdog lover may not like mustard.
The last of the hot dogs.
hotdog:tschultz
File2.txt
One superhotdog here
Two hotdog hotdogs here
Not a hot dog here
File3.txt
Nothing to see here
I mean it, move along
File4.txt
{empty}
From that setup your final script should produce the following expected output when searching the SampleTextFiles folder for files containing hotdog
Searching directory C:\Users\tschultz\documents\SampleTextFiles for hotdog
File1.txt line 2 character 7: A real hotdog lover may not like mustard.
File1.txt line 4 character 0: hotdog:tschultz
File2.txt line 1 character 9: One superhotdog here
File2.txt line 2 character 4: Two hotdog hotdogs here
In your Documents folder "above" the text files folder start on a PowerShell script named ExtractString.ps1 with the following content
$SelLines = Get-ChildItem -Path "SampleTextFiles" | Select-String "hotdog"; foreach ($SelLine in $SelLines) { Write-Output $SelLine; }
Running the script provided above should produce something like the following demo output
PS C:\Users\tschultz\Documents> .\ExtractString.ps1 C:\Users\tschultz\documents\SampleTextFiles\File1.txt:2:He is a real hotdog man but does not like mustard. C:\Users\tschultz\documents\SampleTextFiles\File1.txt:4:hotdog:tschultz C:\Users\tschultz\documents\SampleTextFiles\File2.txt:1:One superhotdog here C:\Users\tschultz\documents\SampleTextFiles\File2.txt:2:Two hotdog hotdogs here
which isn't a complete solution. It is a starting point for your development requirements outlined below and note that each $SelLine variable includes the full path, file name, line number, and line from the file where the text being searched for is found at least once.. .
Update the ExtractString.ps1 script to include
Use of parameters to make the script general purpose. Use (suggested) $SearchFolder for the path to the folder with the files to be searched as an optional parameter defaulting the the current working directory and (suggested) $SearchText as a required parameter with the text being searched for instead of 'hard-coded' SampleTextFiles and hotdog as in the example. Executing the script (from the PowerShell console with your Documents as working directory)
.\ExtractString.ps1 -SearchPath SampleTextFiles -SearchText hotdog
should produce the expected output above but executing
.\ExtractString.ps1 -SearchPath ExtractTest -SearchText xyzpdq
would generate results for finding xyzpdq in files from the ExtractTest folder.
When your script is completed, download ExtractTest.zip into your Documents folder and 'extract all' to create and populate a folder named ExtractTest. From the PowerShell command line (in your Documents as working directory) issue the two commands listed in item 3 above saving a screen cap of the commands and results in WSResults.jpg (or other image format). Copy ExtractString.ps1 to ExtractString.txt so it will upload, then attach a copy of WSResults.jpg and ExtractString.txt to this assignment in D2L.
Ref: (Video) Deploy an 'intermediate' PowerShell script in Linux
In your Linux VM duplicate the context of the previous task; that is, within your Documents folder create the folder SampleTextFiles with the four files described and download / unzip ExtractTest.zip into an ExtractTest folder. Install PowerShell in your Linux VM and deploy your ExtractString.ps1 into your (Linux) Documents folder.
From the PowerShell command line (in your Documents as working directory) issue the two commands listed in item 3 from the tas above saving a screen cap of the commands and results in LSResults.jpg (or other image format). Attach a copy of LSResults.jpg to this assignment in D2L.
25 pts - Masters Freebie Enjoy the break; twenty-five points posted to your grades.