Powershell: Get XML through a SOCKSv5 proxy - powershell

For a reporting tool I need to download an XML which is only accessible through a SOCKS proxy. My idea was to install the proxy in IE but disable it, then in powershell enable it a few seconds so I download it, but that doesn't seem to work. Anybody got any other work-arounds?
Here is my code
$proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
$proxy = new-object System.Net.WebProxy
$proxy.Address = $proxyAddr
$proxy.useDefaultCredentials = $true
$url = "https://appmon-apm.####.##/rest/management/dashboard/xmlgen"
$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)
$str

Related

Powershell - How to receive large response-headers from error-response of a web-request?

I am looking for a solution to parse an error-response of a given web-service.
Below sample works great in general, but if the response is larger than 64kb then the content is not availabe in the exception at all.
I have seen some solutions recommending to use webHttpClient and increase the MaxResponseContentBufferSize here, but how can I do this for a given WebClient-object?
Is there any option to change that BufferSize globally for all net-webcalls like below TLS12-settings?
Here is my sample-code:
# using net-webclient to use individual user-side proxy-settings:
$web = new-object Net.WebClient
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$url = "address to web-service"
try {
$response = $web.DownloadString($url)
} catch [System.Net.WebException] {
# this part needs to work even if the error-response in larger than 64kb
# unfortunately the response-object is empty in such case
$message = $_.Exception.Response
$stream = $message.GetResponseStream()
$reader = new-object System.IO.StreamReader ($stream)
$body = $reader.ReadToEnd()
write-host "#error:$body"
}
I solved it at the end by switching to system.net.httpclient.
That way I still repect any custom proxy-settings and also avoid the above mentioned 64kb-limit in any error-response. Here a sample how to use it:
$url = "address to web-service"
$cred = Get-Credential
# define settings for the http-client:
Add-Type -AssemblyName System.Net.Http
$ignoreCerts = [System.Net.Http.HttpClientHandler]::DangerousAcceptAnyServerCertificateValidator
$handler = [System.Net.Http.HttpClientHandler]::new()
$handler.ServerCertificateCustomValidationCallback = $ignoreCerts
$handler.Credentials = $cred
$handler.PreAuthenticate = $true
$client = [System.Net.Http.HttpClient]::new($handler)
$client.Timeout = [System.TimeSpan]::FromSeconds(10)
$result = $client.GetAsync($url).result
$response = $result.Content.ReadAsStringAsync().Result
write-host $response

powershell WebClient downloadFile always returns 401 though NetworkCredential passed correctly

I am trying to download jenkins.war file through powershell on windows server, but I get 401 Unauthorized error in downloadFile. I passed correct network credentials still getting this error. Below is my code:
$source = "http://mirrors.jenkins.io/war-stable/latest/jenkins.war"
$destination = "$jenkinsDir\jenkins.war"
$client = new-object System.Net.WebClient
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential("admin", "P#ssw0rd")
$credCache.Add("http://mirrors.jenkins.io/war-stable/latest/jenkins.war", "Basic", $creds)
$client.Credentials = $credCache
$client.downloadFile($source, $destination)
How to get rid of this 401 error?
I also tried this link System.Net.WebClient doesn't work with Windows Authentication but no luck.

Using PowerShell to FTP to label printer - mimic command-line FTP command

I'm trying to print files to an Intermec printer. I can do it with ftp command like:
put C:\myfile.prn pr1
Now I'm trying to do the same thing with PowerShell and I've been able to upload files but I'm not sure how execute the last part, which is the port of the printer pr1.
This is what I got so far.
$Dir = "C:\files"
$ftp = "ftp://printerip/pr1/"
$user = "admin"
$pass = "pass"
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
#list every sql server trace file
foreach($item in (dir $Dir "*.prn")) {
"Uploading $item..."
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
}
You are uploading the local file myfile.prn to the remote "file" pr1.
So do the same in PowerShell:
$ftp = "ftp://printerip/pr1"
$webclient.UploadFile($ftp, $item.FullName)

Powershell using gzip for downloading by WebClient and Downloadstring()

Is it possible to make a downloadstring() and using gzip-compression, if the server is accepting this?
$wc = New-Object System.Net.WebClient
$wc.Encoding = [System.Text.Encoding]::UTF8
$wc.Headers.Add("User-Agent: Other")
$qc = $wc.Downloadstring($url)
Does anyone know the correct Headers.Add or what do we have to add?
Try this:
$url = "http://www.somewebsite.com/"
$wc = New-Object System.Net.WebClient
$wc.Headers.Add([System.Net.HttpRequestHeader]::AcceptEncoding, "gzip")
$wc.Headers.Add("User-Agent: Other")
$qc = $wc.Downloadstring($url)
Alternatively you can try and use DownloadFile method to see if this yields an expected result:
$wc.DownloadFile($url, "c:\temp\dump.txt" )
If there are errors then update your question to include them.

Using PsGet through a Proxy

I'm having a hard time modifying PsGet to work though a proxy. I replaced every $client initialization on PsGet.psm1 with this
# $client = (new-object Net.WebClient)
$proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
$proxy = new-object System.Net.WebProxy
$proxy.Address = $proxyAddr
$proxy.useDefaultCredentials = $true
$client = new-object system.net.WebClient
$client.proxy = $proxy
but I still keep getting the DotNetMethodException during the WebClient request.
Give this a try. Use [System.Net.WebRequest]::DefaultWebProxy instead of the registry read.
$wc = New-Object System.Net.WebClient
$wc.Headers.Add("User-Agent","Mozilla/4.0+")
$wc.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$wc.DownloadString("https://github.com/chaliy/psget/raw/master/PsGet/PsGet.psm1")