CENG214: Intermediate Filters, Functions and PSCustomObjects

[Pages:2]Out: Nov 16, 2021 Groups C & A, Nov 19 Group B Due: Nov 23, 2021 Groups C & A, Nov 26 Group B

Marked out 30.

CENG214: Intermediate Filters, Functions and PSCustomObjects

1. Writing foreach-object filters

For each exercise manipulate information from the suggested data provider (a-d are worth 1 mark each; e-g are worth 2 each. 12 marks total)

a. Given a list of names in an array, use a % filter to create text file for each name, ie: dog,cat,mouse | % { your filter} creates the files dog.txt, cat.txt and mouse.txt.

b. Given a list of numbers, use a % filter to output a hashtable for each number consisting of 3 fields, the number, the square root of the number and the number squared.

c. Given an array consisting of different data types (strings, doubles, dates...), use a % filter that passes on the datatype name of each item. Test with 5 different data types. ie: 12,"Hello",(get-date) | % { your filter } should generate: Int32,String,DateTime

d. From Assignment #5 you should have a set of .txt and .data files. Use a % filter to change the lastAccessDate of all of them to some time tomorrow and verify the result.

e. For every entry returned by get-childItem, calculate the difference between the creation time and the last access time and express this as the total number of minutes. The output of the filter should be the file name and this difference.

Hint: Create a [timespan] and use get-member to look at the available fields.

f. There's an array function [array]::reverse($anyString) that some students discovered when you were asked to write a loop in the midterm to display an array backwards. It was a wrong answer then because of the loop requirement, but you are permitted to use it now. Create a PowerShell pipeline that behaves like the Unix rev command, reverses each line of a file or each element array of strings.

g. Create a pipeline that expands a string consisting of vowels, consonants, digits and whitespace to an array using -split, then uses multiple tests in a % filter to replace each category of letter with a text representing the category. For example `M3B' might become `Const','Digit','Const'. At the end of the pipeline use group-object to count the number of items in each category.

h. Demonstrate that you can replace any select-object command with a % filter. Show this by providing any pipeline with a select-object statement that references 3 fields, then showing a pipeline using % to do the same thing.

2. Writing Filter functions to replace foreach-object (2 marks each)

a. Rewrite any 2 of the % filter expressions in Q1 as a Filter function. (You may also choose to do this for any example presented in class that was not converted to a filter function. Show that they work.)

b. Write a filter function that adds a new NoteProperty field with a randomly generated value to any list of [pscustomobject]. The fields can be of any data type other than date or timespan. I prefer that you use the type of object that you generated in Assignment #6. Show that this works and once set the value does not change.

c. Write a filter function that adds a new ScriptProperty field to every [pscustomobject] in a list that calculates a random date in the current year. Prove that this works by selecting the same 3 items twice.

3. Special features of functions (4 each/12 marks)

a. Write a Function with mandatory parameters for userName, password and fullname. Have optional fields for description and group but the default value for group is "Users" i. If the group dos not exist, create it ii. Turn the password into a secure string. iii. Create the user and add them to their group.

b. Write a Function that creates a hash table with 4 fields that are passed to it as arguments. Only the name field required, the others should have default values. The fields are the employee name, position, hourly salary and hoursPerWeek. Use ValidateSet to limit the number of available positions. Use ValidateRange to limit the number hours per week to between 4 and 48 and the salary should be between $15 and $275 per hour. i. Test the function and show that it works ii. What happens when the default value is out of the valid set of values?

c. Review this week's example function demo-switch. Write a filter function that the following switches: -thousands -mega and -giga with alias --KB, -MB and -GB. and a parameter -filePath. i. If -thousands, -mega or -giga is present, output nn KB, nn MB or nn GB depending on which switch is present, by dividing the original # by 1024, 10242 or 10243. If the -filePath operator is present, send the output to the named file rather than the screen. ii. Show testing to verify that this works.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download