ZyxelKeeneticNDMSv2.psm1
# http://download.from.zyxel.ru/76b36d5d-1901-4d4d-975c-06a06da201aa/cli_manual_Keenetic_Extra_2.07.pdf Function Connect-NdmsSession { param( [string]$IP = '192.168.1.1', [PSCredential]$Credential ) $Global:NdmsSession = [ordered]@{'Requestid' = 0} $NdmsSession.Uri = [string]::Concat('http://',$IP,'/ci') $RequestBody = @" <request id="$($NdmsSession.Requestid)"><command name="show system"></command></request> "@ $RequestParam = @{ 'UseBasicParsing' = $True 'SessionVariable' = 'WebSession' 'ContentType' = 'text/xml' 'Uri' = $NdmsSession.Uri 'Method' = 'Post' 'Credential' = $Credential 'Body' = $RequestBody } $Responce = Invoke-RestMethod @RequestParam $Responce.SelectNodes('/packet/response') $NdmsSession.WebSession = $WebSession } Function Invoke-NdmsCommand { <# .LINK https://sawfriendship.wordpress.com/ .EXAMPLE Invoke-NdmsCommand -CommandName "interface" -Name Gre10 -XmlNode '/packet/response/message' #> param( [string]$CommandName, [string]$Name, [switch]$No, [string]$XmlNode = '/packet/response' ) if(!$NdmsSession.WebSession){ Write-Error -Message 'Session Variable not found. Use Connect-NdmsSession' break } $RequestBody = @" <packet ref="/"> <request id="$($NdmsSession.Requestid)"> <command name="$CommandName"> $(@{$true = '<no/>'; $false = $Null}[[bool]$No]) $(@{$true = $Null; $false = $('<name>'+$Name+'</name>')}[[string]::IsNullOrEmpty($Name)]) $(@{$true = $Null; $false = $('<argument>'+$Value+'</argument>')}[[string]::IsNullOrEmpty($Value)]) </command> </request> </packet> "@ $RequestParam = @{ 'UseBasicParsing' = $True 'WebSession' = $NdmsSession.WebSession 'ContentType' = 'text/xml' 'Uri' = $NdmsSession.Uri 'Method' = 'Post' 'Body' = $RequestBody } try { $Responce = Invoke-RestMethod @RequestParam } catch { Write-Error -Message $Error[0] break } if($Responce.SelectNodes('/packet/response/error').'#text'){ Write-Error $($Responce.SelectNodes('/packet/response/error').'#text') } else { $Responce.SelectNodes($XmlNode) } } Function Get-NdmsProcesses { param ( [string]$Name ) Invoke-NdmsCommand -CommandName 'show processes' -Name $Name -XmlNode '/packet/response/process' } Function Get-NdmsVersion { param () Invoke-NdmsCommand -CommandName 'show version' } Function Get-NdmsSiteSurvey { <# Get-NdmsSiteSurvey WifiMaster1 #> param ( [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)][alias("id")]$WifiMasterInterfaceID ) Invoke-NdmsCommand -CommandName 'show site-survey' -Name $WifiMasterInterfaceID -XmlNode '/packet/response/ap_cell' } Function Get-NdmsRunningConfig { param() (Invoke-NdmsCommand -CommandName 'show running-config' -XmlNode '/packet/response/message').'#text' } Function Get-NdmsRunningConfigSection { param( [Parameter(Mandatory=$true)][string]$Pattern, [switch]$NotMatch, [switch]$SimpleMatch, [switch]$CaseSensitive ) $SearchParam = @{ 'Pattern' = [string]$Pattern 'NotMatch' = [bool]$NotMatch 'SimpleMatch' = [bool]$SimpleMatch 'CaseSensitive' = [bool]$CaseSensitive 'Quiet' = [bool]$true } Get-NdmsRunningConfig | ? {$_ -match '[\w\d\s]' -and $_ -notmatch '^$|^![\s\t]+$'} | % -Begin {$Sections = [hashtable]@{}; [int]$i = 0} -Process { if ($_ -match '^[\s\t]+') { $Sections[$i] = @($Sections[$i], $_.ToString()) -join [string][char]10 } else { $i += 1 $Sections[$i] = @($Sections[$i], $_.ToString()) -join [string][char]10 } } $Sections.GetEnumerator() | ? {$_.Value | Select-String @SearchParam} | % {$_.Value} } Function Get-NdmsInterface { param ( [string]$Name = '*', [string]$Description = '*' ) Invoke-NdmsCommand -CommandName 'show interface' -XmlNode '/packet/response/interface' | ? {($_.name -like $Name) -and ($_.description -like $Description)} } Function Add-NdmsInterface { param ( [Parameter(Mandatory=$true)][string]$Name ) Invoke-NdmsCommand -CommandName 'interface' -Name $Name -XmlNode '/packet/response/message' } Function Remove-NdmsInterface { param ( [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)][string[]]$Name ) Begin {$NdmsInterface = Get-NdmsInterface} Process { foreach ($interface in $Name) { $NdmsInterface | ? {$_.name -like $interface} | % { Write-Warning -Message "Remove interface $($_.name)" Invoke-NdmsCommand -CommandName 'interface' -Name $_.name -XmlNode '/packet/response/interface/message' -No } } } End {} } Function Get-NdmsIpRoute { param () Invoke-NdmsCommand -CommandName 'show ip route' -XmlNode '/packet/response/route' } Function Get-NdmsIpArp { param ( [string]$IP = '*', [string]$Mac = '*' ) $Invoke = Invoke-NdmsCommand -CommandName 'show ip arp' -XmlNode '/packet/response/arp' if((!$IP -or $IP -eq [string]'*') -and (!$Mac -or $Mac -eq [string]'*')) { $Invoke } else { $Invoke | ? {($_.ip -like $IP) -and (($_.mac -replace "[g-z]|[^\w\d]") -like ($Mac -replace "[g-z]|[^\w\d\*]")) } } } Function Get-NdmsIpNat { param ( [string]$Protocol = '*', [string]$Src = '*', [string]$Dst = '*', [string]$sPort = '*', [string]$dPort = '*', [string]$SrcOut = '*', [string]$DstOut = '*', [string]$sPortOut = '*' ) Invoke-NdmsCommand -CommandName 'show ip nat' -XmlNode '/packet/response/nat' | ? {($_.'protocol' -like $Protocol) -and ($_.'src' -like $Src) -and ($_.'dst' -like $Dst) -and ($_.'sport' -like $sPort) -and ($_.'dport' -like $dPort) -and ($_.'src-out' -like $SrcOut) -and ($_.'dst-out' -like $DstOut) -and ($_.'sport-out' -like $sPortOut)} } Function Get-NdmsIpSec { param () (Invoke-NdmsCommand -CommandName 'show ipsec' -XmlNode '/packet/response/ipsec_statusall').'#cdata-section' } Function Get-NdmsIpNameServer { param () Invoke-NdmsCommand -CommandName 'show ip name-server' -XmlNode '/packet/response/server' } Function Get-NdmsClock { param () Invoke-NdmsCommand -CommandName 'show clock date' -XmlNode '/packet/response' } Function Get-NdmsNtpStatus { param () Invoke-NdmsCommand -CommandName 'show ntp status' -XmlNode '/packet/response/status' } Function Get-NdmsProcesses { param ( [string]$Name = '*' ) Invoke-NdmsCommand -CommandName 'show processes' -XmlNode '/packet/response/process' | ? {$_.name -like $Name} } Function Get-NdmsLog { param ( [int]$Last = 100 ) Invoke-NdmsCommand -CommandName 'show log' -XmlNode '/packet/response/log' | Select-Object -Property timestamp,ident,@{'Name' = 'Text'; Expression = {$_.message.'#text'}} -Last $Last } Function Get-NdmsAssociations { param () Invoke-NdmsCommand -CommandName 'show associations' -XmlNode '/packet/response/station' } Function Save-NdmsConfig { param () (Invoke-NdmsCommand -CommandName 'system configuration save' -XmlNode '/packet/response/message').'#text' } Function Get-NdmsSystem { param () Invoke-NdmsCommand -CommandName 'show system' -Name $Name -XmlNode '/packet/response' } Function Restart-NdmsSystem { param () Invoke-NdmsCommand -CommandName 'system reboot' -Name $Name -XmlNode '/packet/response' } Function Get-NdmsSystemUptime { param () (Get-Date).AddSeconds((Get-NdmsSystem).uptime) - (Get-Date) } |