Automating JNLP Downloads with PowerShell Using Session Cookies
When managing remote servers or BMC interfaces, some resources such as JNLP (Java Network Launch Protocol) files require authentication via cookies and session handling. Manually downloading these files can be cumbersome. PowerShell provides a way to automate this process using web sessions and cookie management.
Creating a Persistent Web Session
A web session in PowerShell allows you to persist cookies, headers, and other session parameters across multiple requests. This is essential when working with authenticated resources. To create a session and set a custom user agent, use:
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
Adding Required Cookies
Many interfaces require specific cookies for authentication. In PowerShell, you can add cookies to the session like this:
$session.Cookies.Add((New-Object System.Net.Cookie("test", "1", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("SessionCookie", "V3rBznVdGPy1MDotq4DBUoMiYfbEnEq9000", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("BMC_IP_ADDR", "10.10.10.4", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("SessionExpired", "false", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("Username", "admin", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("PNO", "4", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("gMultiLAN", "true", "/", "10.10.10.4")))
Some cookies, like settings, may contain special characters ({}, [], ,) that are not allowed directly. You must URL-encode them:
$settingsValue = "{eth:[0,1],ethstr:['eth0','eth1'],lan:[1,8],enable:[1,1],flag:[1,1]}"
$encodedValue = [System.Net.WebUtility]::UrlEncode($settingsValue)
$session.Cookies.Add((New-Object System.Net.Cookie("settings", $encodedValue, "/", "10.10.10.4")))
Downloading the JNLP File
Once the session and cookies are configured, you can download the JNLP file using Invoke-WebRequest. Include headers like Referer and Accept to mimic a browser request:
Invoke-WebRequest `
-Uri "http://10.10.10.4/Java/jviewer.jnlp?EXTRNIP=10.10.10.4&JNLPSTR=JViewer" `
-WebSession $session `
-Headers @{
"Accept" = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
"Accept-Language" = "en-US,en;q=0.9,sw;q=0.8"
"Accept-Encoding" = "gzip, deflate"
"Referer" = "http://10.10.10.4/page/jviewer_launch_new.html?JNLPSTR=JViewer&JNLPNAME=/Java/jviewer.jnlp"
"Upgrade-Insecure-Requests" = "1"
} `
-OutFile "jviewer.jnlp" `
-UseBasicParsing
This will save the jviewer.jnlp file to your current directory, ready for launching with Java Web Start.
Benefits of Using PowerShell Sessions
- Persistent Authentication: Cookies and session information persist across requests.
- Automation: Scripted downloads remove the need for manual browser interaction.
- Custom Headers: Easily set headers to mimic real browser behavior.
- Compatibility: Works across PowerShell 5.1 and PowerShell Core (7+).
Full Working Script
Here is the complete script in one block, ready to run:
# Create and configure a persistent web session
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"
# Add required cookies
$session.Cookies.Add((New-Object System.Net.Cookie("test", "1", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("SessionCookie", "V3rBznVdGPy1MDotq4DBUoMiYfbEnEq9000", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("BMC_IP_ADDR", "10.10.10.4", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("SessionExpired", "false", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("Username", "admin", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("PNO", "4", "/", "10.10.10.4")))
$session.Cookies.Add((New-Object System.Net.Cookie("gMultiLAN", "true", "/", "10.10.10.4")))
# URL-encode the complex settings cookie value
$settingsValue = "{eth:[0,1],ethstr:['eth0','eth1'],lan:[1,8],enable:[1,1],flag:[1,1]}"
$encodedValue = [System.Net.WebUtility]::UrlEncode($settingsValue)
$session.Cookies.Add((New-Object System.Net.Cookie("settings", $encodedValue, "/", "10.10.10.4")))
# Download JNLP using the authenticated session
Invoke-WebRequest `
-Uri "http://10.10.10.4/Java/jviewer.jnlp?EXTRNIP=10.10.10.4&JNLPSTR=JViewer" `
-WebSession $session `
-Headers @{
"Accept" = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
"Accept-Language" = "en-US,en;q=0.9,sw;q=0.8"
"Accept-Encoding" = "gzip, deflate"
"Referer" = "http://10.10.10.4/page/jviewer_launch_new.html?JNLPSTR=JViewer&JNLPNAME=/Java/jviewer.jnlp"
"Upgrade-Insecure-Requests" = "1"
} `
-OutFile "jviewer.jnlp" `
-UseBasicParsing
This script ensures all session cookies are set correctly, handles complex cookie values safely, and automates the download of the JNLP file.
Related Guides
Complete Guide to Downloading Files with PowerShell
Introduction PowerShell provides powerful tools for downloading files from web servers, with Invoke-WebRequest being the primary cmdlet for making HTTP requests. This guide covers everything from basic downloads to advanced scenarios involving authentication, cookies, and custom headers. Basic File Downloads Simple Download The most straightforward way to download a file: Download with Progress Bar PowerShell […]
The Complete Guide to Installing StorCLI on Linux and Windows
StorCLI (Storage Command Line Tool) is Broadcom’s powerful command-line utility for managing LSI MegaRAID and PRAID controllers. Whether you’re managing hardware RAID arrays on servers or workstations, StorCLI provides comprehensive control over your storage infrastructure. This guide will walk you through the complete installation process on both Linux and Windows systems. What is StorCLI? StorCLI […]
SNMP Exporter Installation Guide
Introduction This guide provides step-by-step instructions for installing the Prometheus SNMP Exporter on various platforms. The SNMP Exporter allows Prometheus to monitor network devices like routers, switches, firewalls, and other SNMP-enabled infrastructure. Official Repository: SNMP Exporter on GitHub What You’ll Need Before starting the installation, ensure you have: Installation Method 1: Binary Installation (Linux) This […]