Question: How to implement the choice of an action option in one scenario? Is it possible to do this through a function and substitute only the necessary parameters when choosing an option?
There are remote devices that need to be backed up if necessary. There is a way to do this manually, after logging in, waiting for the GUI to fully load - it may take 5-10 minutes. I wrote a CURL authorization request - it works. Then I did not bother with the command line, because there is a need to dodge, there is no necessary functionality. I rewrote it to PowerShell.
Step 1. Request authorization
$ip = Read-Host "Enter the ip address"
$xmlauth = "<request method=""login"" request_id=""1"" username=""name"" password=""pass""/>"
$access = Invoke-WebRequest -Uri ("http://"+$ip+":4000/xap2") `
-Method Post `
-ContentType "text/xml" `
-Body $xmlauth
$sessionid = ([xml]$access.Content).response.session_id
Write-Host $access #example of output after successful authorization
<response username="name" session_id="123456789" request_id="1" access_lvl="2">
<status status="0" status_text="OK"/>
</response>
Write-Host $sessionid #this id is inserted in all subsequent requests
123456789
Step 2. Request to change the parameters
$xml3in = @"
<request method="set" request_id="2" session_id="$sessionid" return_events="false">
<data>
<ports>
<asi_input enable="false" _id="0"/>
<asi_input enable="true" _id="2"/>
<_all _id="8">
<plprep main_globid="2"/>
</_all>
</ports>
</data>
</request>
"@
$request3in = Invoke-WebRequest -Uri ("http://"+$ip+":4000/xap2") `
-Method Post `
-ContentType "text/xml" `
-Body $xml3in
$status = ([xml]$request3in.Content).response.status.status_text
Write-Host $request3in #Example output after successful execution
<response request_id="2">
<status status="0" status_text="OK"/>
</response>
Write-Host $status #An example of the output of the status text, you can also do it by number
OK
if ( $status -like 'ok' ) {
Write-Host "THE DEVICE IS SWITCHED WHERE IT IS NEEDED ($status)" -foregroundcolor Green
}
else {
Write-Host "SOMETHING WENT WRONG ($status)" -foregroundcolor Red
}
Step 3. Request to change the parameters (return to the initial state) The only difference is in the request body.
$xml1in = @"
<request method="set" request_id="3" session_id="$sessionid" return_events="false">
<data>
<ports>
<asi_input enable="true" _id="0"/>
<asi_input enable="false" _id="2"/>
<_all _id="8">
<plprep main_globid="0"/>
</_all>
</ports>
</data>
</request>
"@
Update 10/28/24 Added menu. Now the function needs to be implemented in the menu selection options.
function askchoice {
Clear-Host
Write-Host "░░░░░ RESERVATION MENU ░░░░░" -backgroundcolor DarkBlue
Write-Host "░ ░" -backgroundcolor DarkBlue
Write-Host "░ 1. ENTER IP ADDRESS ░" -backgroundcolor DarkBlue
Write-Host "░ 2. TRANSFER TO 3in ░" -backgroundcolor DarkBlue
Write-Host "░ 3. RETURN TO 1in ░" -backgroundcolor DarkBlue
Write-Host "░ Q. EXIT ░" -backgroundcolor DarkBlue
Write-Host "░ ░" -backgroundcolor DarkBlue
Write-Host "░░░░░░░░░░░░░░░░░░░░░░░░░░░░" -backgroundcolor DarkBlue
Write-Host "CURRENT STATION IP ADDRESS: $ip" -ForegroundColor Green
}
Do {
askchoice
$choice = Read-Host "SELECT NUMBER [1-3] OR Q TO LOG OUT"
switch ($choice)
{
1 {
$ban = @("10.10.10.10", "10.10.10.11", "10.10.10.12", "10.10.10.13")
do {
$Global:ip = Read-Host "ENTER IP ADDRESS"
if ($ban -contains $ip) {
Write-Host "ACCESS DENIED" -ForegroundColor Red
}
}
While ($ban -contains $ip)
}
2 {
Write-Host "BACKUP PROCESS IN PROGRESS" -ForegroundColor Cyan
#line of code
ping $ip; pause
}
3 {
Write-Host "RETURN TO STANDARD SCHEME IN PROCESS IN PROGRESS" -ForegroundColor Cyan
#line of code
tracert $ip; pause
}
q {
exit
}
default {
Write-Host "NO NUMBER IN LIST, REPEAT SELECT" -ForegroundColor Yellow; pause
}
}
}
while ($choice -ne "q")
At the moment, this is the working design that has been achieved.
#Request body
$auth = [xml]"<request method=""login"" request_id=""1"" username=""name"" password=""pass""/>"
$backup = [xml]@"
<request method="set" request_id="2" session_id="" return_events="false">
<data>
<ports>
<asi_input enable="false" _id="0"/>
<asi_input enable="true" _id="2"/>
<_all _id="8">
<plprep main_globid="2"/>
</_all>
</ports>
</data>
</request>
"@
$primary = [xml]@"
<request method="set" request_id="3" session_id="" return_events="false">
<data>
<ports>
<asi_input enable="true" _id="0"/>
<asi_input enable="false" _id="2"/>
<_all _id="8">
<plprep main_globid="0"/>
</_all>
</ports>
</data>
</request>
"@
$check = [xml]@"
<request method="get" request_id="4" session_id="">
<data>
<ports>
<_all _id="8">
<plprep _select="main_globid"/>
</_all>
</ports>
</data>
</request>
"@
#POST requests by URL + Body
function Select-Replacer
{
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$url,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[xml]$body
)
$ErrorActionPreference = "STOP"
try
{
$response = Invoke-WebRequest -Uri $url -Method Post -ContentType "text/xml" -Body $body
Write-Host ("RESPONSE CODE - "+$response.StatusCode) -Foregroundcolor Green
Write-Host ("CONNECTION - "+$response.StatusDescription) -Foregroundcolor Green
$sessionid = ([xml] $response.Content).response.session_id
#Write-Host ("session_id in function - "+$sessionid)
$status = ([xml]$response.Content).response.status.status_text
#Write-Host ("execution status in a function - "+$status)
$asiout = ([xml]$response.Content).response.data.ports.asi_output.plprep.main_globid
#Write-Host ("source id in function - "+$asiout)
}
catch
{
#$e = $Error[0].Exception
$e = $_.Exception
Write-Host ("ERROR - "+$e.Message) -Foregroundcolor Red
$ecode = [int]$e.Response.StatusCode
if ($ecode -eq 404)
{
Write-Host ("RESPONSE CODE - "+$e.Response.StatusCode.value__) -Foregroundcolor Red
Write-Host ("NO CONNECTION - "+$e.Response.StatusDescription) -Foregroundcolor Red
}
else
{
Write-Host ("RESPONSE CODE - "+$e.Response.StatusCode.value__) -ForegroundColor Yellow
Write-Host ("SOMETHING WENT WRONG - "+$e.Response.StatusDescription) -ForegroundColor Yellow
}
}
$ErrorActionPreference = "CONTINUE"
Return @{"session"=$sessionid; "status"=$status; "source"=$asiout;}
}
#Menu with the ability to clear the console
function Ask-Choice
{
Clear-Host
Write-Host "░░░░ RESERVATION MENU ░░░░" -Backgroundcolor DarkBlue
Write-Host "░ ░" -Backgroundcolor DarkBlue
Write-Host "░ 1. ENTER IP ADDRESS ░" -Backgroundcolor DarkBlue
Write-Host "░ 2. REGISTER NOW ░" -Backgroundcolor DarkBlue
Write-Host "░ 3. SWITCH TO 3in ░" -Backgroundcolor DarkBlue
Write-Host "░ 4. RETURN TO 1in ░" -Backgroundcolor DarkBlue
Write-Host "░ 5. CHECK THE SCHEMA ░" -Backgroundcolor DarkBlue
Write-Host "░ Q. QUIT ░" -Backgroundcolor DarkBlue
Write-Host "░ ░" -Backgroundcolor DarkBlue
Write-Host "░░░░░░░░░░░░░░░░░░░░░░░░░░░░" -Backgroundcolor DarkBlue
}
#Selecting an option and calling a function
Do
{
Ask-Choice
Write-Host "CURRENT STATION IP ADDRESS: $ip" -ForegroundColor Magenta
$choice = Read-Host "SELECT NUMBER [1-5] OR Q TO EXIT"
switch ($choice)
{
1
{
$ban = @("10.10.10.10", "10.10.10.11", "10.10.10.13", "10.10.10.14", "10.10.10.15")
do
{
$ip = Read-Host "ENTER IP ADDRESS"
$ipregex = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
$url = "http://{0}:8049/xap2" -f $ip
#$url = ("http://"+$ip+":8049/xap2")
if ($ban -contains $ip)
{
Write-Host "WARNING: ACCESS DENIED" -ForegroundColor Red
}
elseif ($ip -notmatch $ipregex)
{
Write-Host "WARNING: INCORRECT IP OR EMPTY VALUE" -ForegroundColor Yellow
}
}
While (($ban -contains $ip) -or ($ip -notmatch $ipregex))
}
2
{
Write-Host "REGISTRATION PROCESS IN PROGRESS" -ForegroundColor Cyan
$result = Select-Replacer $url $auth
$XMLbody = $backup, $primary, $check
Foreach ($item in (Select-XML -Xml $XMLbody -XPath "//request"))
{
$item.node.session_id = "$($result.session)"
}
Write-Host ("Session_id number - "+$($result.session))
Write-Host ("Execution status - "+$($result.status))
if ($($result.status) -like 'ok')
{
Write-Host "SUCCESS" -ForegroundColor Green; pause
}
else
{
Write-Host "ERROR" -Foregroundcolor Red -Backgroundcolor White; pause
}
Clear-Variable -name result.status
}
3
{
Write-Host "THE RESERVATION PROCESS IS IN PROGRESS" -foregroundColor Cyan
$result = Select-Replacer $url $backup
Write-Host ("Execution status - "+$($result.status))
if ($($result.status) -like 'ok')
{
Write-Host "SUCCESS" -ForegroundColor Green; pause
}
elseif ($($result.status) -like "Unknown session ID")
{
Write-Host "REQUIRED TO REGISTER" -Foregroundcolor Red -Backgroundcolor White; pause
}
else
{
Write-Host "ERROR" -Foregroundcolor Red -Backgroundcolor White; pause
}
Clear-Variable -name result.status
}
4
{
Write-Host "THE PROCESS OF RETURNING TO THE STANDARD SCHEME IS IN PROGRESS" -foregroundColor Cyan
$result = Select-Replacer $url $primary
Write-Host ("Execution status - "+$($result.status))
if ($($result.status) -like 'ok')
{
Write-Host "SUCCESS" -ForegroundColor Green; pause
}
elseif ($($result.status) -like "Unknown session ID")
{
Write-Host "REQUIRED TO REGISTER" -Foregroundcolor Red -Backgroundcolor White; pause
}
else
{
Write-Host "ERROR" -Foregroundcolor Red -Backgroundcolor White; pause
}
Clear-Variable -name result.status
}
5
{
Write-Host "CHECKING THE CURRENT STATE OF THE SHEMA" -ForegroundColor Cyan
$result = Select-Replacer $url $check
Write-Host ("Execution status - "+$($result.status))
Write-Host ("This is the source id - "+$($result.source))
if (($($result.status) -like 'ok') -and ($($result.source) -eq '0'))
{
Write-Host "PRIMARY SOURCE 1.in" -Foregroundcolor Yellow; pause
}
elseif (($($result.status) -like 'ok') -and ($($result.source) -eq '2'))
{
Write-Host "BACKUP SOURCE 3.in" -Foregroundcolor Green; pause
}
elseif ($($result.status) -like "Unknown session ID")
{
Write-Host "REQUIRED TO REGISTER" -Foregroundcolor Red -Backgroundcolor White; pause
}
else
{
Write-Host "ERROR" -Foregroundcolor Red -Backgroundcolor White; pause
}
Clear-Variable -name result.status
}
q
{
exit
}
default
{
Write-Host "THERE IS NO NUMBER IN THE LIST, REPEAT YOUR SELECTION" -ForegroundColor Yellow; pause
}
}
}
while ($choice -ne "q")