N01361264 Deo Madrid CENG214 Assignment 4 Part 1: Demo 1 $animals = "aardvaark;bear;cat;dog,elephant,fox,goose;hare;ibex;jaguar;kangaroo" -split ";" $animals -split ";" | sort-object -desc $animals When declaring the variable $animals and separating the data with commas, instead of semicolons; However, when correcting the error by replacing the commas with semicolon $animals = "aardvaark;bear;cat;dog;elephant;fox;goose;hare;ibex;jaguar;kangaroo" -split ";" d Gives you this instead kangaroo jaguar ibex hare goose fox elephant dog cat bear aardvaark aardvaark bear cat dog elephant fox goose hare ibex jaguar kangaroo Selecting multiple values at the the same time changes the order of how the data appears #selecting multiple values at the same time $animals[1,3,2] $animals[3..7] $animals[8..2] N01361264 Deo Madrid $animals[@(1,5,3)] bear dog cat dog elephant fox goose hare ibex hare goose fox elephant dog cat bear fox dog Mixing the ranges with single values in an array allows you to assign multiple values to the variable $animals[@(1,5,3)] $animals[@(7;(1..5);0)] #mixing ranges with single values - use @ array notation $a1,$a2,$a3=$animals[1,7,2] #multiple assignment "This is a1: $a1" "This is a2: $a2" "This is a3: $a3" bear fox dog hare bear cat dog elephant fox aardvaark This is a1: bear This is a2: hare This is a3: cat Demo 2 Building an array of hash tables $student1 = @{name = ”Jean”; GPA = 2.85; Semester = 3 }; $student2 = @{name = ”La Fontaine”; GPA = 3.1; Semester = 2 }; Then copying the data into an array N01361264 Deo Madrid $array = $student1, $student2, $student1.clone(), $student2.clone() And setting the key values $student1.GPA = 4 $student2.Semester = 4 $student1.Keys Loop though the list and key values foreach ($myKey in $student1.keys) { "key: $mykey" "value: " + $student1[$myKey] "" } out-Host -input "\nList of values" gives you key: GPA value: 4 key: name value: Jean key: Semester value: 3 selecting multiple keys and joining them together while also replacing certain strings foreach($value in $student1.values) { $value} $student1['gpa','name'] #same multiple selection trick as with arrays $string1 = $student1.keys -join "***" $string1.replace("***", "!?") 4 Jean 3 4 Jean GPA!?name!?Semester Demo 3 cd 'C:\users\public\Documents' #make sure that your directory is reasonable out-file -file hello.c -force -input 'fprintf(stdout,"Hello world\n");' #replace -force w -append start-process -file NotePad -ArgumentList hello.c Created the file hello.c in the specified directory hello.c fprintf(stdout,"Hello world\n"); After changing -force to -append it opens notepad with the appended text N01361264 Deo Madrid #Start up a program but wait for it to finish remove-item hello.class #make sure file is not there clear get-childItem hello.* $process=start-process -file scp -passthru -ArgumentList "n01361264@apollo.humber.ca:public_html/CENG214/hello.*","." wait-process $process.id start-process -file java -argumentList hello -NoNewWindow start-process -file java -argumentList hello -NoNewWindow Opens ssh terminal to apollo humber with my user id, after signing in with password program immediately closes Demo 4 Generates lottery tickets with random numbers for the date October 12 #Generate lottery picks for the next 10 weeks set-location 'C:\Users\Public\Documents' #Make sure this is in the right dir new-item -path lottery.csv -type file -forc #replace file if it exists out-file -filePath lottery.csv -input "" #define excel's delimiter out-file -filePath lottery.csv -input "sep=;" #This has to be in the 3nd line of the file $startDate = get-date -day 12 -month 10 for ($i = 0; $i -lt 10; $i++) { $ticket = 1..50 | get-random -count 7 $ticket = $ticket -join ";" $outputLine = ($startDate.addDays(7 * $i).toString() + ";" + $ticket) out-file "lottery.csv" -input $outputLine -append } out-file "lottery.csv" -input "" -append #blank line out-file "lottery.csv" -input " ;=sum(b1..b10);=average(c1..c10);=count(d1..d10)" -append start-Process -file Excel -ArgumentList lottery.csv Generated Excel Document lottery.csv #Should bring up Excel. N01361264 Deo Madrid Demo 5 #this loop is basically the wc command written in PowerShell #There's a bug with empty lines - why... and how do we fix it? $lineNo=0 foreach($line in (get-content lyrics.txt)) { $chars = $line.length $words = (-split $line).count $lineNo++ $output=[String]$lineNo + " " + $line + " $chars/$words" $output } Prints the lyrics to the song American Pie from lyrics.txt however, when there’s an empty line it shows it as 0/1 Added the if statement if the line length equals 0, reset word count back to 0 N01361264 Deo Madrid $lineNo = 0 foreach ($line in (get-content lyrics.txt)) { $chars = $line.length $words = (-split $line).count $lineNo++ if($line.length -eq 0) {$words=0} $output = [String]$lineNo + " " + $line + " $chars/$words" $output } Reads the file lottery.csv, if line length equals 0 puts in blank lines, if the line contains an equal symbol, skip the line and continue, split each value with a semicolon; discards the date. It then sorts the numbers alphabetically and prints them out from the array. #Reading the file lottery.csv foreach($line in (get-content lottery.csv)) { if($line.length -eq -0) {break;} if($values[0] -like "=*") { continue } $values= $line -split ";" $values= $values[1..7] | sort-object #$values= $values[1..7] | sort-object {[int]$_} #Advanced: sort the values as numbers $line $values } Output: N01361264 Deo Madrid Demo 6 Creates a new user called Aladin where his password is Open Sesame. His account will expire in the next two months, he is also part of the group Rocketeers, had to run as Administrator to work. $pass = ConvertTo-SecureString -string "Open Sesame" -AsPlainText new-localUser -name "Aladin" -password $pass -AccountExpires (getdate).addMonths(2).toString() add-localGroupMember -Group 'Rocketeers' -member 'Aladin' Get-LocalGroupMember -group 'Rocketeers' get-localUser | select-object -prop name,passwordExpires,enabled,lastLogon #clean up Remove-LocalGroupMember -group 'Rocketeers' -member 'Aladin' remove-localUser -name 'Aladin' N01361264 Deo Madrid Part 2: 1. $1stInput, $2ndInput Write-Host ("Please enter an integer between 3 and 9: ") $1stInput = Read-Host Write-Host ("Please enter an integer between 100 and 250: ") $2ndInput = Read-Host if($2ndInput -gt $1stInput) { Write-Host "$1stInput is Greater" Write-Host "$2ndInput is Smaller" } else { Write-Host "$2ndInput is Greater" Write-Host "$1stInput is Smaller" } 2. # Display date and Username "$env:USERNAME $((Get-Date).ToString())" # Starting number of bottles $NumOfBottles = 99 $Bottles = "bottles" # Declaring data to be written into file in a string for later $BottleToFile = "" do { # Print in Terminal Write-Host "$NumOfBottles $Bottles of beer on the wall, $NumOfBottles $Bottles of beer." # Print to Word Doc $BottleToFile += "$NumOfBottles $Bottles of beer on the wall, $NumOfBottles $Bottles of beer.`n" # When there is only 1 bottle left N01361264 Deo Madrid if ($NumOfBottles -eq 2) { $Bottles = "bottle" } else { $Bottles = "bottles" } $BottleToFile += "Take one down and pass it around, $($NumOfBottles - 1) $Bottles of beer on the wall.`n`n" Write-Host "Take one down and pass it around, $($NumOfBottles - 1) $Bottles of beer on the wall.`n" # Decrement number of bottles $NumOfBottles -# When there are no more bottles, prints the last line and exits if ($NumOfBottles -eq 0) { Write-Host "No more bottles of beer on the wall, no more bottles of beer." Write-Host "Go to the store and buy some more, 99 bottles of beer on the wall.`n" $BottleToFile += "No more bottles of beer on the wall, no more bottles of beer.`n" $BottleToFile += "Go to the store and buy some more, 99 bottles of beer on the wall.`n" } } while ($NumOfBottles -gt 0) # Create Word Object $Word = New-Object -ComObject Word.Application # Will not open the Word doc $Word.Visible = $False # Creates the word document $Document = $Word.Documents.Add() # The pointer used getting the strings and typing into the word file $Selection = $Word.Selection # Types the data passed $Selection.TypeText($BottleToFile) # Specify the save Location $SaveLoc = "$pwd\99bottles.docx" # Save the Document $Document.SaveAs([ref]$SaveLoc, [ref]$SaveFormat::wdFormatDocument) # Opens the word document $Word.Visible = $True # Closes Word $Word.Quit() N01361264 99bottles.docx Deo Madrid