VMware Cloud Community
dalehassinger
Contributor
Contributor

PowerCli Script parameter to allow all characters. How To allow?

How do I format the script input to allow all characters when using parameters?

Example:

.\Script.ps1 -text 'Check all VM's to see if powered On' - will not work.

Because the parameter string has a single quote this will fail.  If I use a double quote this would work but what if the String also has a Double Quote?

.\Script.ps1 -text "Check all VM's to see if powered On" - will work

.\Script.ps1 -text "PowerCLI "Is Cool!" to do automation" - would not work

I do not want to use a double single quote within the parameter string.

This is probably something easy but I did not see any examples doing a Google search.

0 Kudos
4 Replies
LucD
Leadership
Leadership

Since you can't change PowerShell's quoting rules, the only option I can think of is a here-string.

function Test-Parameter{

  

    param(

        [string]$Text

    )


    $text

}


$test = @'

This is a text with ' and " quotes in there

'@


Test-Parameter -Text $test


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dalehassinger
Contributor
Contributor

My use case is I have a custom form in vRA 7.6 that has a text field for the user to enter information. I pass that text to a powershell script.  So the user can enter any text. The one thought I had was to inspect that text with an javascript Action within Orchestrator and if I see an single quote replace it with two single quotes before passing to Powershell script or use an regex and not allow single quotes. Smiley Happy

0 Kudos
LucD
Leadership
Leadership

If that string from the form is provided as a variable to passed to to the .ps1 file, it should act in the same way as the here-string.

Only when you try to type a literal string in a PS session, the PS quoting rules will apply.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
dalehassinger
Contributor
Contributor

Thanks for the feedback LucD!

0 Kudos