Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Query a QNAP for any available updates using the API (PowerShell 5)
# Ignore self-certs
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()
# Ported/tweaked from: https://github.com/colinodell/python-qnapstats
Class QNAPStats{
[string]$Hostname
[string]$Username
hidden [string]$Password
[int]$Port
hidden [string]$BaseUrl
hidden [string]$SID
QNAPStats([string]$hostname, [int]$port, [string]$username, [string]$password){
$this.Username = $username
$this.Password = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($password))
If(!($hostname.StartsWith('http://') -Or $hostname.StartsWith('https://'))){
$this.Hostname = 'https://' + $hostname
}
Else { $this.Hostname = $hostname }
$this.BaseUrl = [string]::Format("{0}:{1}/cgi-bin/", $this.Hostname, $port)
}
_InitSession(){
If(-Not $this.SID.Length){
$this.Login()
}
}
[System.Xml.XmlElement] _HandleResponse($request){
$data = [Xml]$request.Content
If($data.QDocRoot.authPassed.InnerText -ne 1){
Return $NULL
}
Return $data.QDocRoot
}
[System.Xml.XmlElement] _ExecuteGet([string]$url, [bool]$appendSid = $True){
If($appendSid){
$url = $url + '&sid=' + $this.SID
}
$request = Invoke-WebRequest -Uri $url
Return $this._HandleResponse($request)
}
[System.Xml.XmlElement] _ExecutePost([string]$url, $params, [bool]$appendSid = $True){
If($appendSid){
$url = $url + "&sid=" + $this.SID
}
$request = Invoke-WebRequest -Uri $url -Method POST -Body $params
Return $this._HandleResponse($request)
}
[System.Xml.XmlElement] _GetUrl([string]$url){
$this._InitSession()
Return $this._ExecuteGet($url, $True)
}
[bool] Login(){
$params = @{'user' = $this.Username; 'pwd' = $this.Password}
$response = $this._ExecutePost($this.BaseUrl + 'authLogin.cgi', $params, $False)
If(!$response){
Return $False
}
$this.SID = $response.authSid.InnerText
Return $True
}
[string] GetFirmwareUpdate(){
$response = $this._GetUrl($this.BaseUrl + 'sys/sysRequest.cgi?subfunc=firm_update')
Return $response.func.ownContent.newVersion.InnerText
}
}
# Alternatively, you can use http:// and port 8080 (not recommended)
[QNAPStats] $qnap = [QNAPStats]::new('192.168.1.5', 443, 'admin', 'yourpassword')
$qnap.GetFirmwareUpdate()
# Example responses: "none", "error", or "4.3.4.0695 Build 20180830"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.