PowerShell tasks
git repository for tasks
Make a private repository called TTC2060
on gitlab@labranet for the exercises. You have to give Developer rights
to your project for the lecturers Juho Pekki and Esa Salmikangas . For PowerShell tasks create folders for the tasks named like this:
PowerShell
- PowerShell-1
- PowerShell-2
- PowerShell-3
- PowerShell-4
- PowerShell-5
Take a screenshot from every succesful task run, and add the screenshots to the same folder where the tasks exist. Push your the scripts and screenshots of the tasks to the labranet according to when they are ready. Tasks must be returned according to the given schedule. After that, visit Moodle to return the link to the repository's tasks folder according to the given schedule.
PowerShell-1
Task01
Create a script that writes current logged username and current date to console.
Task02
Ask from a user his/her firstname, lastname and groupcode. Save them to variables. After that show the following info in console.
Task03
Ask two integers from a user, and return addition, subtraction, multiplication and division of numbers.
Task04
Create an array named $names
, that includes seven (7) names. You can select the names, they can be your friends or comic characters. Create a script that show how many items is in the array, show the items and also the first and last name of the array.
Task05
PowerShell includes many kind of commands: alias, cmdlets, function and application. Create a script that counts how many application PowerShell returns.
PowerShell-2
Task01
Create a script that counts how many files are in user's home folder. The script shows the home folder name and the number of the files.
Task02
Create a script that counts how many files and folders are in given folder. The script shows the name of the folder and the number of the files.
Task03
Create a script that takes three parameters, the type of the parameters is string. The script order the given strings by string length. Test your script at least with four different test runs, like in the picture.
Task04
Create a script that takes two named parameters. The first parameter is the name of a user, second how many times the script greets the user. Give default values for the parameters, so if the script is run without arguments, it will say Hello unknown
only once.
Task05
Create a script that simulates pinging a remote server. It takes two named parameters. The first parameter is the IP-address of a server, second how many times the ping is tried. The script also ask the permission to make ping with. Pings are made only if a user says Yes. Give default values for the parameters, so if the script will be run without the arguments, it will try to ping 192.168.1.1
only once. Because ping
is blocked in the labranet, we simulate only pinging.
PowerShell-3
Hint Please, familiarize yourself carefully with FileSystem material before you start to do tasks.
Task01
Create a script that takes one parameter: foldername
. Check if the given folder exist. If the folder exist: show how many files are in the folder and then list the files of the folder. If the folder does not exist, the script will show a message: "Sorry, {foldername} does not exist.".
Task02
Create a script that takes two parameters: foldername
and filename
. Check first if the given folder exist. If the folder exists, check if the file exists in the folder. If true, show a proper message. If it does not exist, show a message: "Sorry, {filename} in the {foldername} does not exist.".
Task03
Create a script that takes two parameters: foldername
and filename
. Check first if the given file is in the given folder. If the file exists, show a proper message. If it does not exist, ask from a user "Create the file {filename}? Yes/No". If the user returns "Yes", create the file to the folder.
Task04
Create a script that takes three parameters: foldername
, filename
and number
. Check first if the given folder, exists. If not, create the folder. After that create as many new files as the number argument says to the folder. Finally return a report how many files has been created, and list the names of the files.
Task05
Create a script that takes two parameters: foldername
and newname
. Check first if the given folder, exists. If not, show a proper message and quit the script. If the folder exist, count files in the folder, and ask from a user: "All {number} files will be renamed with name {newname}. Yes/No". If the user returns "Yes", rename all files in the folder.
PowerShell-4
Task01
Create a function named Get-FiluInfo
that shows Name and Length properties of all files in user's home folder. Run the function to prove that it works.
Task02
Create a function named Get-FiluInfo2
that shows Name, Length, LastWriteTime properties of files with given extension in user's home folder. The extension is like .txt or .csv, and it has been given as a parameter. Run the function to prove that it works.
Task03
Create a script that ask workstation names from an user. The names are asked until the user gives empty input. After that, the script writes all names to a file in user's home folder. The file name is given with the parameter: filename
. Finally show a proper message that the file was created succesfully.
Please, read about manipulating arrays
Task04
Create a script that ask file names from an user. The names are asked until the user gives empty input. After that, the script creates the files with given names to user's home folder. The script writes the current date and time to the files in the following format 30.9.2021 12.30
. Finally show a message that how many files were created succesfully.
Task05
Create a script that takes one parameter: foldername
. First check that the given folder exist, then write all names of files in the folder to a text file named files.txt. Create the file to user's home folder. If the folder does not exist, the script will show a message: "Sorry, {foldername} does not exist."
PowerShell-5
In PS-5 tasks you have to return .ps1
-files and screenshots
of all tasks to the gitlab repo. Thank you.
Task01
Create a script that creates new accounts for a placebo system (placebo means here that the system not really exists). The script takes one parameter filename
. The parameter points to a csv file including user names. Check first that the given file exist, then read all lines. User's account is created by taking first four characters of lastname and two characters of firstname. The account contains only lowercase letters. Give a message presented in the picture below. If the given file does not exist, the script will show an error message.
Task02
Create a script that opens all the given urls to a browser. The script has one parameter filename
. The parameter points to a text file where urls are. Check first that the given file exist, then read all lines and open the urls in a browser. If the given file does not exist, the script will show a error message.
Task03
Create a script that creates new localusers by a given csv-file. The script has one parameter filename
. Check first that the given file exist, then read all lines and create a new local user account for every user in file. The account name is created same way as in the task 01. You can use in this script the parameter NoPassword
Please remember: you have to run this script with Administrator privileges that you can use the New-LocalUser cmdlet. If the given file does not exist, the script gives a proper error message.
Please note: and you can delete creted local users after you have tested that your script is working. Please remember take a screenshot from your test run.
Task04
Create a script that writes information of all the local users in a computer to a CSV-file. The script has one parameter filename
. The information includes local users's 1) name 2) full name 3) SID and 4) lastlogon.
Task05
In this task you have to create a new function called Add-MyEvent
. The function takes two parameter: Type
and Message
. The type can be: Error, Warning, or Information. Message is a clear message. Add the following functionality to the task 04 using your new function.
- if the script can create a new CSV-file succesfully, then it will add a Information message to the eventlog.
- if the script cannot create a file succesfully, then it adds a Error message to the eventlog.
Before using your new function create a new source named "MyPowerShell" to the Evenlog for writing your events.