sharepoint online – Connect to SPO site with Proxy settings

I’m trying to connect to a SharePoint Online site using the Connect-pnponline cmdlet, but facing a proxy issue for the same. The exception I’m getting is : “Error: exception has been thrown by the target of an invocation”

I’m able to invoke-webrequest when I try accessing using my proxy credentials and also connect to SharePoint online using Sharegate when I pass the same set of credentials, but somehow not able to connect to SharePoint Online. Following is the code which I’m using :

(System.Net.WebRequest)::DefaultWebProxy = new-object System.Net.WebProxy ('proxy url with port')
(System.Net.WebRequest)::DefaultWebProxy.Credentials = new-object System.Net.Networkcredential("My ID with domain", "My Password")
(System.Net.WebRequest)::DefaultWebProxy.ByPassProxyOnLocal = $true
Import-Module "Path to SharePointPNPPowershellOnline" -MaximumVersion 3.9.1905.0 -DisableNameChecking
$credentials = Get-Credential #(some other credential)
Connect-PNPOnline -Url "My online site" -Credentials $credentials

Any help is appreciated.

ThankYou