Skip to content

Instantly share code, notes, and snippets.

@Demonslay335
Created September 20, 2018 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Demonslay335/e6db68da15ddc57f63ffe60d72b82917 to your computer and use it in GitHub Desktop.
Save Demonslay335/e6db68da15ddc57f63ffe60d72b82917 to your computer and use it in GitHub Desktop.
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