I can't get the following code to work. It appears to log in but then returns the login page with $response. I am guessing it has something to do with the postbacks? Any way to get around that? Thanks!
$login = Invoke-WebRequest -Uri 'http://www.sqlpass.org/UserLogin.aspx' -SessionVariable sqlpass
$login.Forms[0].Fields["txtUsername_14615"] = 'myuser'
$login.Forms[0].Fields["txtPassword_14615"] = 'mypass'
$response = Invoke-WebRequest -Uri 'http://www.sqlpass.org/UserLogin.aspx' -WebSession $sqlpass -Method POST -Body $login
There is an event target field that also needs to be set, also the POST needs a different URL, i tested the solution below and it works:
$login = Invoke-WebRequest -Uri 'http://www.sqlpass.org/UserLogin.aspx' -SessionVariable sqlpass
$form = $login.Forms[0]
$form.Fields["__EVENTTARGET"] = "UserLogin"
$form.Fields["txtUsername_14615"] = 'myuser'
$form.Fields["txtPassword_14615"] = 'mypass'
Invoke-WebRequest -Uri 'http://www.sqlpass.org/UserLogin.aspx?returnurl=%2fdefault.aspx' -WebSession $sqlpass -Method POST -Body $form.Fields
Note: Just as a side note, you can use Web Debugging proxies like Fiddler to debug issues like this, which is exactly what i did.
Related
just got a simple code from my friend.
He wants to fill a form on a website and save the results.
The code fills the fields correctly, but thats it, nothing more.
I'm new to coding so andybod can help me / has a hint?
$R = Invoke-WebRequest -URI https://kfz2.virtuelles-rathaus.de/igv/servlet/Internetgeschaeftsvorfaelle?MANDANT=08215000"&"AUFRUF=WKZ -SessionVariable fb
$Form = $R.Forms[0]
$Form.fields
$Form.Fields["INPUT-TXT_KENNZEICHENSUCHE_ERKENNUNGSZEICHEN"]="MK"
$Form.Fields["INPUT-TXT_KENNZEICHENSUCHE_ZIFFERN"]="??"
$Uri = "https://kfz2.virtuelles-rathaus.de/igv/servlet/Internetgeschaeftsvorfaelle?MANDANT=08215000&AUFRUF=WKZ" + $Form.Action
$R = Invoke-WebRequest -Uri $Uri -WebSession $FB -Method POST -Body $Form.Fields
Trying to connect to a REST-API via Powershell client. When testing the endpoint in Postman, I have no problems at all. Here's the main part of the function (I have a [pscredential]$Creds parameter that I use to get the username and password):
[string]$username = $Creds.UserName
[string]$password = (New-Object System.Net.NetworkCredential($Creds.UserName, $Creds.Password, 'Null')).Password
[string]$authorizationInfo= ([Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $username, $password))))
Invoke-WebRequest -Uri "https://$($HostName)/api/" -Method Get -Headers #{Authorization = ('Basic {0}' -f $authorizationInfo)}
For some reason the Authorization header is different in my script than in Postman. I can even copy the Authorization header out of Postman and paste it into the -Headers parameter and everything works fine. I just don't see where I'm getting this wrong.
I can't tell you why that's not working, but I can suggest something that works for me all the time with APIs:
$auth = $username + ':' + $upassword
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
$authorizationInfo = [System.Convert]::ToBase64String($Encoded)
$headers = #{"Authorization"="Basic $($authorizationInfo)"}
Invoke-WebRequest -Uri "https://$($HostName)/api/" -Method GET -Headers $headers
If that doesn't work, try this subtle difference with Invoke-Restmethod:
Invoke-RestMethod -Uri "https://$($HostName)/api/" -Method GET -Headers $headers
Working with APIs is always an adventure. Keep trying. :)
https://www.ibm.com/support/knowledgecenter/en/SS4GSP_6.2.7/com.ibm.udeploy.api.doc/topics/udclient_addversionstatus.html
How to make this PUT call using Powershell please?. I am using Powershell 5.
I came across this post while trying to do the same thing. The problem for me was in knowing what exactly the correct URL was (see Adam Parsons' answer):
$URL = "url-goes-here"
After a lot of searching (IBM's documentation was not worth much in this effort), I was able to identify the correct URL by way of watching traffic in Chrome developer tools (thanks to Darrell Schrag's blog post: https://drschrag.wordpress.com/2013/10/03/the-udeploy-rest-api).
For those searching for this, my PowerShell REST call sequence now looks like this (and executes successfully):
$tokenEncoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( "PasswordIsAuthToken:{"token":"$authToken"}" ))
$headers = #{Authorization = "Basic "+$tokenEncoded}
# 1. Get component version ID
$uri = "$uDeployServer:8443/cli/version/getVersionId`?component=$componentName&version=$versionName"
$versionId=Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
# 2. Add component version status
$uri = "$uDeployServer:8443/rest/deploy/version/$versionId/status/$versionStatus"
Invoke-RestMethod -Uri $uri -Method PUT -Headers $headers
Probably something like this...
$Hash = #{
Component="StringValue"
Version="StringValue"
Status="StringValue"
}
$Json = $Hash | ConvertTo-Json
$URL = "url-goes-here"
$Cred = Get-Credential
Invoke-RestMethod -Method "POST" -Uri $url -Credential $Cred -Body $Json
I am having difficulty creating a Powershell script to login to a website and download a file. The script seems to be logging in just fine (I receive a successful status return) but when I attempt to download the file, I receive an Unauthorized message in return.
Code is below:
$r=Invoke-WebRequest http://testurl/index.htm -SessionVariable fb
$form = $r.Forms[0]
$form.fields["loginusername"] = "user"
$form.fields["loginpassword"] = "pass"
$r=Invoke-WebRequest -Uri ("https://testurl/index.htm" + $form.Action) -WebSession $fb -Method POST -Body $form.Fields
Invoke-WebRequest -Uri "https://testurl/api/table.csv" -WebSession $fb -OutFile "C:\status\current.csv"
Thanks for any guidance!
I've seen the example you're trying to reference. It had a few mistakes when I tried to replicate it, too. I've automated some site logins/downloads; here's an adjustment to try:
$r=Invoke-WebRequest http://testurl/index.htm
$r.Forms.fields.loginusername = 'user'
$r.Forms.fields.loginpassword = 'pass'
Invoke-WebRequest -Uri 'https://testurl' + $form.Action) -SessionVariable fb -Method POST -Body $r
Invoke-WebRequest -Uri 'https://testurl/api/table.csv' -WebSession $fb -OutFile 'C:\status\current.csv'
I'm trying to access a Swagger based API using powershell invoke-restmethod with websession to (hopefully) capture the cookies/session information I'd need to do a post method.
I start by requesting a CSRF
$CSRF = Invoke-RestMethod -Uri ($Uri+'csrf-token') -Method Get -Credential $Creds -ContentType 'application/json'-SessionVariable websession
and I can see the correct token value without any issues. Looking at the websession variable I do have some data, but I don't get any cookie values at all. Thus if I submit a second request using the session variable:
Invoke-RestMethod -Method Post -Uri ($Uri+'post') -Headers $Header -Body $Body -Credential $creds -WebSession $websession
it fails due to the missing cookie values. If I do a normal request via Firefox I see cookies with a jsessionid, etc but I don't know how to get these values somewhere where I can use them (please excuse me ignorance here- I'm relatively new to the invoke-restmethod in PS)
I've sussed it out (at last- very painful) - I had to build my own cookie:
$CSRF = Invoke-RestMethod -Uri ($Uri+'csrf-token') -Method Get -Credential $Creds -ContentType 'application/json' -SessionVariable websession -MaximumRedirection 0
$CSRFToken = $CSRF.tokenValue
# Capture cookie
$cookiejar = New-Object System.Net.CookieContainer
$cookieUrl = $uri +'csrf-token'
$cookieheader = ""
$webrequest = [System.Net.HTTPWebRequest]::Create($cookieUrl);
$webrequest.Credentials = $creds
$webrequest.CookieContainer = $cookiejar
$response = $webrequest.GetResponse()
$cookies = $cookiejar.GetCookies($cookieUrl)
# add cookie to websession
foreach ($cookie in $cookies) {$websession.Cookies.Add((Create-Cookie -name $($cookie.name) -value $($cookie.value) -domain $apiserverhost))}
# Finally, I can post:
Invoke-RestMethod -Method Post -Uri ($Uri+'versions/createVersionRequests') -Headers $Header -Body $Body -Credential $creds -WebSession $websession
Hope that helps someone else (I've spent hours pulling my hair out over this!)