Passing parameters to pwoershell script

Completed

I'm trying to pass parameters to a powershell script under windows and passing a path to the script (set to run as "source code"). I tried a lot of variations to get the path into the script, but everything I tried gave me a path of "null".

It looks like I should define an environment variable (which I did, called "fileURI"), then access it in the source code script as "%fileURI%" - but that doesn't work (null path):

$contents = get-content %fileURI% -Raw
[16:08:20][Step 3/7] + ~~~~~
[16:08:20][Step 3/7] + CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
[16:08:20][Step 3/7] + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentC
[16:08:20][Step 3/7] ommand
5 comments
Comment actions Permalink

Hello!

I have just done a quick test with PS script and configuration parameter to show a minimal setup example:

(Just a quick note here - if you aim to use environment parameter, those are passed as an environment of a spawned process. For PS that would mean you could access env.fileURI like this:
Get-Variable fileURI -valueOnly)

Here`s my build step code:

And build log:
As you can see, variable has been successfully retrieved from the script context and Get-Content received non-null parameter. 

May I ask you to provide me with the screenshot of parameter configuration?

0
Comment actions Permalink

Thanks Fedor. The configuration parameter works fine as you suggested, but I couldn't get the environment parameter example you gave above to work - same error - the value retrieved from the variable is null/empty.

 

I must have looked at 10 - 20 examples of how to pass the parameter and none of them looked like yours (which worked) - so thanks for pointing me to the correct solution. Is there a preferred way to pass parameters - configuration or environment (assuming I can get it working as an environment param).

I define it as an environment parameter as:

and using it by:

$file=%fileURI%

 

which is always null/empty

Richard

0
Comment actions Permalink

Hello Richard,

Glad it worked for you! In regards to the environment variables; as I`ve mentioned, those are passed into the environment of the subprocess. This means that you can access them just as you would access the PATH variable, for example:

Get-Variable PATH -valueOnly

or

$env:PATH

A quick example:


And the variable has been accessed properly:


In regards to what type of parameter is preferred - it`s more about your preference, really. There are tools which have no other option but environment parameters (which they do read internally) - this is not the case with PS, so you are free to decide. The configuration parameters seem to be easier though as there`s no need to explicitly retrieve the value via $env:* or Get-Variable. 

Please let me know if there`s anything else I could help with.

0
Comment actions Permalink

Thanks again Fedor. That's helped me to solve one problem: Configuration params need double-quotes around the path. Environment params fail with an "illegal character" error if the path is wrapped in double-quotes (when it comes to be used to open a file).

That's probably the reason I couldn't get it to work correctly yesterday. Thanks very much for clearing it up and providing a solution

Richard

0
Comment actions Permalink

Richard,

Good to hear that this was helpful! I`m sorry about not checking the Get-Content for the run with environment variables (and double-quotes string).

0

Please sign in to leave a comment.