Search code examples
powershellmultilinestring

Hand over multiline string as script Paramter in Powershell


The setup

In a powershell script I have written I use a Multiline string like the following:

$MLINE_VARIABLE = "*file1
  - change1
  - change2
*file_2
  - change1
  - change2
*file-3
  - change1
  - change2"

Since this string is saved as a variable I wanted to give the script that variable as parameter:

param(
[string]$NORMAL_STRING,
[string]$MULTILINE_STRING
)

echo $NORMAL_STRING
echo $MULTILINE_STRING

run the script

I am fairly new to powershell but feel quite well with bash scripts. I tested this script with multiple inputs. here are the results:

powershell .\test.ps1 -NORMAL_STRING "This is a Normal String!" -MULTILINE_STRING $MLINE_VARIABLE

The first run resultet in following long error Message (Error Message was in german and I translated the first occurance of an error):

powershell : In line:2 char:4
In line:1 char:1
+ powershell .\small_test.ps1 -NORMAL_STRING "THIS IS A NORMAL STRING!" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (In line:2 char:4:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
+   - change1
+    ~
Missing expression after unary operator '-'.
In line:2 char:5
+   - change1
+     ~~~~~~~
Unexpected token "change1" in expression or statement.
In line:3 char:4
.
.
.
Not all analytical errors were reported. Correct the reported errors and try again.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

The Next test I didn't used the multiline string for a sanity check:

powershell .\test.ps1 -NORMAL_STRING "This is a Normal String!"

I realized that the script is working, but only showing the first 2 entries of the normal string:

>> This
>> is

I assume that somehow the NORMAL_STRING was set to "This" and the missing parameter MULTILINE_STRING was set to "is".

So to test if the scripts works as intended I changed the Parameter -NORMAL_STRING "This_is_a_normal_Parameter" and instead of using the $MULTILINE_STRING as parameter I set it inside the script.

What I need

How could I successful hand over this Multiline Parameter without messing up all the variables?


Solution

  • Without using parameter -File, what follows after powershell.exe is regarded as if it were a -Command, which executes the specified commands (and any parameters) as though they were typed at the PowerShell command prompt.

    In this case however, you want it to run another PowerShell script, stored in a file.
    When used with -File <PathToTheScript>, the script runs in the local scope ("dot-sourced"), so that the functions and variables that the script creates are available in the current session.
    All values typed after the File parameter are interpreted as the script file path and parameters passed to that script.

    This means that changing your code to

    powershell -File '.\test.ps1' -NORMAL_STRING "This is a Normal String!" -MULTILINE_STRING $MLINE_VARIABLE
    

    would work as intended