In: Computer Science
Using PowerShell ISE or VSCode, create a PowerShell script that will do the following:
You are going to create an HTML web page report from a standard processed report. You will use the text file “STUDENTS.TXT” located on Canvas.
The script must meet the following requirements:
What we need to accomplish?
Now we can apply the Henry Ford method to these basic tasks ('Every task is simple if split into sufficiently small steps'):
I have used PowerShell to create the script and to check it by executing the script.
#Input and Output
Files
$inputFile = Get-Content
c:\users\sharp\desktop\STUDENTS.txt
#getting input file
$output_file =
‘c:\users\sharp\desktop\ITS3410-praveen.htm’
#to get the output file
$FileLine = @()
Foreach ($Line in $inputFile) {
$MyObject = New-Object -TypeName PSObject
#adding the objects
Add-Member -InputObject $MyObject
-Type NoteProperty -Name Student# -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name LAST
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name FIRST
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name DN
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name ERN
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name HR
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name STS
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name DEG
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name CON
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name VER
-Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name GPA
-Value $Line
$FileLine += $MyObject
}
#to convert the script into
html
$FileLine | ConvertTo-Html -Property Student#, LAST, FIRST, DN,
ERN, HR, STS, DEG, CON, VER, GPA -body "<H2>Student
Report</H2>" | Out-File $output_file
Invoke-Expression $output_file
$line = "9999-9999 PRAVEEN KUMAR N 85 12 4 CER CSC 2007A 2.68"
$strings = $line -split " " | Where-Object { $_ }
# Bulk add properties
$props = @{
StudentID = $strings[0]
Surname = $strings[1]
Firstname = $strings[2]
DN = $strings[3]
ERN = $strings[4]
HR = $strings[5]
STS = $strings[6]
DEG = $strings[7]
CON = $strings[8]
} # Add more properties by name
$props["VER"] = $strings[9]
$props["GPA"] = $strings[10]
# Build Object
New-Object PSObject -Property $props
This method is useful to keep in mind as it is a highly flexible method you will be able to apply in many scenarios.
As for working with strings (which we'll need to do lots of times), we can try running this on the console:
"foo" | gm
This will list us all properties and methods of the string class. We can check out the method names - lots of useful utility there.
Regular Expression (regex) for the Student Number to be recognized:
$str = (Get-Content ... -Raw) -replace '\r'
$cb = {
$args[0].Groups[1].Value -replace '(?m)^.{7}' -replace '(?m).(.{3}).{5}$', '$1'
}
$re = [regex]'(?m)^(?<=-\n)((?:\d{4}\s\d{2}[^\n]*\d{5}(?:\n|$))+)'
$re.Replace($str, $cb)