I am trying to use the Invoke-WebRequest cmdlet (for the first time) to connect to the web interface for a Sharp printer. So far, the code I have is the following:
$cred = Get-Credential
$url = 'http://<IP address of printer>/login.html?/main.html'
$login = Invoke-WebRequest $url -SessionVariable printer -Method Get
$login.Forms[0].Fields.element10002 = $cred.UserName
$login.Forms[0].Fields.element10002 =
$cred.GetNetworkCredential().Password
$mainPage = Invoke-WebRequest -Uri ($url + $login.Forms[0].Action) `
-WebSession $printer -Body $login -Method Post
...but I keep getting this error:
Invoke-WebRequest : The underlying connection was closed: The connection
was closed unexpectedly.
At line:6 char:13
+ $mainPage = Invoke-WebRequest -Uri ($url + $login.Forms[0].Action) -W
...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation:
(System.Net.HttpWebRequest
:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId :
WebCmdletWebResponseException,Microsoft.Powe
rShell.Commands.InvokeWebRequestCommand
This line of code does not cause an error:
$login = Invoke-WebRequest $url -SessionVariable printer -Method Get
But this line does:
$mainPage = Invoke-WebRequest -Uri ($url + $login.Forms[0].Action) `
-WebSession $printer -Body $login -Method Post
I am using PS version 5.1, and Tls12 on a Windows 7 box. After some Googling, it seems like this problem might have something to do with the version of Tls. but I'm not sure what to change it to.
Does anybody have any ideas? - @leah_cyberpadawan