Created
July 14, 2023 18:36
-
-
Save markgodiy/7f30f97b4c2da4d3354632652b7caa05 to your computer and use it in GitHub Desktop.
WIP-Test-HP-HTTPWebRequest.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $printerIP = <IPaddrr> | |
| # $firmwareFilePath = "C:\path\to\ljM506_fs3.9.12_fw_2309088_001464.bdl" | |
| $policyType = [System.AppDomain]::CurrentDomain.GetAssemblies() | | |
| ForEach-Object { $_.GetTypes() } | | |
| Where-Object { $_.Name -eq 'TrustAllCertsPolicy' } | |
| if (-not $policyType) { | |
| $code = @" | |
| using System.Net; | |
| using System.Security.Cryptography.X509Certificates; | |
| public class TrustAllCertsPolicy : ICertificatePolicy | |
| { | |
| public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) | |
| { | |
| return true; | |
| } | |
| } | |
| "@ | |
| $assembly = Add-Type -TypeDefinition $code -Language CSharp -PassThru | |
| [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
| } | |
| # $firmwareBytes = [System.IO.File]::ReadAllBytes($firmwareFilePath) | |
| # $firmwareBase64 = [System.Convert]::ToBase64String($firmwareBytes) | |
| $requestBody = @{ | |
| firmware = $firmwareBase64 | |
| } | ConvertTo-Json | |
| $loginUrl = "https://$printerIP/hp/device/SignIn/Index" | |
| try { | |
| $request = [System.Net.HttpWebRequest]::Create($loginUrl) | |
| $request.Method = "POST" | |
| $request.ContentType = "application/json" | |
| $requestBodyBytes = [System.Text.Encoding]::UTF8.GetBytes($requestBody) | |
| $request.ContentLength = $requestBodyBytes.Length | |
| $requestStream = $request.GetRequestStream() | |
| $requestStream.Write($requestBodyBytes, 0, $requestBodyBytes.Length) | |
| $requestStream.Close() | |
| $response = $request.GetResponse() | |
| if ($response.StatusCode -eq [System.Net.HttpStatusCode]::OK) { | |
| Write-Host "Good Connection: $printerIP" | |
| } else { | |
| Write-Host "No Connection: $printerIP" | |
| } | |
| $response.Close() | |
| } catch { | |
| Write-Host "Error connecting to the printer with IP: $printerIP" | |
| Write-Host $_.Exception.Message | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment