Normal 0 false false false false EN-US X-NONE X-NONE
Hello, there. I have no idea where to start with this assignment (Exercise 2; Step 1 - 9).
I hope somebody please helped me. (I know it is not a good way to learn Powershell to
ask this kind of question. But, I cannot help myself. I feel so embassrassed.
And I would like to understand how it works.
Thank you. Good evening.
----------------------------------------------------------------------------------------------------------
Exercise 2: Creating the Win213 Repository – Using Conditional Control Structure
Commands | Purpose | ||
| Multiline comment | ||
| Clears the console | ||
| Tests of a condition | ||
| Gets the current working directory | ||
| Sets the current working directory | ||
Pause | Suspends a script. Prompts user “Press Enter to continue” | ||
Test-Path | Tests if a path is true of false | ||
Write-Host | Displays message on console |
The commads used in this script are listed above.In this exercise, we will use a conditional statement to check whether the user is in the documents folder, and if not, give him/her the chance to exit the script or continue.
Wk6
Wk5
Wk4
Wk3
Wk2
Copy the script template to a new tab. Name the file LearnName_Lab5_Win213Rep.ps1Wk1
- Modify the Syntax to reflect that the user must be in the documents folder to run the script
- ##Create variables to store the documents, Win213,and Scripts folder
path
#############################################################################- Name the variables DocumentsPath, Win213Path, and ScriptsPath
- ##Create variables to hold the collection of folder names for Win213
folder and Scripts folder
##############################################################################- Separate each folder name with a comma (double quotes around each name)
- ##Test if user in Documents folder, if not display message giving the
option to exit or ##continue,if continue change to documents folder and
make Win213 folder
##############################################################################- This is a common scripting technique you need to learn.
i. Create a variable to store the current prompt location
ii. Use Test-Path cmdlet to see if the current location is the same as the document path variable you created in step 1. If it is, then make the directory, if not display this message “"This script only runs in the documents directory. Change to the documents directory and continue? Press CTRL + C to exit or "
iii. Add the pause command
iv. Change to the documents folder and make the Win213 folder.
|
Commands | Purpose | ||
| Cycles through a collection performing the commands in the script block on each item in the collection | ||
| Creates a new file or directory | ||
| Displays a directory listing of the current folder | ||
Optional Step 8 | |||
Where-Object{…} | Sorts objects based on criteria | ||
$_ | Pipeline variable holds an item in the pipeline | ||
Move-Item | Moves and renames files or directories | ||
In this exercise you will create a foreach loop to cycle through the folder names collection, replacing the New-Item name parameter with the folder name on each loop. This is an excellent technique to use to change the New-Item command, only the collection names in the variable at the beginning of the script need be changed, the rest of the script stays the same.
- ## Cycle through the Win213 folder names using a foreach loop
############################################################################# - ##Cycle through the Scripts folder using a foreach loop
##############################################################################
See Optional 8 and 9 below and decide which option you want to do. - ##Do a recursive search of the documents folder to find all files PS1
and CMD files
#############################################################################- Save the output to a variable called Files.
- Now manually, copy and paste the files based on LastWriteTime to the correct week subfolder. Scripts created in week 2 go to Wk2 folder, etc.
· If LastWriteTime is between Jan 6 and Jan 12 move to Wk1
· If LastWriteTime is between Jan 13 and Jan 19 move to Wk2
· If LastWriteTime is between Jan 20 and Jan 26 move to Wk3
· If LastWriteTime is between Jan 27 and Feb 2 move to Wk4
· If LastWriteTime is greater than Feb 2 move to Wk5