Updating variable within a process step

doorman's Avatar

doorman

08 Jan, 2018 09:57 AM

I am using the client SDK in c# to create hundreds of project resource based on a template (which is also a project resource). I am cloning the template. For some of the steps in the template I need to update variables for each process resource accordingly.

I want to automate the whole process of creating a process template. Is there a way to update a variable within a process step using the client SDK?

  1. Support Staff 1 Posted by John Simons on 09 Jan, 2018 01:46 AM

    John Simons's Avatar

    Hi,

    If you want to update the variables of a project, we have https://github.com/OctopusDeploy/OctopusDeploy-Api/blob/master/Octo... script that does that.

    Would this help?

    Cheers
    John

  2. 2 Posted by doorman on 09 Jan, 2018 07:32 AM

    doorman's Avatar

    Hi John,

    Are there plans to support this in the C# sdk?

    Thanks for your help!

  3. Support Staff 3 Posted by John Simons on 09 Jan, 2018 10:42 PM

    John Simons's Avatar

    Hi,

    Anything you can do in Powershell, can be done in C#, have a look at the scripts in https://github.com/OctopusDeploy/OctopusDeploy-Api/tree/master/Octo...

    Also, one way to figure out what needs to be called is to drive the ui with the dev tools network tab opened and see what requests are being made.

    Hope this helps

    Cheers
    John

  4. 4 Posted by doorman on 10 Jan, 2018 08:28 PM

    doorman's Avatar

    Hi John,

    Thanks for the info. I ended up using the Powershell script. But it took me some time to figure out how to call it from C# :) . I realized I had to remove the Process {} wrapper from the script you provided.

    Here is the code In case some else needs to do this it will safe some time.

    Including
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    the dll is located under
    C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0

    using (var runspace = RunspaceFactory.CreateRunspace())
    {
        try
        {
            var script = File.ReadAllText("MyScript.ps1");
            runspace.Open();
            var ps = PowerShell.Create();
            ps.Runspace = runspace;
            ps.AddScript(script);
            ps.Invoke();
            ps.AddCommand("MyFunction").AddParameters(new Dictionary<string, string>
            {
                { "Param1" , value1},
                { "Param2", value2},
                { "Param3", value3},
            });

            foreach (var result in ps.Invoke())
            {
            }
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
    Important, I also had to remove the process wrapper attribute from the script file

    ****MyScripts.ps1

        function MyFunction
        {
            Param (
                [Parameter(Mandatory=$true)][string] myParam1,
                [Parameter(Mandatory=$true)][string] myParam2,
                [Parameter(Mandatory=$true)][string] myParam3,
            )
            // Removing the Process wrapper solved the problem, the code didn't run with it
            Process {
                //DOSTUFF
            }
        }

  5. Support Staff 5 Posted by John Simons on 10 Jan, 2018 10:45 PM

    John Simons's Avatar

    I am happy that you worked it out.
    And thanks for the example.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac