Special characters being passed as parameter

This topic contains 7 replies, has 3 voices, and was last updated by  Vern Anderson 4 years, 3 months ago.

  • Author
  • #13602

    Vern Anderson

    # Preface Windows is an after thought not a priority at my company

    I am writing a script that will take an input parameter into the function. The strings are a POSIX based timezone string that contains both open and close parenthesis and either the plus or the minus symbol for the timezone offset value. What is the best way to handle these types of strings coming from the pipeline or as a parameter. Here are some examples of the strings.

    Adak – HAST – (GMT/UTC -10)
    Adak – HAST – (GMT/UTC -10) Daylight Savings Time
    Anchorage – AKST – (GMT/UTC -9)
    Anchorage – AKST – (GMT/UTC -9) Daylight Savings Time
    Anguilla – AST – (GMT/UTC -4)
    Antigua – AST – (GMT/UTC -4)
    Araguaina – BRT – (GMT/UTC -3)
    Argentina/Buenos_Aires – ART – (GMT/UTC -3)
    Argentina/Catamarca – ART – (GMT/UTC -3)
    Argentina/Cordoba – ART – (GMT/UTC -3)
    Argentina/Jujuy – ART – (GMT/UTC -3)
    Argentina/La_Rioja – ART – (GMT/UTC -3)
    Argentina/Mendoza – ART – (GMT/UTC -3)
    Argentina/Rio_Gallegos – ART – (GMT/UTC -3)
    Argentina/Salta – ART – (GMT/UTC -3)
    Argentina/San_Juan – ART – (GMT/UTC -3)
    Argentina/San_Luis – WARST – (GMT/UTC -3)
    Argentina/San_Luis – WARST – (GMT/UTC -3) Daylight Savings Time

    Once the script receives this parameter string it will need to compare and match it to a table which ties it into the Microsoft equivalent Windows Server Time Zones. Of which there only seems to be 97.

  • #13607

    Vern Anderson

    Thank you David,

    However I won't have control of the quotes they will be passed as a parameter to my script from an RBA and Orchestration like system (Not Microsoft product).

    So I guess my question then is, Are parameters automagically treated as a quoted string or is there some way in my script that I will have to add quotes onto the variable that captured the string from my param block?


  • #13608

    Dave Wyatt

    The quotation marks are not part of the strings themselves; they're what indicates a string literal. Once you have a [string] variable, you can do whatever you want with it, and the contents don't matter, as far as PowerShell is concerned.

    When your other product calls the PowerShell script, it needs to make sure to quote the strings properly. This is expected anytime you call any command-line utility (though the quoting / escaping rules may be different.)

  • #13622

    Vern Anderson

    Yes sorry that's exactly what I was trying to say. I have no control over the "other product" it is going to be passing the parameter with spaces and parenthesis and + or minus symbols no matter what I do and I have no control over that fact. That's what I meant to say. But thanks for you help I guess only testing and practice will tell.


  • #13625

    Dave Wyatt

    Well, look at it this way. Calling any command-line utility (including PowerShell.exe, commands, scripts, etc) basically involves an "API" which is the correct syntax to use when calling that utility. The caller is responsible for getting the syntax right. There's nothing you can do in your script that will allow people to pass in a string that contains spaces, special characters, etc without being properly quoted.

  • #13965

    Vern Anderson

    I just wanted to followup on this in the off chance that it may help some one later.

    What I wound up doing was taking the string from the external process and using the DOS echo command to pipe it into a CSV. Above it I had a similar echo command to pipe in a header row into the CSV. Then I was able to import it as an object and from there I Split off the parenthesis and other special characters using both the "string".Split("(") method and the -split operator to get rid of some others.

    I learned a valuable lesson about the split method. If you say "sting".Split(DST) it will split any D and any S and any T not the entire block of characters as a collective.


  • #13604

    Dave Wyatt

    There's nothing in those strings that would cause a problem for PowerShell, so long as they're quoted.

    Some-Function -Parameter 'Argentina/San_Luis – WARST – (GMT/UTC -3) Daylight Savings Time'
    'Argentina/San_Luis – WARST – (GMT/UTC -3) Daylight Savings Time' | Some-Function
    # ... etc
  • #13624

    Tore Groneng


    Don't know if this will help, however say you create this function:

    Function test-string{
    write-host "This is foo: $foo"
    write-host ""
    write-host "All arguments:"

    Let's test it:

    PS c:\> test-string this is a string
    This is foo:this

    All arguments:

    PS c:\> test-string -foo "this is a string"
    This is foo:this is a string

    All arguments:

    This pattern is the same no matter if you are calling a script or a function (scriptfiles may also have a Param() section (place it at the top in a scriptfile).


You must be logged in to reply to this topic.