• Brian Denzer (Customer)

    There's a problem with this script.

     

    This block:

    1. $userid = $item.Username
    2.  
    3. # users REST endpoint
    4. $url = "https://$hostname/api/v1/users/$userid"

    Should instead reference the user id, not the user name:

    1. $userid = $item.id
    2.  
    3. # users REST endpoint
    4. $url = "https://$hostname/api/v1/users/$userid"

    When attempting to use the script as is, the (lazy) API error handler returns a misleading error message:

    1. The requested resource with API version '1' does not support HTTP method 'DELETE'.

    The end result is that -- to delete users with the API, using a CSV, the approach would have to be loading a CSV containing user id values and deleting those user id values. For most practical purposes, the MOVEit Transfer user id will not be a value that any organization stores, so a more realistic use case would be loading a list of user names from a CSV file, maybe storing that in an array or hash, then using the API to obtain a list of users (user names and user id's), then looping through that list of users, comparing the user name to the array or hash, and if a match is found, using the user id in the API call to delete the user account.

     

    Something like this:

    1. #Obtain a list of users:
    2. $url = "https://$hostname/api/v1/users"
    3.  
    4. $response = Invoke-WebRequest -Uri $url -Method get -UserAgent $useragent -Headers $headers
    5.  
    6. $response
    7. $responseObject = $response | ConvertFrom-Json
    8.  
    9. ForEach($r in $responseObject.items)
    10. {
    11.  
    12. $username = $r.username
    13. $id = $r.id
    14. Write-Host $username, $id
    15.  
    16. #Next: Match the user name to, for example, a list of users from a CSV or database, and do something (e.g., delete the user)
    17.  
    18. }