0

I am looking to setup a powershell script that will append a new line of data to an HTML table on an output file until it is terminated by the user. This particular example is being written to write the current hard disk used space along with a time stamp every two seconds to an HTML file.

I tried using a CSV file for the output, but the script can't write to the CSV file and have it open at the same time. This is important.

$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: gray; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #E1DEDC;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: gray;}
</style>
"@

$DateTime = Get-Date -Format G
$FreeDiskSpace = Get-WmiObject -Class Win32_logicaldisk -Filter "DriveType ='3'" | Select -ExpandProperty FreeSpace

Write-Output $Table1| ConvertTo-Html -Head $Header | Out-File -FilePath C:\users\public\documents\FreeSpaceTimeStamp.html

while ($True)
{

$Table1 = new-object psobject
$Table1 | add-member noteproperty Date/Time $DateTime
$Table1 | add-member noteproperty FreeSpace $FreeDiskSpace

Start-Sleep -Seconds 2
}

Right now, the code doesn't keep adding lines to the same HTML file but simply hangs in the while loop and I don't understand why.

Any help is appreciated.

Thanks,

Seth

sethjbr
  • 125
  • 2
  • 11
  • I'm not seeing where the while loop will ever write to anything, it appears all it will do is instantiate a psobject object and then add note properties to it over and over. Is there missing code where it should write to the HTML file with in the loop? – Persistent13 Apr 15 '19 at 21:57
  • If I move the Write-Output command from line 12 into the while loop and move the $Table commands outside the While loop, than it still doesn't add another line every time the write-output command is executed. Is there a method of adding a row to a PsObject that will take the new DateTime and Free disk Space and add it to the table? – sethjbr Apr 15 '19 at 22:08

1 Answers1

0

As for …

Is there a method of adding a row to a PsObject

... See this Q&A.

Powershell: Add lines to a custom objects

# array of objects (not 'lines'). So
$serverList= @() 
$serverList+= @{ServerName= 'blabla1'; OSType='Windows XP'}
$serverList+= @{ServerName= 'blabla2'; OSType='Windows XP Profesional'}

#display as table
$serverList | % { new-object PSObject -Property $_}

You do this in memory, until the user is done, then write or update the serialized file. Not while it is open by another process that has t locked. Only one process can be editing a file at a time. This is not a PowerShell limitation, it is an OS one.

postanote
  • 15,138
  • 2
  • 14
  • 25
  • Hmm... I saw that post, but it doesn't address my question. In that Q and A, the data is known, but in my case, the row data is not known until the current date/time is found again each time. – sethjbr Apr 16 '19 at 14:40
  • Also, by using an HTML file, I can refresh the page and obtain the new data that is being written to it by the script without terminating the script. This is not the case with a txt file or a CSV file. I can't have them open while the script is running and writing to the file. – sethjbr Apr 16 '19 at 14:46
  • Have you considered writing to temp file then merging on task complete? – postanote Apr 16 '19 at 17:18
  • I haven't considered that, no. That seems way too complicated than it needs to be. I have a script working now that will create a CSV file and add a new row after every entry using a hash table, but the format of the CSV data is very strange. – sethjbr Apr 16 '19 at 17:29
  • Is that script different than what you posted? If so, update your question as well the expected output vs what you say is strange. – postanote Apr 20 '19 at 03:09
  • I figured it out. I had to run a macro on the outputted CSV file to remove the non-printing characters on the output. Although I haven't found a solution to my original question and the stated requirements, I have made the CSV option work despite that fact that it can't be viewed while it is being created. – sethjbr Apr 25 '19 at 18:25
  • This question and answer helped a lot towards developing that solution. https://stackoverflow.com/questions/9813228/add-rows-to-csv-file-in-powershell I don't know if I should post this question as answered or not with this solution because my initial question still isn't answered. Thoughts?? – sethjbr Apr 25 '19 at 18:28