• No results found

Requirements

1. Create a PowerShell script named Lab1.ps1 in your MyScripts directory.

2. The first five lines should be comments indicating the name of the file, your name, this course (CSNT xxx), the above lab number, and what you must type at the command line to run the program. For example, if Hello6.ps1 were the lab, the first five lines of that file would be

# file: Hello6.ps1 # author: Charlotte # course: CSNT xxx # lab: 1

# execution: .\Hello6.ps1 "Capt. Kirk" "Mr. Spock" "Dr. McCoy"

3. Although PowerShell recognizes most DOS and Unix commands, your script MUST use the appropriate PowerShell commands.

4. When you run the script, you will enter three command-line inputs. These three inputs will be the paths to three different directories on your hard drive, one of which must be the path to your MyScripts directory.

5. Store your name in a variable inside the script.

6. Display the contents of the above variable on the screen followed by the text "wrote this script."

7. You will then use a foreach loop to display a directory listing of each of the three directory names you passed through.

8. Initialize a counter, then increment the counter by one each time the foreach loop executes.

9. End your script with the End Script comment like we did in the examples.

10. After getting your foreach loop to work, add code to your script to perform the same function with these additional loop structures:

a. FOR b. WHILE c. DO WHILE d. DO UNTIL

PowerShell Lab #2

Requirements

1. Create a PowerShell script named Lab2.ps1 in your MyScripts directory.

2. The first five lines should be comments indicating the name of the file, your name, this course, the above lab number, and what you must type at the command line to run the program.

3. Create a subdirectory under your MyScripts directory if you don't already have one. Place copies of at least two .ps1 files in that subdirectory.

4. Write your script to find all .ps1 files in your MyScripts directory and all subdirectories which contain a string you will pass through at the command line.

5. Just in case you missed it direction in Step 4, the string you're searching for must be entered at the command line.

6. You will display the results of the search in four ways (the fourth way is explained in Step 8):

a. Sorted alphabetically by file name

b. Sorted in reverse alphabetical order (HINT: get-help is your friend) c. Grouped by the filename property

7. Test your script by searching for the string write-output.

8. Now change your script so that your search is case-sensitive (HINT: get-help is still your friend).

9. Test your script by running it once, searching for the string write-output, and again searching for the string Write-Output.

PowerShell Lab #3

Requirements

1. Create a PowerShell script named Lab3.ps1 in your MyScripts directory.

2. The first five lines should be comments indicating the name of the file, your name, this course, the above lab number, and what you must type at the command line to run the program.

3. Enter two numbers $n1 and $n2: one at the command line and one through a Read-Host

command (make sure you prompt the user for input).

4. Multiply the two numbers and assign them to a variable named $product.

5. Square each number by multiplying it by itself. The results should be stored in the variables $n1Sq and $n2Sq.

6. Add the two squares and assign to a variable named $sumSq.

7. Now subtract the variable $product from the variable $sumSq and store in a variable named $result.

8. Check to see if the values stored in $result and $product are equal.

a. If they are equal, output the text "You entered the same value for both numbers." and display one of the values.

b. Otherwise, just display what's stored in both $n1 and $n2. 9. Finally, you will output three lines of text:

a. The value of $product b. The value of $sumSq c. The value of $result

10. Test your script by entering two different numbers, then by entering the same value for both numbers.

PowerShell Lab #4

Requirements

1. Create a PowerShell script named Lab4.ps1 in your MyScripts directory.

2. The first five lines should be comments indicating the name of the file, your name, this course, the above lab number, and what you must type at the command line to run the program.

3. When you run this script you will need to enter at least two values at the command line 4. Create an empty array called $userArray.

a. Using a foreach loop, populate $userArray with the command line arguments you entered.

b. Display the number of elements in the filled array using the length property. c. Display the values in the array.

5. Create an array called $myArray with at least two elements in it (define these values at the time of creation).

a. Display the number of elements in the array using the length property. b. Display the values in the array.

6. Combine the two arrays into an array called $comboArray.

a. Display the number of elements in the array using the length property. b. Display the values in the array.

7. Initialize a counter variable.

8. Using a while loop, which increments the counter, display each value of each element of $comboArray using the value of the counter as the index (HINT: The length property will be very helpful here).

Related documents