Trying to use Powershell to List all users with roles scripts and erroring out

Hi,

Iam trying to Execute Powershell for List Users with Roles from this link “https://octopus.com/docs/octopus-rest-api/examples/list-users-with-role”, and it errors out with “An error occurred: The remote server returned an error: (404) Not Found.”
My Octopus Version is 2018.6.2

As suggested i have left SpaceName blank as its old version. But still it fails.

is there any other script which runs with this version of OD?

–Manojreddy

Any Update on this request?

Hi Manoj,

I’m sorry for the delay in getting back to you.

You are right, there are some API endpoint changes that needed to be made within the script in order to have it successfully run on 2018.6.2. I’ve made the necessary changes to get it working and returning the information you are after. I’ve also removed all references to Spaces (as Spaces are introduced in a later version of Octopus Deploy). This should make it easier to work with.

If you want to expand on the script, you can view the api endpoints available on your Octopus Deploy server by navigating to http:///swaggerui/index.html

The script below will return the Teams, Roles and Usernames allocated to those Teams.

# Define working variables
$octopusBaseURL = "http://<servername>/api"
$octopusAPIKey = "<valid API key>"
$headers = @{ "X-Octopus-ApiKey" = $octopusAPIKey }


try
{

    # Get reference to role
    $role = (Invoke-RestMethod -Method Get -Uri "$octopusBaseURL/userroles/all" -Headers $headers -ErrorVariable octoError) | Where-Object {$_.Name -eq $roleName}

    # Get list of teams
    $teams = (Invoke-RestMethod -Method Get -Uri "$octopusBaseURL/teams/all" -Headers $headers -ErrorVariable octoError)

    # Loop through teams
    foreach ($team in $teams)
    {
   Write-Host  "Team:`t"$team.Name 
   Write-Host "Roles:"
   if (!$team.UserRoleIds){
    Write-Host "`t`t<null>"
   }
   foreach ($urid in $team.UserRoleIds){
        write-host "`t`t"$urid
   }
                # Loop through members
                Write-Host "Users:"
                foreach ($userId in $team.MemberUserIds)
                {
                    # Get user object
                    $user = Invoke-RestMethod -Method Get -Uri ("$octopusBaseURL/users/$userId") -Headers $headers -ErrorVariable octoError
                    
                    # Display user
                    Write-Output "`t`t$($user.DisplayName)"
                }
                Write-Host "`n"
                # Check for external security groups
                if (($null -ne $team.ExternalSecurityGroups) -and ($team.ExternalSecurityGroups.Count -gt 0))
                {
                    # External groups
                    Write-Output "External security groups:"

                    # Loop through groups
                    foreach ($group in $team.ExternalSecurityGroups)
                    {
                        # Display group
                        Write-Output "$($group.Id)"
                    }
                }
            }
          }

catch
{
    if ([string]::IsNullOrEmpty($octoError))
    {
        Write-Output "There was an error during the request: $($octoError.Message)"
    }
    else
    {
        Write-Output "An error occurred: $($_.Exception.Message)"
    }
}

I really hope this helps. Let me know how you go, and feel free to reach out again if you need some further guidance.

Regards,

Dane

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.