Skip to main content

Work with Powershell in upKeeper

Comments

1 comment

  • Peter Johansson

    You can add more information in to the header and get cleaner calls for methods. Thanks Markus Ullin for this input.

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

    # 1. Set up the connection to the API

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

     

    #Configure URL to upKeeper API

    $baseUrl = "https://upKeeperApiUrl"

    $apiUrl = "$baseUrl/api"

    $tokenUrl = "$baseUrl/token"

     

    # Get Credentials

    $myCreds = Get-Credential -Message "Enter API Username"

     

    # Create body for login

    $loginBody = @{

        UserName = $myCreds.UserName

        Password = $myCreds.GetNetworkCredential().Password

        grant_type = "password"

        client_id = "ngAuthApp"

    }

     

    # Acquire an API access token

    $token = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $loginBody -ContentType "application/json"

     

    # Header for subsequent REST API calls

    $header = @{Authorization="$($token.token_type) "+$token.access_token}

     

    # Save the user_organizations in the variable $allOrg

    $allOrg = $token.user_organizations | ConvertFrom-Json

     

    # Set standard-parameters for Invoke-RestMethod

    $PSDefaultParameterValues = @{

        "Invoke-RestMethod:Headers" = $header

        "Invoke-RestMethod:ContentType" = "application/json;charset=utf-8"

        "Invoke-RestMethod:Verbose" = $true

    }

     

    # Use Out-GridView to select the organization to work with

    $selectedOrg = $allOrg | Out-GridView -PassThru

    $orgNumber = $selectedOrg.Number

    $orgId = $selectedOrg.Id

     

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

    # 2. List computers, devices and applications

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

     

    # Get all computers in organization

    # GET api/{organizationNumber}/ComputerNames

    $allComputers = Invoke-RestMethod -Method Get -Uri "$apiURL/$orgNumber/ComputerNames"

     

    # Get all devices in organization

    # GET api/{organizationNumber}/Users

    $allDevices = Invoke-RestMethod -Method Get -Uri "$apiURL/$orgNumber/DeviceNames"

     

    # Get all applications in organization

    # GET api/{organizationNumber}/Applications

    $allUsers = Invoke-RestMethod -Method Get -Uri "$apiURL/$orgNumber/Applications"

     

    0

Please sign in to leave a comment.

Powered by Zendesk