Management API calls from Endpoint

Product: PowerShell Universal
Version: 1.4.3

Is there a way to call the Management API, for an Endpoint API? I’m getting an “Internal Server Error 500” when attempting this.

Can you provide an example of what you are trying to do? Are you specifying an app token and computer name\url?

Here’s the code for the endpoint

   New-PSUEndpoint -Url '/Cycle-Dev-Site/:action/:site' -Method 'GET' -Authentication -Role 'ITSM_DEV' -Endpoint { 

    #region CONSTANTS
    
    
    $Action      = $Action
    
    $site        = $site
    
    #$Site        = 'DEV'
    
    #$id          = 9
    
    
    #endregion
    
    
    #region FUNCTIONS
    
    
    Function Stop-Dashboard {
        
        Param (
            
            [Parameter(Mandatory=$true, HelpMessage='ID of the site')]
            [int] $Id
        )
        
        Invoke-WebRequest -Method Delete -Uri $uri -Credential $Credential
    }


    Function Start-Dashboard {
        
        Param (
            
            [Parameter(Mandatory=$true, HelpMessage='ID of the site')]
            [int] $Id
        )

        $uri = '{0}/dashboard/{1}/status' -f $PSPUApiRoot,$Id
        
        Invoke-RestMethod -Method Put -Uri $uri -Credential $Credential
    }
    
    
    Function Get-DashboardId {
        
        Param (
            
            [Parameter(Mandatory=$true, HelpMessage='Name of the site')]
            [String] $Site
        )

        $uri = '{0}/dashboard' -f $PSPUApiRoot
        
        $dbs = Invoke-RestMethod -ContentType 'text/json' -Method get -Uri $uri -Credential $Credential
        
        $id = $dbs | Where-Object {$_.Name -eq $Site} | ForEach-Object {$_.Id}
        
        if ($id) {
        
            $id
        }
        Else {
        
            Write-Error -Message 'Site not found'
            return
        }
    }
    
    
    #endregion
    
    
    #region VARIABLES


    $id          = Get-DashboardId -Site $site
    
    $PSPUApiRoot = 'https://myserver.name.local/api/v1'
    
    $uri         = '{0}/dashboard/{1}/status' -f $PSPUApiRoot,$Id
    
    $rootDir     = 'C:\Universal\Dashboards\ITSM'
    
    $Credential  = (Invoke-CredentialManager -FilePath ('{0}\resources\credentials\adAdminCreds.auth' -f $rootDir) -ReturnCredObject)
    
    
    #endregion
    
    
    #region MAIN

    
    If ($Action -eq 'Start') {
    
        Start-Dashboard -Id $id 
    }
    ElseIf ($Action -eq 'Stop') {
    
        Stop-Dashboard -Id $id

    }
    Else {
        
        'Invalid action provided'
    }
    
    
    #endregion
    
}

Can you do me a favor and check the logs in %ProgramData%\PowerShellUniversal? It should provide a better error message than just 500.

Should be after your Credential definition? :slight_smile:

Nice catch. Still doesn’t work, even with $ID statically configured. Thanks though.

So I’ve upgraded to the latest stable version, and no longer receive the error 500, but still nothing happens.

2021-04-08T22:46:04.3037223-04:00 0HM7QV5UCU3CA:0000000D [INF] Request starting HTTP/1.1 GET http://server.local.com/Cycle-Dev-Site/start/DEV (ca22a1cb)
2021-04-08T22:46:04.3319670-04:00 0HM7QV5UCU3CA:0000000D [INF] Request finished in 28.2452ms 200 application/json (791a596a)

Alright. I had to give this a good play but I have it working. You need to call the signin URL and establish a web session before calling the other endpoints when using basic auth. I’ve created a Get-WebSession function that does this. You’ll just need to integrate that with your session store. What this is doing is logging in and then getting a cookie that it’s using with the other calls.

New-PSUEndpoint -Url '/Cycle-Dev-Site/:action/:site' -Method 'GET' -Authentication -Role 'ITSM_DEV' -Endpoint { 
    #region CONSTANTS
    $Action      = $Action
    $site        = $site
    #$Site        = 'DEV'
    #$id          = 9
    #endregion
    
    #region FUNCTIONS
    Function Stop-Dashboard {
        Param (
            [Parameter(Mandatory=$true, HelpMessage='ID of the site')]
            [int] $Id
        )

        $Session = Get-WebSesion 
        Invoke-WebRequest -Method Delete -Uri $uri -WebSession $Session
    }

    Function Start-Dashboard {
        Param (
            [Parameter(Mandatory=$true, HelpMessage='ID of the site')]
            [int] $Id
        )

        $uri = '{0}/dashboard/{1}/status' -f $PSPUApiRoot,$Id
        
        $Session = Get-WebSesion 
        Invoke-RestMethod -Method Put -Uri $uri -WebSession $Session
    }

    Function Get-WebSession {
        $uri = '{0}/signin' -f $PSPUApiRoot
        Invoke-RestMethod -Method POST -ContentType 'application/json' -Uri $uri -Body (@{ UserName = "admin"; Password = "password" } | ConvertTo-Json) -SessionVariable session | Out-Null
        $Session 
    }
    
    Function Get-DashboardId {    
        Param (
            
            [Parameter(Mandatory=$true, HelpMessage='Name of the site')]
            [String] $Site
        )
        
        $Session = Get-WebSession

        $uri = '{0}/dashboard' -f $PSPUApiRoot
        $dbs = Invoke-RestMethod -ContentType 'application/json' -Method get -Uri $uri -WebSession $session
        $id = $dbs | Where-Object {$_.Name -eq $Site} | ForEach-Object {$_.Id}
        if ($id) {
            $id
        }
        Else {
        
            Write-Error -Message 'Site not found'
            return
        }
    }
    
    #endregion
    
    #region VARIABLES

    $Password = ConvertTo-SecureString 'asdf' -AsPlainText -Force 
    $Credential  = [PSCredential]::new('admin', $Password)
    $PSPUApiRoot = 'http://localhost:5000/api/v1'
    $uri         = '{0}/dashboard/{1}/status' -f $PSPUApiRoot,$Id

    $id          = Get-DashboardId -Site $site    

    $rootDir     = 'C:\Universal\Dashboards\ITSM'

    #endregion
    
    
    #region MAIN

    
    If ($Action -eq 'Start') {
    
        Start-Dashboard -Id $id 
    }
    ElseIf ($Action -eq 'Stop') {
    
        Stop-Dashboard -Id $id

    }
    Else {
        
        'Invalid action provided'
    }
    
    
    #endregion
    
}