locked
Out-File to variable RRS feed

  • Question

  • Hi

    I wrote a script that needs to have the output displayed on an html page. I currently have it creating an html file then opening it. Is it possible to have it store the information in a variable, instead of creating a file, then display the output automatically like the second example (which doesn't work) below?

    ConvertTo-HTML -body $body -head $head -As Table | Out-File  'C:\Rackhat.html' | Start-Process 'C:\Rackhat.html'

    ConvertTo-HTML -body $body -head $head -As Table | Out-File -OutVariable $Rackhat | Start-Process $Rackhat

    Thanks

    Monday, October 24, 2016 2:43 PM

Answers

  • This is possible by combining techniques:

    $ie = New-Object -ComObject InternetExplorer.Application
    $ie.Visible = $true
    $ie.Navigate('about:blank')
    $ie.Document.body.outerHTML = '<h1>Hello World</h1>'
    $html = dir c:\ | ConvertTo-Html | Out-String
    $ie.Document.Body.outerHTML = $html


    \_(ツ)_/


    • Edited by jrv Monday, October 24, 2016 4:54 PM
    • Marked as answer by RackHat Monday, October 24, 2016 11:30 PM
    Monday, October 24, 2016 4:54 PM
  • How are you going to open an HTML page without starting a browser application?

    How are you going to tell the browser application, which is a separate process, to use the content of a PowerShell variable?

    The answer is that you cannot do that. You have to create file and use the browser to open the file.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by RackHat Monday, October 24, 2016 4:07 PM
    Monday, October 24, 2016 3:52 PM

All replies

  • In what you are doing, no, because Start-Process requires a file.

    -- Bill Stewart [Bill_Stewart]

    Monday, October 24, 2016 2:49 PM
  • $Rackhat =  'C:\Rackhat.html'
    ConvertTo-HTML -body $body -head $head -As Table | Out-File $Rackhat 
    Start-Process $Rackhat

    Monday, October 24, 2016 3:12 PM
  • Thanks. However, I need to do this without creating a file on the local machine.

    ConvertTo-HTML -body $body -head $head -As Table -OutVariable out

    The above displays the needed html in PowerShell ISE when I run my script. Is it possible to get this to open an html page without creating a file and just using the variable ($out)? 

    Thanks

    Monday, October 24, 2016 3:39 PM
  • How are you going to open an HTML page without starting a browser application?

    How are you going to tell the browser application, which is a separate process, to use the content of a PowerShell variable?

    The answer is that you cannot do that. You have to create file and use the browser to open the file.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by RackHat Monday, October 24, 2016 4:07 PM
    Monday, October 24, 2016 3:52 PM
  • Thanks
    Monday, October 24, 2016 4:07 PM
  • This is possible by combining techniques:

    $ie = New-Object -ComObject InternetExplorer.Application
    $ie.Visible = $true
    $ie.Navigate('about:blank')
    $ie.Document.body.outerHTML = '<h1>Hello World</h1>'
    $html = dir c:\ | ConvertTo-Html | Out-String
    $ie.Document.Body.outerHTML = $html


    \_(ツ)_/


    • Edited by jrv Monday, October 24, 2016 4:54 PM
    • Marked as answer by RackHat Monday, October 24, 2016 11:30 PM
    Monday, October 24, 2016 4:54 PM
  • Awesome! Got it to work with my script. Always appreciate your help.
    Monday, October 24, 2016 11:31 PM
  • Great.  Good luck.

    \_(ツ)_/

    Monday, October 24, 2016 11:32 PM