Access web service behind Citrix NetScaler - powershell

I'm having an internal .NET web service which I try to access from the public network.
If using a browser, a user may enter login and passwd in a form on the Citrix NetScaler custom page and after the form is submitted, the browsing continues and the web service endpoint is working with no issues.
I don't know anything about Citrix NetScaler, but by checking the network calls in the browser's dev tools, I was able to get to the web service by using PowerShell and the a Microsoft.PowerShell.Commands.WebRequestSession object:
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie
$cookie.Name = "NSC_TASS"
$cookie.Value = "https://REST_SERVICE_DOMAIN/api/"
$cookie.Domain = "CITRIX_NETSCALER_DOMAIN.com"
$session.Cookies.Add($cookie);
$response = Invoke-WebRequest -Uri "https://CITRIX_NETSCALER_DOMAIN/cgi/login" `
-Method "POST" `
-Headers #{"Content-Type"="application/x-www-form-urlencoded"} `
-Body "login=URL_ENCODED_EMAIL&passwd=URLENCODED_PASSWORD" `
-WebSession $session
Is there a way to login by not using PowerShell's Microsoft.PowerShell.Commands.WebRequestSession object? Do I use the right endpoint?
EDIT: I'm looking for a solution which doesn't involve PowerShell, but it only relies on HTTP headers and cookies

I think you may be able to do -SessionVariable with Invoke-WebRequest, and then append the cookies a similar way.
Invoke-WebRequest https://CITRIX_NETSCALER_DOMAIN/cgi/login -SessionVariable session
...
$session.Cookies.Add($cookie);

Related

Is there any way to open a website with specified cookies in a browser using Powershell?

Is there any way I can use Powershell to open a website in a browser with the cookies specified?
Yes, by preparing a WebRequestSession instance for use with the Invoke-WebRequest cmdlet:
$uri = "https://some.website.example/path/page"
$session = [Microsoft.PowerShell.Commands.WebRequestSession]::new()
$session.Cookies.Add($uri, [System.Net.Cookie]::new("CookieName", "Value"))
Invoke-WebRequest $uri -WebSession $session

Invoke-RestMethod PowerShell Google DDNS

I seem to be having a lot of issues with this seemingly basic script to update my IP with Google's DDNS service.
#Fetch current IP address
$ipuri = "https://api.ipify.org"
$ip = Invoke-RestMethod -Uri $ipuri
#Send fetched IP address to Google Domains through their API
$uri = "https://username:password.google.com/nic/update?hostname=home.domain.com&myip="$ip""
Invoke-RestMethod -Method 'Get' -Uri $uri
The first issue is with the $ip variable. It produces no output if I keep the $ip on it but works fine without it (I need it as a variable as I use it later on).
The second issue is with the https://username:password#domains.google.com/nic/update?hostname=home.domain.com&myip="$ip".
It works fine if I dump the exact string into postman (substituting an actual IP address instead of $ip)
but fails to send anything even if I run it with a manually inserted IP (such as https://username:password#domains.google.com/nic/update?hostname=home.domain.com&myip=1.2.3.4) in PowerShell.
P.S. In my actual code I substitute in the correct username and password (as provided by Google) and correct subdomain, domain, top-level domain as it applies to me.
Any thoughts on this?
EDIT:
The updated (and working) code is looks like this:
#Fetches current IPv4 address
$ipuri = "https://api.ipify.org"
$ip = Invoke-RestMethod -Uri $ipuri
#Stores Google-provided username and password
$password = ConvertTo-SecureString "password" -AsPlainText -Force
$credential = New-Object Management.Automation.PSCredential ('username', $password)
#Send fetched IP address to Google Domains through their API
$uri = "https://domains.google.com/nic/update?hostname=home.domain.com&myip=$($ip)"
Invoke-RestMethod -Method 'POST' -Uri $uri -Credential $credential
First off, that's a neat service, api.ipify.org, I'll have to use that in the future.
Secondly, I think the only issue here is your definition of the $url.
The syntax you had before actually throws an error if you try to run the line on its own, error shown here.
"https://username:password.google.com/nic/update?hostname=home.domain.com&myip="$ip""
At line:1 char:81
+ ... e:password.google.com/nic/update?hostname=home.domain.com&myip="$ip""
+ ~~~~~
Unexpected token '$ip""' in expression or statement.
In PowerShell you should use string expansion syntax like this, instead of nesting quotes.
$uri = "https://username:password.google.com/nic/update?hostname=home.domain.com&myip=$($ip)"
Update
Found the API Docs here https://support.google.com/domains/answer/6147083?hl=en. They say to provide your username and password using Basic Auth, which makes a base64 encoded string of your credential. We can do that in PowerShell pretty easily!
You can provide your credentials my using the Get-Credential cmdlet to save them then pass them into Invoke-RestMethod and adding -Credential and -Authentication as parameters. Here is what a completed solution would look like.
$ipuri = "https://api.ipify.org"
$ip = Invoke-RestMethod -Uri $ipuri
#Send fetched IP address to Google Domains through their API
$myCredential = Get-Credential #you'll be prompted to provide your google username and pwd.
$uri = "https://domains.google.com/nic/update?hostname=home.domain.com&myip=$($ip)"
Invoke-RestMethod -Method 'POST' -Uri $uri -Credential $myCredential -Authentication Basic

Custom Data Refresh in Microsoft Power BI

I have a data source connected to to a dashboard that needs to refreshed without actually opening the power bi account. Currently I am able to figure out the powershell script that help me do it from my powershell promt.
The request is something like this
Invoke-WebRequest -Uri "https://api.powerbi.com/v1.0/myorg/datasets/DATASET_ID/refreshes" -Method "POST" -Headers #{"Sec-Fetch-Mode"="cors"; "Authorization"="Bearer XXXXXXXXXXTOKENXXXXXXXX"} -ContentType "application/json;charset=UTF-8"
Now the Token gets expired after sometime and I again need to open the power bi website to get the new token.
Is there a way for generate token locally without actually open the website? i tried using Login-PowerBI in Powershell to get token but is there any other way also.
you need to get a new token each time or get the refresh token from the API. I have tested getting token everytime and I never have a problem. Check my code here to complete your powershell:
https://github.com/ibarrau/PowerBi-code/blob/master/PowerShell/RefreshPowerBi.ps1
Remember you can always install an On Premise Data Gateway to solve this for you.
https://learn.microsoft.com/en-us/power-bi/service-gateway-onprem
As far as I understood, you want to refresh a dataset with PowerShell, but without prompting for credentials. In this case, you can store them in the script itself and do something like this:
Import-Module MicrosoftPowerBIMgmt
Import-Module MicrosoftPowerBIMgmt.Profile
$password = "xxxxx" | ConvertTo-SecureString -asPlainText -Force
$username = "xxxxx#yyyyy.com"
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Connect-PowerBIServiceAccount -Credential $credential
Invoke-PowerBIRestMethod -Url 'groups/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/datasets/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/refreshes' -Method Post
Disconnect-PowerBIServiceAccount

How to use PowerShell invoke-webrequest with Windows Authentication and Username\Password

The following PowerShell invoke-webrequest works for me when the Windows Service I'm running it from has permission to call the webservice. However, this isn't always the case.
I need to the ability to use Windows Authentication but also set the account username\password for the call. Does anyone have some sample code for doing this?
Invoke-WebRequest -UseBasicParsing "url" -UseDefaultCredentials -Method GET
You may set the UseDefaultCredentials property of Invoke-WebRequest module to true. Link to the official doc: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6
$url = "http://yourURL"
$wc = New-Object System.Net.WebClient
$wc.UseDefaultCredentials = $true
$response = $wc.DownloadString($url)

Powershell Invoke-WebRequest Login to website not working

I am trying to write a Powershell script that will login to and navigate a website. When trying to POST the login credentials, however, I'm getting the error "Server Error in '/Webpostage' Application.". I have no issue with logging in manually using a browser. I've tried using the same URI for both requests as well as using the Developer network monitor in Chrome to capture the actual URI of the POST (shown below). Any ideas?
$Username = "username"
$Password = "password"
# GET Stamps.com Login page
$R = Invoke-WebRequest https://print.stamps.com/webpostage/signin/default.aspx -SessionVariable StampsCom
$Form = $R.Forms[0]
# Fill in login credentials
$Form.Fields["UserNameTextBox"] = $Username
$Form.Fields["PasswordTextBox"] = $Password
# Login to Stamps.com
$R = Invoke-WebRequest -Uri ("https://print.stamps.com/webpostage/ajax/AuthenticateUser.aspx?LoginSource=SigninPage" + $Form.Action) -WebSession $StampsCom -Method POST -Body $Form.Fields