Start a new topic
Solved

Creating script Parameters in Powershell connector using dynamic folders

I am trying to create a powershell connector with a dynamic folder.

I already have the code available to retrieve credentials dynamically.

I have also successfully created the connector using a raw jSON connector.


I need to create 2 script paramters but the documentation is not clear on the syntax and I could not find an example in the repo.


I know the snippet below works when I created it in my raw jSON format.

I manually created the Parameters in the second tab and mapped those 2 parameters to the $EffectiveUsername$ and $EffectivePassword$ tokens.

And then I went to the Test tab and tested successfully.


"Script": "#Get RoyalTS Creds

param (

 [string] $userdn,

 [string] $userpw

 )


$user = $uid

$userpass = $userpw

.....


However when I referred to the online documentation

https://docs.royalapps.com/r2021/scripting/objects/connections/royalpowershellconnection.html#parameters


Parameters

Type: string, Default Value: Empty String
The script parameters to pass on to the script.


It does not provide any sample syntax. Most of the parameters in the doco are clear, for example:

"Script" : "# powershell code......."

It is only the parameter and a value.

And I have figured out how to format extended Properties into a block.


However, the Parameters are a series of custom_parameter_name and parameter_Value grouped in the Parameters section of the Powershell-> Script block. I have tried multiple varieties of syntax of formatting but without a reference, it is impossible to script this key part of my script.


Thank you


Did you find a solution to your problem?  I have the same issue.

Hi!


you can specify the script parameters using a simple XML encoded string. Here's an example:


Line 4 to 18 in the screenshot above shows the XML which is expected by Royal TS/X. 

Line 1 shows the actual property value as XML encoded string.


You can create the following XML:

<?xml version="1.0" encoding="utf-16">
<ParamConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ParamConfigItems>
        <ParamConfigItem>
            <ParameterName>Param1</ParameterName>
            <Value>$EffectiveUsername$</Value>
            <TestValue />
        </ParamConfigItem> 
        <ParamConfigItem>
            <ParameterName>Param2</ParameterName>
            <Value>Some Value</Value>
            <TestValue />
        </ParamConfigItem>
    </ParamConfigItems>
</ParamConfig>


Set this XML as XML encoded string to the Parameters property as described in the Advanced Scenarios topic:

https://docs.royalapps.com/r2021/scripting/rjson/advanced-scenarios.html


I hope this helps. I will also include the XML sample in the docs.


Regards,
Stefan


I will take a look. But I will also look forward to your samples to help provide guidance in how to incorporate into my scripts.


Thank you.

Nino 

Hi Nino,


the sample I included in the docs is the very same as the one above. Do you still need assistance to incorporate this into your script?


Regards,
Stefan

I am uploading the script I have been testing with. I feel I may completely missing the point. 

I created a password object and tried to assign it to the PowerShell connection object. 

And want to pass the credential into the powershell script by assigning it to $uid and $userpw. 

I could be way off.

rdfe

Or are you saying I can insert the XML block into the jSON code.? 

Hi Nino,


you first need to create the XML according to your requirements. Once you have that, you need to encode the XML block into a XML-encoded string (like the line 1 in the screenshot). This string must then be added to the Properties -> Parameters property in the rJson (see https://docs.royalapps.com/r2021/scripting/rjson/advanced-scenarios.html)


I'm not a PowerShell expert. I guess you can use the XML powershell cmdlets to create the paramters XML and also use a cmdlet to encode it into a string. In C# I would youse the XmlTextWriter.WriteString method but there's probably a better way to do it in PowerShell.


Let me know if this helps. I'm currently quite busy and don't have much time to provide more details. If you need further help with that, I'm happy to dig deeper and provide a more complete sample.


Regards,
Stefan





Thank you. 

I will see if i can make some headway on my own. I just want to make sure I understand the method.

If I am successful, I will pass the information back.

Yes, I think I may need guidance. I understand the XML code that is stored in the .rtsz file and how it related to the Parameters panel in the RoyalTS Properties screen. 

I also review the link you provided in the online doco which refers to the Advanced scenarios.


Currently, i first try to create the dynamic folder with just jSON format before I create my powershell script, so I can use that to model my script output.


However, I assume I would add something like the following jSON into my connection object.

  "Properties": {

    "Param1": "$EffectiveUsername$",

    "Param2": "Some Value"

   }


But that does not seem to work.

I am assuming the result would be Param1 and Param2 being visible in the Parameters panel.


 I am trying to some variations, but I feel I must be missing something in translating the custom Powershell parameters into the jSON

Thank you for your earlier responses, but either this functionality does not work or it is not clear what the jSON syntax and object names should be are documented.


I have now given up on on this because it does not appear to actually work.


I have found an entry in the online doco at: 

https://docs.royalapps.com/r2021/scripting/objects/connections/royalpowershellconnection.html#parameters

But it appears to stop short of details or even an example.


Parameters

Type: string, Default Value: Empty String
The script parameters to pass on to the script.

I have had tremendous success with creating JSON objects for credentials and connections, as well as applying Properties with additional parameters values.

However, I am now guessing at what to do after Parameters. I am able to construct a variety of JSON blocks without errors, (including your example) but no success in the resulting connection object.

As I said earlier, if this feature does work, there are no Powershell examples in the samples folders to clarify this use case. 

Below is my best guess at what the Properties should look like if I was to assign multiple parameter values, but they do not result in being defined in the generated connections.

  "Properties": {          

  "Parameters": [

  { "Parametername": "Param1", "Value": "Value1" }, 

  { "Parametername": "Param2", "Value": "Value2"}

  ]

 }

Above does not work.

Hi Nino,


I think I can see the confusion ;)


In my previous post, I showed you how to "build" the XML for each parameter. The XML must be inserted as "XML encoded string" into the json. Here's the example I used:

"Properties": {
    "Parameters": "&lt;?xml version=\"1.0\" encoding=\"utf-16\"?&gt;&lt;ParamConfig xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\"&gt;&lt;ParamConfigItems&gt;&lt;ParamConfigItem&gt;&lt;ParameterName&gt;Param1&lt;\/ParameterName&gt;&lt;Value&gt;$EffectiveUsername$&lt;\/Value&gt;&lt;TestValue \/&gt;&lt;\/ParamConfigItem&gt;&lt;ParamConfigItem&gt;&lt;ParameterName&gt;Param2&lt;\/ParameterName&gt;&lt;Value&gt;Bla&lt;\/Value&gt;&lt;TestValue \/&gt;&lt;\/ParamConfigItem&gt;&lt;\/ParamConfigItems&gt;&lt;\/ParamConfig&gt;"
}


Does this help?


Regards,
Stefan

Thank you for the clarification. However, I had also tried that format as well without success.

I  had add backslash to escape the double quotes as well as the forward slash. Although the JSON Verify button reported success, when I reloaded and opened the generated Powershell connector object, the Script section would error with a "Royal TS has encountered a problem" Error Report.

Even through the JSON is verified, the XML still does not seem to be be interpreting the escape code  as expected.

I even created a object manually, saved it and copied the XML from that directly and inserted the backslashes like your example. 


And I did try you example directly assuming that it should at least result in the parameters showing up in the generated object. Which would at least have given me a working model.. 



Hi again,


sorry this took so long. I was quite busy but I had some time today to take a closer look. Here's a quick summary what I found:

1. I was mistaken that the parameters XML must be an XML encoded string. It actually needs to be an escaped string for json.

2. The same applies to the script. New lines, etc. are not allowed. It must be escaped for json usage.


Here's a working example I just tested:

{
    "Objects": [
        {
            "Type": "Credential",
            "Name": "DEVuser",
            "Username": "User123",
            "Password": "mysecretpassword",
            "ID": "Dev1"
        },
        {
            "Type": "PowerShellConnection",
            "Name": "Powershell-Dev",
            "Computername": "localhost",
            "CredentialMode": 3,
            "CredentialID": "Dev1",
            "Description": "Dev connection",
            "Properties": {
                "DefaultOutput": 1,
                "CredentialAutologon": false,
                "Parameters": "<ParamConfig xmlns:xsi=\"http:\/\/www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org\/2001\/XMLSchema\">\n                    <ParamConfigItems>\n                        <ParamConfigItem>\n                            <ParameterName>Param1</ParameterName>\n                            <Value>$$EffectiveUsername$$</Value>\n                            <TestValue />\n                       </ParamConfigItem> \n                        <ParamConfigItem>\n                            <ParameterName>Param2</ParameterName>\n                            <Value>Some Value</Value>\n                            <TestValue />\n                        </ParamConfigItem>\n                    </ParamConfigItems>\n                </ParamConfig>"
            },
            "Script": "#Get RoyalTS Creds\nparam ([string] $uid,\n            [string] $userpw,\n            [string] $Param3)\n      Set-PSDebug -trace 1\nwrite-host $uid\nwrite-host $userpw\nWrite-host $Param3\nWrite-host $Param4\nwrite-host 'CredUser Name' $$CredentialUsername$$ \nwrite-host 'password' $$CredentialPassword$$\nwrite-host 'Effective user' $$Effectiveusername$$\n"
        }
    ]
}


Let me know if this works for you.


Regards,
Stefan


A quick tip: if you are using a code editor like VS Code you can easily spot issues with malformed json as it constantly validates your json as you type. There are also a lot of extensions which may be helpful, like this one: https://marketplace.visualstudio.com/items?itemName=joshuapoehls.json-escaper

Login or Signup to post a comment