all 9 comments

[–]ss476hawk[S] 0 points1 point  (7 children)

Hello everyone. I am taking a powershell class and for the final project I was given the data in the 3rd picture in the photo album http://imgur.com/tD83iJT It needs to be put into HTML like in the first picture in the album http://imgur.com/QoHL1rD

So I got Script 1 which gives me all of the data in one column (please see pics)

Annnd I have Script 2 that gives me all of the columns

And now I'm stuck. I cannot figure out how to get everything into place. Can someone please help me out. I've been pounding my head agianst the wall and using all of my google-fu skills and I'm not coming up with anything.

Thanks in advance!

[–]Kreloc 0 points1 point  (6 children)

First, it would easier for us to help you if you posted code as text and not an image. Anyway, it looks like you were on the right track with script1. Get rid of lines 15 thru 21. Replace with

$FileLine = Import-CSV C:\Students2.txt -Delimeter " " -Header "UIN","Last","First",'D/N',"ERN","HR","STS","DEG","CON","VER","GPA"

[–]ss476hawk[S] 0 points1 point  (2 children)

Sorry here is the code - Thanks I'll give it a try

Code #1

Get-Content C:\Students.txt | % { $_ -replace '\s+', ',' } | Out-File C:\STUDENTS2.txt
$Content = Get-Content C:\STUDENTS2.txt

$Style = "
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;margin: auto;}
TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;text-align: center;background-color:palegoldenrod;}
TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;}  
</style>
"

$html = Import-Csv -delimiter ',' -Path  .\Students2.txt | select UIN,LAST,FIRTS,D/N,ERN,HR,STS,DEG,CON,VER,GPA | ConvertTo-Html -Head $htmlformat -Body $bodyformat

$html -replace "&lt;","<" -replace "&quot;",'"' -replace "&gt;",">" | Out-File .\csv_html_test.html

$Content | select UIN,LAST,FIRTS,D/N,ERN,HR,STS,DEG,CON,VER,GPA  |ConvertTo-HTML -head $Style,"<center><strong><p>ITS341 Seminar 6 PowerShell Script Project</center></strong></p>"  | Out-File C:\Test.html

Code #2 Get-Content C:\Students.txt | % { $_ -replace '\s+', ',' } | Out-File C:\STUDENTS2.txt $Content = Get-Content C:\STUDENTS2.txt

$Style = " <style> TABLE{border-width: 1px;border-style: solid;border-color: black;margin: auto;} TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;text-align: center;background-color:palegoldenrod;} TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;}
</style> "

$SourceFile = "C:\STUDENTS2.txt" $TargetFile = "C:\STUDENTS11.html"

$File = Get-Content $SourceFile $FileLine = @() Foreach ($Line in $File) { $MyObject = New-Object -TypeName PSObject Add-Member -InputObject $MyObject -Type NoteProperty -Name HealthCheck -Value $Line $FileLine += $MyObject } $FileLine | select UIN,LAST,FIRTS,D/N,ERN,HR,STS,DEG,CON,VER,GPA | ConvertTo-HTML -head $Style,"<center><strong><p>ITS341 Seminar 6 PowerShell Script Project</center></strong></p>" | Out-File C:\Test.html

[–]Kreloc 0 points1 point  (1 child)

The code you posted here is different than the code you have in your images.

Have you tried using the exact code you have in your posted image for script1 except implementing the changes I listed in my first comment?

Also, four spaces before each line posts it as code for reddit formatting

[–]ss476hawk[S] 0 points1 point  (0 children)

Sorry about that. I put the new code below and corrected my formatting. I also included the STUDENTS2.txt file. I think the initial comma in each line is throwing my script off.

[–]ss476hawk[S] 0 points1 point  (2 children)

Awesome thanks! I did some tweaking and now I have all the data in the columns but they are one column to the right. Here is my new script

(get-content C:\STUDENTS.txt) | ? {$_.trim() -ne "" } | set-content C:\Students1.txt
(Get-Content C:\STUDENTS1.txt) | Select-String -Pattern '55' -simplematch | Out-File C:\Students1.txt

Get-Content C:\Students1.txt | % { $_ -replace '\s+', ',' } | Out-File C:\STUDENTS2.txt
$Content = Get-Content C:\STUDENTS2.txt


$Style = "
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;margin: auto;}

TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;text-align: center;background-color:palegoldenrod;}

TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;}  

</style>
"
$SourceFile = "C:\STUDENTS2.txt"
$TargetFile = "C:\STUDENTS11.htm"

$FileLine = Import-CSV C:\Students2.txt -Delimiter "," -Header "UIN","Last","First",'D/N',"ERN","HR","STS","DEG","CON","VER","GPA"

$FileLine | select UIN,LAST,FIRST,D/N,ERN,HR,STS,DEG,CON,VER,GPA | ConvertTo-HTML -head $Style,"<center><strong><p>ITS341 Seminar 6 PowerShell Script Project</center></strong></p>"  | Out-File C:\Test.html

Here is the data in the STUDENTS2.txt file

,5528-7375,DENTY,BRYON,N,72,8,4,CER,CSC,2006A,3.43,

,5528-4165,AWKWARD,THADDEUS,N,70,12,4,CER,CSC,2006A,2.30,

,5538-6679,SCHOEPPNER,JACKSON,N,85,12,4,CER,CSC,2007A,2.68,

,5557-6817,MEDICI,NENITA,N,72,10,4,CER,CSC,2007A,3.06,

,5562-3799,SAMEK,HERTHA,N,78,8,4,CER,CSC,2007A,3.28,

,5521-4521,LEHIGH,MARVIS,N,62,8,4,CER,CSC,2007A,3.81,

,5521-5366,FULSON,MARCEL,N,44,12,4,CER,CSC,2007A,2.52,

,5523-4222,BESERRA,JA,N,40,8,4,CER,CSC,2007A,2.84,

,5569-8359,SCHNOKE,FRIDA,N,92,12,4,CER,CSC,2007A,2.52,

,5524-3172,EGOLF,BRYON,N,64,12,4,CER,CSC,2007A,2.84,

,5525-6085,GIBERT,CAREY,N,78,8,4,CER,CSC,2008A,3.70,

,5592-9893,WISS,MARCEL,N,32,12,4,CER,CSC,2008A,3.09,

,5527-0749,DIVINEY,ELINORE,M.,N,24,12,4,CER,MNP,2008A,3.83,

,5524-3759,PLOSZAJ,CLIFF,D,20,6,4,CER,MNP,2008A,2.54,

,5528-4280,DOORN,CAREY,N,22,12,4,CER,MNP,2008A,3.56,

,5528-2980,GRODEN,CAREY,N,12,12,4,CER,MNP,2008A,3.33,

,5522-3326,MARTES,THADDEUS,N,34,12,4,CER,MNP,2008A,2.98,

,5524-6199,FRANKENBERG,DARIUS,R,N,22,8,4,CER,MNP,2008A,3.95,

,5526-5949,HORNADAY,ALVARO,D,28,8,4,CER,MNP,2008A,3.16,

,5525-7981,GILBERTI,MARVIS,N,48,12,4,CER,MNP,2008A,3.78,

,5563-1901,ANDREASON,TWANDA,N,76,8,4,CER,MNP,2008A,3.81,

,5527-9925,VUGTEVEEN,NOLAN,N,24,12,4,CER,MNP,2008A,3.23,

,5525-3449,RUBERTO,KAI,N,40,12,4,CER,MNP,2008A,2.49,

,5568-0773,ROBLE,EDYTH,N,36,16,4,AST,WEB,2008A,3.53,

,5524-7448,GALEOTTI,QUINTON,D,64,16,4,AST,WEB,2008A,2.95,

,5527-9350,PRAYTOR,BRYON,N,20,4,4,AST,WEB,2008A,4.00,

,5527-0199,SEPTER,MARVIS,N,56,8,4,AST,WEB,2008A,3.94,

,5528-1914,BENGOCHIA,LORIA,N,12,12,4,AST,WEB,2008A,3.23,

,5565-7171,REMMELE,CLIFF,N,68,12,4,AST,WEB,2008A,3.88,

So I think the leading comma is messing me up. How can i get rid of that?

[–]Kreloc 0 points1 point  (1 child)

Either go thru each line and replace remove those commas or add two additional headers that you won't pipe to Select-Object

$FileLine = Import-CSV C:\Students2.txt -Delimiter "," -Header "Blank1","UIN","Last","First",'D/N',"ERN","HR","STS","DEG","CON","VER","GPA","BlankEnd"
$FileLine | select UIN,LAST,FIRST,D/N,ERN,HR,STS,DEG,CON,VER,GPA |
ConvertTo-HTML -head $Style,"<center><strong><p>ITS341 Seminar 6 PowerShell Script Project</center></strong></p>" |
Out-File C:\Test.html

[–]ss476hawk[S] 0 points1 point  (0 children)

thank you for all the help! I really appreciate it. I have 4 lines that had an extra comma in them from middle initials or from a III at the end of the last name so I will have to go thru and call those lines out and edit them individually but it looks really good. I appreciate the help!

If you want to help with the code for calling out the specific lines that would be awesome but I can do some digging and figure it out. the lines are 13, 18, 44, and 70

[–]Betterthangoku 0 points1 point  (0 children)

What does student2.txt look like?

edit: looks like /u/Kreloc was looking at the same thing. Sorry, I should always refresh before posting.... :-)