Powershell Web with Proxy - powershell

I am getting a proxy error with my script. "The remote server returned an error: (407) Proxy Authentication Required."
IE uses a script to configure it's connection (that's all the info I have). I looked in there and tried some IPs/ports in my script, but none seemed to work. Is there some other way of using this IE connection script in PS?
$web_client = New-Object System.Net.WebClient;
$web_client.Headers.Add("user-agent", "PowerWeb");
$proxyAddr = "http://172.16.192.34:9090"
$proxy = new-object System.Net.WebProxy
$proxy.useDefaultCredentials = $true
$proxy.Address = $proxyAddr
$web_client.proxy = $proxy
$data = $web_client.DownloadData("http://stackoverflow.com")

If you are using PowerShell 3.0 you can use Invoke-WebRequest

Related

Powershell script unable to decrypt password from Jenkins withCredentials()

I created a powershell script to run DB2 queries in Jenkins
withCredentials([usernamePassword(credentialsId: 'cred-id', usernameVariable: 'ID', passwordVariable: 'PASSWORD')]) {
$cn = new-object system.data.OleDb.OleDbConnection("Server=Server; Provider=IBMDADB2;DSN=DBName;User Id=$ID;Password=$PASSWORD");
$ds = new-object "System.Data.DataSet" "ds"
$q = "myQuery"
$da = new-object "System.Data.OleDb.OleDbDataAdapter" ($q, $cn)
$da.Fill($ds)
$cn.close()
}
If I run the script and hard code my credentials, it run fine.
With withCredentials(), I am getting the following error: Security processing failed with reason "15" ("PROCESSING FAILURE")
From some research, the error seems to be because DB2 can't handle encrypted data. Is there a way to overcome this error?
EDIT:
I tried to add
$SecurePassword = ConvertTo-SecureString $PASSWORD -AsPlainText -Force
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)
$UnsecurePassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
at the beginning of my powershell script, but it still throws the same error even though the credential work fine if used in plain text
If I understand the docs for the Credentials Binding Jenkins plugin correctly, the variables designated in the withCredentials() call become environment variables, so as to enable their use across process boundaries.
Note that the values of these environment variables are not encrypted, so no extra (decryption) effort is required on the part of the target process.
Therefore, you need to use $env:[1] instead of just $ to refer to these variables in PowerShell:
$cn = new-object system.data.OleDb.OleDbConnection "Server=Server; Provider=IBMDADB2;DSN=DBName;User Id=$env:ID;Password=$env:PASSWORD"
[1] See the conceptual about_Environment_Variables help topic.

Powershell, Invoke Web-Request Error (just at Windows Server 2012R)

at the moment i am writing some scripts to handle our Backups of our Costumers.
So the backups are running over a Synology Box.
The Script works really fine with Windows 10 and Windows Server 2019.
But i can not start it at WindowsServer 2012R2. And i tried so many things to handle but it didnt work.
$ip = "XXXXXXXXXX"
$u = "XXXXXXX"
$p = "XXXXXXX"
$d = "XXXXXX"
#Session ID auslesen & Authentifizierung
$a = Invoke-WebRequest -UseBasicParsing "http://$ip/webapi/auth.cgi?api=SYNO.API.Auth&version=2&method=login&account=$u&passwd=$p&session=ActiveBackup&format=cookie"
$ajson = $a.Content
$aobject = ConvertFrom-Json -InputObject $ajson
$s = $aobject.data.sid
At first he shows me the error that he can not handle utf-8, ive updated Powershell to Version 5.1 and now Windows always tells me that the Value of $a is not allowed to be null. It Seems that he cannot receive any session Number. On Windows 10 and Windows Server 2019 it runs perfectly. Please can anybody help me please. Would beeee really nice
Greetings
Yaspo
Edit i got it.
For everybody who got the same problems just go the classic way without
invoke-webrequest
$url = "http://$ip/webapi/auth.cgi?api=SYNO.API.Auth&version=2&method=login&account=$u&passwd=$p&session=ActiveBackup&format=cookie"
$wc = New-Object System.Net.WebClient
$result = $wc.DownloadString($url)

How to run powershell script using default credential from sql job agent

I am downloading a file from sharepoint.
I have already scheduled the job in SQL job agent
Its working fine when i use the following code
$UserName = "xxxx"
$PswdPath = "D:\securestring.txt"
$SecurePassword = cat $PswdPath| convertto-securestring
$fileName = [System.IO.Path]::GetFileName($FileUrl)
$DownloadPath = "D:\Excel\"
$downloadFilePath = [System.IO.Path]::Combine($DownloadPath,$fileName)
$client = New-Object System.Net.WebClient
$client.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$client.DownloadFile($FileUrl, $downloadFilePath)
$client.Dispose()
But the problem here i face is whenever i update my password i need to update the secure string as well
So i wanted to use the default credentials
so i used the following script
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
$webclient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$webclient.DownloadFile($FileUrl, $DownloadPath)
but its getting failed with the following error
Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized."
went through different blogs all were suggesting the same approach which i have followed
Any help in this regard?
As far as I understand default credentials users the account and password the sql agent process is started and since it will not match the SharePoint online account it will fail. It would be easier if you create a powershell that updates all secure strings once password is changed.

access sharepoint REST api using powershell with windows authentication

I have a sharepoint 2013 server, which I can log into using Windows Authentication via a web-browser. When I have logged on using my browser, if I then - using that same browser - browse to http://mysharepointserver/_api/web I get some useful XML.
However, I want to write a powershell script that uses this XML. To that end, following the suggestions from this page:
How to make an authenticated web request in Powershell?
I wrote the following code:
$PWord = ConvertTo-SecureString –String "MyAwesomePassword" –AsPlainText -Force
$creds = New-Object System.Net.NetworkCredential("MyUserName",$PWord,"TheDomain")
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $creds
$url = "http://mysharepointserver/_api/web"
$output = $webclient.DownloadString($url)
echo $output
However, when I run this code, I get the error message:
Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (403) Forbidden."
Even though I can access the same URL using a web-browser, if I type in the (same) credentials using the Windows login dialog that pops up for authentication purposes.
Does anyone know if it is possible to do this using Powershell?
thanks heaps all
Why aren't you using the default CMDlets that come with Powershell (unless using PS2 or lower)?
Invoke-Webrequest and Invoke-Restmethod both support the -credential switch which can be used to authenticate against the webserver.
You can also use Get-credential to store your credentials (though you can't get yhem from a file or string that way, at least not the password)
Script (in pseudo code and since i'm on my phone no code tags) would look like this :
Invoke-Restmethod -uri 'http://bla/api' -credential (get-credential)

Powershell FTPWebRequest and EnableSsl = True

I searched this site and found an FTPWebRequest example via Powershell. I put it to use and it works fine. However, when I enable SSL via EnableSsl=$True, all I get is timeouts or a delayed "227 Entering Passive Mode", which breaks the process. As soon as I disable EnableSsl, I can fly right through. Can someone point me in the right direction? SSL is supported on the FTP host.
I'd eventually like change the method to DownloadFile and loop the code to download files, after I get the list and find matches. I'd like to do it securely, though.
# Create an FTPWebRequest object to handle the connection to the FTP server
$ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri)
# Set the request's network credentials for an authenticated connection
$ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password)
# Set FTPWebRequest method to ListDirectory
$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
$ftprequest.EnableSsl = $True
$ftprequest.UseBinary = $False
$ftprequest.UsePassive = $True
$ftprequest.KeepAlive = $False
$ftpresponse = $ftprequest.GetResponse()
Write-Out $ftpresponse.StatusCode
Write-Out $ftpresponse.StatusDescription
Come to find out, spontaneous issue was server-side.