Let's understand what that error means and review 3 different ways to fix it.
Photo by Max Rovensky on Unsplash |
This sort article details a good trick for those getting the following error in PowerShell:
The server committed a protocol violation
After - Modifying your powershell.exe.config;
- Modifying your request in order to avoid it stopping if your url is invalid in .NET
- Modifying your request in order to avoid it stopping if your url is invalid in PowerShell.
Solution 1 - Modifying powershell.exe.config
This solution will apply to all PowerShell requests so use with caution.
Also, this solution is not portable so, if you plan to run this script on a
machine you don't have admin access to, this solution will probably not
work.
<system.net>
<settings>
<httpWebRequest
useUnsafeHeaderParsing="true" />
</settings>
</system.net>
Solution 2 - Modifying your request in .NET
To modify your request, have the code below before you actually run Invoke-WebRequest:
$netAssembly =
[Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])
if($netAssembly)
{
$bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic"
$settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal")
$instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @())
if($instance)
{
$bindingFlags = "NonPublic","Instance"
$useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags)
if($useUnsafeHeaderParsingField)
{
$useUnsafeHeaderParsingField.SetValue($instance, $true)
}
}
}
if($netAssembly)
{
$bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic"
$settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal")
$instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @())
if($instance)
{
$bindingFlags = "NonPublic","Instance"
$useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags)
if($useUnsafeHeaderParsingField)
{
$useUnsafeHeaderParsingField.SetValue($instance, $true)
}
}
}
Solution 3 - Modifying your request in PowerShell
This is probably the simplest. Just run the line below:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {
$true }
Conclusion
On this post we reviewed three different ways to resolve a problem in
PowerShell. Don't know PowerShell yet? I would urge you to take a look and
learn it (even if just the basics). PowerShell is a powerful tool used
extensively in devops on Windows, Azure and
Linux.
References
See Also
- My journey to 1 million articles read
- Getting last modified software on Windows using PowerShell
- Determining installed .NET Framework versions using PowerShell
- Windows Subsystem for Linux, the best way to learn Linux on Windows
- Why I use Fedora Linux
- How I fell in love with i3
- Diagnosing and Fixing WSL initialization issues
- Creating a Ubuntu Desktop instance on Azure
- Hosting NuGet packages on GitHub
- My journey to 1 million articles read
- How and why use stronger passwords