Global variables and Sessions with functions

Hi! I’m developing a Dashboard that has a function to connect to a Linux SSH Session to reboot an IPPhone. I’m using SSHSession Module from here: https://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library

So, I use encrypted credentials to connec to the PBX as this:

$PasswordFile_PBX = “C:\UDService\Password_PBX.txt”
$key_PBXFile_PBX = “C:\UDService\AES_PBX.key”
$User_PBX = “root”
$key_PBX = Get-Content $key_PBXFile_PBX
$Cache:Credential_PBX = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User_PBX, (Get-Content $PasswordFile_PBX | ConvertTo-SecureString -Key $key_PBX)

The Function:

Function ResetIPPhone {

param ($extension,$pbx)

New-SshSession -ComputerName $pbx -Credential $Cache:Credential_PBX
$command = “asterisk -rx ‘sip notify polycom-check-cfg $extension’”
Invoke-SshCommand -ComputerName $pbx -command $command
Remove-SshSession -ComputerName $pbx

}

UD portion:

                            New-UDInput -Title "Reboot IP Phone" -SubmitText "Reboot" -Content {

                                New-UDInputField -Type 'textbox' -Name 'Extension' 

                            }-Endpoint {
                                param($Extension)

                                ResetIPPhone -Extension $Extension -pbx IP_OF_PBX
                                Show-UDToast -Message "Rebooting Phone..."
                            }

So, It does work perfect when I ran it, but then, if someone else runs it AFTER me (other extension for example), He gets the message “The Variable ‘Global:SshSessions’ cannot be retreived because it has not been set’”.
If I restart the UD Service, and then, that person tries to run reboot, it DOES work, but if I try, the error appears.

Any help with be appreciated.

Hi @abarrozo!
Are you running UD 2.7?
In 2.7 the cache variables are cleared after some time, so using cache as a “constant” is no longer optimal.
Dirty solution: run an endpoint on a schedule to set your variables.
Proper (probably): add your “constants” as variables in your endpointinizialization

Or just build the credentials from file within the same endpoint.

Hi BoSen29 and thanks for the quick response.
So, I do use 2.7 Community Edition

  • Removed Cache as you said
  • Built the credentials within the endpoint, same problem. Should I put the “credential builder” within the function?
  • Added it to the endpointinitialization, same problem

It seems that whoever do the first, takes control of that variable. Even when we all use the same encrypted files/credentials

Hi agaia @abarrozo!

Sounds sketchy, can you share some full code example?

Hi @abarrozo

I might have misunderstood but is the issue that the function isn’t loading consistently in UD?
Have you loaded your function into an endpoint?

Function ResetIPPhone ($extension,$pbx)
{...}

New-UDEndpointInitialization -Function ResetIPPhone  # <== 

# Copy of your code below

New-UDInput -Title "Reboot IP Phone" -SubmitText "Reboot" -Content {

    New-UDInputField -Type 'textbox' -Name 'Extension' 

            }-Endpoint {
                 param($Extension)

                ResetIPPhone -Extension $Extension -pbx IP_OF_PBX
                Show-UDToast -Message "Rebooting Phone..."
                        }

I hope this helps

Maylife

Hi maylife! The thing is that if any of my partners run the phone reset, it WILL work, but if me or anyone else tries to do it, it will fail. Is like that the function gets taken by the first user who uses it. Restarting UD will enable someone else to move on, but in the same scenario.

This is a cropped version of my code, that does the same thing.

I use UD as a Windows Service, but the example below is run from a Powershell instance just to show you.

Get-UDDashboard | Stop-UDDashboard

PBX PASS

$PasswordFile_PBX = “C:\UDService\Password_PBX.txt”
$key_PBXFile_PBX = “C:\UDService\AES_PBX.key”
$User_PBX = “root”
$key_PBX = Get-Content $key_PBXFile_PBX
$Credential_PBX = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User_PBX, (Get-Content $PasswordFile_PBX | ConvertTo-SecureString -Key $key_PBX)

#################################################################################################################################

Function ResetIPPhone {

param ($Extension,$PBX)

New-SshSession -ComputerName $pbx -Credential $Credential_PBX
$command = “asterisk -rx ‘sip notify polycom-check-cfg $Extension’”
Invoke-SshCommand -ComputerName $pbx -command $command
Remove-SshSession -ComputerName $pbx

}

$ResetPBX = New-UDPage -Name “IP Phone Reset” -Icon server -Endpoint {

New-UDInput -Title "IP Phone Reset" -SubmitText "RESET" -Content {

    New-UDInputField -Type 'textbox' -Name 'Extension' 

}-Endpoint {
param($Extension)
     
ResetIPPhone -Extension $Extension -PBX IP_of_PBX
Show-UDToast -Message "Phone $Extension rebooting..."

}

}

$EI = New-UDEndpointInitialization -Function @(‘ResetIPPhone’) -Variable @(‘Credential_PBX’)
$SRTDashboard = New-UDDashboard -Title “Reset IPPhone” -Pages $ResetPBX -EndpointInitialization $EI
Start-UDDashboard -Port 1000 -Dashboard $SRTDashboard

To be clear, I use this approach on other functions such as pass encrypted credentials for HPE iLO servers reset, boot, etc.

I also use this way to interact with Azure/Office 365 with no problems.

Hi guys! I got it working. I had to remove the module “SSHSessions” and Install “Posh-SSH”. That module was the problem. After changing module, tweaking the function with parameters for the new Module, it worked. Thanks to all.

3 Likes

Awesome @abarrozo
Can confirm, the PoSH-SSH module works flawlessly on UD. I’ve had it running for the past 6 months i believe.

1 Like

Were you able to get Output from Posh-SSH and put it in a UD-Grid?

Hi @abarrozo

With some formatting, yes.
I did some dirty code though, and piped the results into a .txt file, which both acts as a log and then have the grid just “get-content -last 20” with some formatting to separate the textstring into “timestamp”, “direction”, " value" to log both sent commands and reccieved commands. Note: the reading is sketchy, as i found no better way than just “start-sleep” to await a response.

Hit me up if you need any examples!

LOL, You were right. So dirty.

New-UDPage -Name “PBX” -Icon tty -Endpoint {

New-UDCard -Title "PBX" -Content {

    New-UDRow -Columns {

        New-UDColumn -Size 2 {

            New-UDIcon -Icon tty -Size 3x

        }

    }

 }


New-UDCard -Title "PBX" -Content {

    New-UDButton -Text "Test" -OnClick {
        $PBX = "IP_OF_PBX"
        $SessionID = New-SshSession -ComputerName $PBX -Credential $Credential_PBX -AcceptKey:$true | Select-Object -ExpandProperty SessionID
        $command = "asterisk -rx 'sip show peers'"
        $variable = $(Invoke-SSHCommand -Index $SessionID -Command $command).output
        $variable = $variable -match "^[^#]"
        $newvar = $variable | ConvertFrom-String -PropertyNames Name/Username, Host, Dyn, Forcerport, Comedia, ACL, Port, Status | Select-Object -Skip 1 Name/Username, Host, Dyn, Forcerport, Comedia, ACL, Port, Status
        Remove-SSHSession -Index $SessionID
    
        Show-UDModal -Header {New-UDHeading -Size 4 -Text "PBX"} -Content {       

            New-UDGrid -Title " " -Headers @("Interno","Host","Status") -Properties @("Name/Username","Host","Status") -Endpoint {

                $newvar | Out-UDGridData
            
            }
            

        }

    }

}

}

I don’t know what “$variable = $variable -match “^[^#]”” did, but ok for me.

1 Like

for you problem with the cache i use this method

to set a variable in the cache

Set-Item -Path "Cache:ContentToDisplay" -Value "showExample"

to get variable value

Get-Item "Cache:ContentToDisplay"
1 Like