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 2)
# 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
$QNAPClass = New-Object -TypeName PSObject -Prop @{Username = $null; Password = $null; _BaseUrl = $null; _SID = $null}
$QNAPClass | Add-Member -MemberType ScriptMethod -Name "_InitSession" -Value {
If(-Not $this._SID.Length){
$this._Login()
}
}
$QNAPClass | Add-Member -MemberType ScriptMethod -Name "_HandleResponse" -Value {
param([Parameter(Mandatory=$True)]
$request)
$data = [Xml]$request.Content
If($data.QDocRoot.authPassed.InnerText -ne 1){
Return $NULL
}
Return $data.QDocRoot
}
$QNAPClass | Add-Member -MemberType ScriptMethod -Name "_ExecuteGet" -Value {
param([Parameter(Mandatory=$True)]
[string]$url,
[Parameter(Mandatory=$False)]
[bool]$appendSid = $True)
If($appendSid){
$url = $url + '&sid=' + $this._SID
}
$request = Invoke-WebRequest -Uri $url -UseBasicParsing
Return $this._HandleResponse($request)
}
$QNAPClass | Add-Member -MemberType ScriptMethod -Name "_ExecutePost" -Value {
param([Parameter(Mandatory=$True)]
[string]$url,
[Parameter(Mandatory=$True)]
$params,
[Parameter(Mandatory=$False)]
[bool]$appendSid = $True)
If($appendSid){
$url = $url + "&sid=" + $this._SID
}
$request = Invoke-WebRequest -Uri $url -Method POST -Body $params -UseBasicParsing
Return $this._HandleResponse($request)
}
$QNAPClass | Add-Member -MemberType ScriptMethod -Name "_GetUrl" -Value {
param([Parameter(Mandatory=$True)]
[string]$url)
$this._InitSession()
Return $this._ExecuteGet($url)
}
$QNAPClass | Add-Member -MemberType ScriptMethod -Name "_Login" -Value {
$params = @{'user' = $this.Username; 'pwd' = $this.Password}
$url = $this._BaseUrl + 'authLogin.cgi'
$response = $this._ExecutePost($url, $params, $False)
If(!$response){
Return $False
}
$this._SID = $response.authSid.InnerText
Return $True
}
$QNAPClass | Add-Member -MemberType ScriptMethod -Name "GetFirmwareUpdate" -Value {
$response = $this._GetUrl($this._BaseUrl + 'sys/sysRequest.cgi?subfunc=firm_update')
Return $response.func.ownContent.newVersion.InnerText
}
function QNAPClass{
param([Parameter(Mandatory=$True)]
[String]$hostname,
[Parameter(Mandatory=$True)]
[int]$port,
[Parameter(Mandatory=$True)]
[String]$username,
[Parameter(Mandatory=$True)]
[String]$password)
$qnapObj = $QNAPClass.psobject.copy()
$qnapObj.Username = $username
$qnapObj.Password = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($password))
If(!($hostname.StartsWith('http://') -Or $hostname.StartsWith('https://'))){
$_host = 'https://' + $hostname
}
Else { $_host = $hostname }
$qnapObj._BaseUrl = [string]::Format("{0}:{1}/cgi-bin/", $_host, $port)
$qnapObj
}
# Alternatively, you can use http:// and port 8080 (not recommended)
$qnap = QNAPClass -hostname '192.168.1.5' -port 443 -username 'admin' -password '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.