Not able to create new role using powershell

(Prathamesh Wagh) #1

The below script is giving me error mentioned at the end

Add-Type -Path ‘C:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll’
$env:OctopusURL = “http://localhost
$env:OctopusAPIKey = “API-KEY”

$endpoint = new-object Octopus.Client.OctopusServerEndpoint $env:OctopusURL,$env:OctopusAPIKey

$repository = new-object Octopus.Client.OctopusRepository $endpoint

$NewUSerRole = Get-OctopusResourceModel -Resource UserRole

$NewUSerRole.Name = “UserRole Name”

New-OctopusResource $NewUSerRole

Error:

New-OctopusResource : Octopus Server returned an error: Value cannot be null.
Parameter name: source
Server exception:
Value cannot be null.
Parameter name: source
System.ArgumentNullException

(Dalmiro Grañas) #3

Hi,

Thanks for reaching out!

I noticed that your script is using Get-OctopusResourceModel which is a cmdlet that belongs to the Octoposh module. That module hasn’t been updated since 2017 and its highly incompatible with current versions of the Octopus Server. I can 100% confirm this because I was the creator and sole maintainer of that project :slight_smile:

I strongly recommend you to just rely on Octopus.client for all your automation needs and stop using Octoposh

The error you are getting is most likely happening because of 2 possible reasons:

  1. Octoposh is giving you an old shape of the UserRoleResource object.

  2. You are not populating all the properties needed on the UserRoleResource.

To work around that you’ll need to:

  1. Modify the line that’s calling Get-OctopusResourceModel with: $NewUserRole = New-Object Octopus.Client.Model.UserRoleResource

  2. Just like you are doing populating $NewUserRole.Name, populate the rest of the mandatory properties.

  3. Finally, modify the line calling New-OctopusResource with $repository.UserRoles.Create($NewUserRole). If your $NewUserRole object is lacking mandatory properties, then the API will return an error telling you which ones.

Hope that helps,
Dalmiro

(Prathamesh Wagh) #4

Thank Dalmiro for valuable feedback. I am stuck in the id property for NewUSerRole. Not sure how the value can be the fetched from the OctopusDeploy & how can that be incremented.

for EX - Current custom user role has id = UserRole-1 and the next created UserRole should have UserRole-2, as the ID gets created/assigned at runtime(that’s what I believe).

In short, I need to find the max of UserRole id and increment it while creating new UserRole.

(Prathamesh Wagh) #5

New-OctopusResource : Octopus Server returned an error: Value cannot be null.
Parameter name: source
Server exception:
Value cannot be null.
Parameter name: source
System.ArgumentNullException

What value does the “source” parameter takes?

(Prathamesh Wagh) #6

Error is resolved, thank you for the help.

(Dalmiro Grañas) #7

Glad to hear you sorted it out :slight_smile:

For anyone else that runs into this issue: You don’t need to populate the Id property yourself. Leave it null and once you save it to the database successfully, octopus will return back the object with the Id populated.