0

We have a html file that is used to test whether a internal tool is properly installed or not.

The html file outputs two values Connected or Connection failed.

Below is the screenshot, when we opened the html file through browser.

enter image description here

I need above values to be output via PowerShell terminal.

I tried Invoke-Webrequest but couldnt get the required values.

Invoke-WebRequest C:\websocket.html | Select content

Content

{60, 33, 68, 79...}

Invoke-WebRequest C:\websocket.html | Select-Object -ExpandProperty rawcontent
Content-Length: 886
Content-Type: application/octet-stream

<!DOCTYPE html>
<html>
   <meta charset = "utf-8" />
   <title>WebSocket Test</title>

   <script language = "javascript" type = "text/javascript">
      var wsUri = "ws://localhost:9091/";
      var output;
    
      function init() {
         output = document.getElementById("output");
         testWebSocket();
      }
    
      function testWebSocket() {
         websocket = new WebSocket(wsUri);
            
         websocket.onopen = function(evt) {
            onOpen(evt)
         };

     websocket.onclose = function(evt){
        onClose(evt)
     };
      }

    function onClose(evt) {
        document.getElementById('output').innerHTML += 'CONNECTION FAILED';
    }
    
      function onOpen(evt) {
    document.getElementById('output').innerHTML += 'CONNECTED';

      }
    
      window.addEventListener("load", init, false);
   
   </script>

   <h2>WebSocket Test</h2>
   <div id = "output"></div>

</html>

Any help?

Edited: (Invoke-WebRequest C:\websocket.html).Content 60, 33, 68, 79, 67, 84, 89, 80, 69, 32, 104, 116, 109, 108, 62, 10, 60, 104, 116, 109, 108, 62, 10, 32, 32, 32, 60, 109, 101, 116, 97, 32, 99, 104, 97, 114, 115, 101, 116, 32, 61, 32, 34, 117, 116, 102, 45, 56, 34, 32, 47, 62, 10, 32, 32, 32, 60, 116, 105, 116, 108, 101, 62, 87, 101, 98, 83, 111, 99, 107, 101, 116, 32, 84, 101, 115, 116, 60, 47, 116, 105, 116, 108, 101, 62, 10, 10, 32, 32, 32, 60, 115, 99, 114, 105, 112, 116, 32, 108, 97, 110, 103, 117, 97, 103, 101, 32, 61, 32, 34, 106, 97, 118, 97, 115, 99, 114, 105, 112, 116, 34, 32, 116, 121, 112, 101, 32, 61, 32, 34, 116, 101, 120, 116, 47, 106, 97, 118, 97, 115, 99, 114, 105, 112, 116, 34, 62, 10, 32, 32, 32, 32, 32, 32, 118, 97, 114, 32, 119, 115, 85, 114, 105, 32, 61, 32, 34, 119, 115, 58, 47, 47, 108, 111, 99, 97, 108, 104, 111, 115, 116, 58, 57, 48, 57, 49, 47, 34, 59, 10, 32, 32, 32, 32, 32, 32, 118, 97, 114, 32, 111, 117, 116, 112, 117, 116, 59, 10, 9, 10, 32, 32, 32, 32, 32, 32, 102, 117, 110, 99, 116, 105, 111, 110, 32, 105, 110, 105, 116, 40, 41, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 111, 117, 116, 112, 117, 116, 32, 61, 32, 100, 111, 99, 117, 109, 101, 110, 116, 46, 103, 101, 116, 69, 108, 101, 109, 101, 110, 116, 66, 121, 73, 100, 40, 34, 111, 117, 116, 112, 117, 116, 34, 41, 59, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 116, 101, 115, 116, 101, 98, 83, 111, 99, 107, 101, 116, 40, 41, 59, 10, 32, 32, 32, 32, 32, 32, 125, 10, 9, 10, 32, 32, 32, 32, 32, 32, 102, 117, 110, 99, 116, 105, 111, 110, 32, 116, 101, 115, 116, 87, 101, 98, 83, 111, 99, 107, 101, 116, 40, 41, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 119, 101, 98, 115, 111, 99, 107, 101, 116, 32, 61, 32, 110, 101, 119, 32, 87, 101, 98, 83, 111, 99, 107, 101, 116, 40, 119, 115, 85, 114, 105, 41, 59, 10, 9, 9, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 119, 101, 98, 115, 111, 99, 107, 101, 116, 46, 111, 110, 111, 112, 101, 110, 32, 61, 32, 102, 117, 110, 99, 116, 105, 111, 110, 40, 101, 118, 116, 41, 32, 123, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 111, 110, 79, 112, 101, 110, 40, 101, 118, 116, 41, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 125, 59, 10, 10, 9, 32, 119, 101, 98, 115, 111, 99, 107, 101, 116, 46, 111, 110, 99, 108, 111, 115, 101, 32, 61, 32, 102, 117, 110, 99, 116, 105, 111, 110, 40, 101, 118, 116, 41, 123, 10, 9, 9, 111, 110, 67, 108, 111, 115, 101, 40, 101, 118, 116, 41, 10, 9, 32, 125, 59, 10, 32, 32, 32, 32, 32, 32, 125, 10, 10, 9, 102, 117, 110, 99, 116, 105, 111, 110, 32, 111, 110, 67, 108, 111, 115, 101, 40, 101, 118, 116, 41, 32, 123, 10, 9, 9, 100, 111, 99, 117, 109, 101, 110, 116, 46, 103, 101, 116, 69, 108, 101, 109, 101, 110, 116, 66, 121, 73, 100, 40, 39, 111, 117, 116, 112, 117, 116, 39, 41, 46, 105, 110, 110, 101, 114, 72, 84, 77, 76, 32, 43, 61, 32, 39, 67, 79, 78, 78, 69, 67, 84, 73, 79, 78, 32, 70, 65, 73, 76, 69, 68, 39, 59, 10, 9, 125, 10, 9, 10, 32, 32, 32, 32, 32, 32, 102, 117, 110, 99, 116, 105, 111, 110, 32, 111, 110, 79, 112, 101, 110, 40, 101, 118, 116, 41, 32, 123, 10, 9, 100, 111, 99, 117, 109, 101, 110, 116, 46, 103, 101, 116, 69, 108, 101, 109, 101, 110, 116, 66, 121, 73, 100, 40, 39, 111, 117, 116, 112, 117, 116, 39, 41, 46, 105, 110, 110, 101, 114, 72, 84, 77, 76, 32, 43, 61, 32, 39, 67, 79, 78, 78, 69, 67, 84, 69, 68, 39, 59, 10, 10, 32, 32, 32, 32, 32, 32, 125, 10, 9, 10, 32, 32, 32, 32, 32, 32, 119, 105, 110, 100, 111, 119, 46, 97, 100, 100, 69, 118, 101, 110, 116, 76, 105, 115, 116, 101, 110, 101, 114, 40, 34, 108, 111, 97, 100, 34, 44, 32, 105, 110, 105, 116, 44, 32, 102, 97, 108, 115, 101, 41, 59, 10, 32, 32, 32, 10, 32, 32, 32, 60, 47, 115, 99, 114, 105, 112, 116, 62, 10, 10, 32, 32, 32, 60, 104, 50, 62, 87, 101, 98, 83, 111, 99, 107, 101, 116, 32, 84, 101, 115, 116, 60, 47, 104, 50, 62, 10, 32, 32, 32, 60, 100, 105, 118, 32, 105, 100, 32, 61, 32, 34, 111, 117, 116, 112, 117, 116, 34, 62, 60, 47, 100, 105, 118, 62, 10, 10, 60, 47, 104, 116, 109, 108, 62, 10

8
  • What does (Invoke-WebRequest C:\websocket.html).Content say? Commented Sep 30, 2021 at 12:21
  • @Theo: It just outputs series of random numbers. Commented Sep 30, 2021 at 13:26
  • Those aren't random numbers, They are the ascii values of the html text Commented Sep 30, 2021 at 14:30
  • 60,33,68,79,67,84,89,80,69,32,104,116,109,108,62 spells <!DOCTYPE html> Commented Sep 30, 2021 at 15:07
  • In order to process the javascript, try checking the ParsedHtml property returned by Invoke-WebRequest instead of content/rawcontent. Commented Sep 30, 2021 at 17:48

3 Answers 3

0

Try this

Invoke-RestMethod -Uri "C:\websocket.html"
Sign up to request clarification or add additional context in comments.

Comments

0

Invoke-WebRequest does not actually handle local html files. Even though it looks like it is successful, it just... opens the file. This is made more clear in newer versions of powershell (v7.1 below):

Invoke-WebRequest C:\websocket.html

[Error] Invoke-WebRequest: Only 'http' and 'https' schemes are allowed. (Parameter 'requestUri')

Another option is to use the internet explorer COM objects to basically open the file in IE, and scrape the page:

$url = 'C:\websocket.html'
$ie = New-Object -com internetexplorer.application  -Property @{visible = $false}
$ie.navigate($url)

# Optionally wait for connection
while ($ie.Busy -eq $true){sleep 1}
$output = $ie.Document.documentElement.innerText
# Output the last line of the returned page
$output -split [environment]::NewLine | Select -Last 1
$ie.Quit()

# Outputs:
CONNECTION FAILED

If it doesn't look like the output is working, set visible = $true, and see if IE is complaining about something (for me, I had to allow ActiveX).

Comments

-1
$value= Invoke-RestMethod -Uri "C:\websocket.html" 
$op = [char[]]$value.Content
$op -join ''

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.