none
Need to Replace non alphanumeric characters with 'n' or or leave if alphabetic with Powershell RRS feed

  • Question

  • Goal:  Utilizing Orchestrator verify the middle initial input into SCSM is an alphabetic character, if not switch to 'n'.

    I have this script working in PS:

    $MI = "b"
    IF ($MI -notlike '[^a-zA-Z]') {$nsinitials = "n"} else {$nsinitials = $MI}

    The issue I am running into is that I get the following error in Orch when the script runs:

    The term 'b' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

    I also get this error in PS without quotes around the input variable. I have attempted several options for including the quotes inside the brackets but can not figure out how to get around this error.  

    Please help.  Thanks,

    Chad

    Thursday, July 6, 2017 4:09 PM

Answers

  • This is the command that does not work:

    $MI = b
    IF ("$MI" -match '[^a-zA-Z]') {$nsinitials = "n"} else {$nsinitials = $MI}

    This is how the variable is coming in from the initialized data in Orchestrator.

    But I figure out that the following command gives me the desired results:  

    $nsinitials = "$MI" -replace "[^a-zA-Z]","n"

    • Marked as answer by ChadM74 Friday, July 7, 2017 1:01 AM
    Friday, July 7, 2017 1:01 AM

All replies

  • There is nothing in the code that you posted that would generate that error, so it would seem that your question is missing one or more rather pertinent pieces of information.

    -- Bill Stewart [Bill_Stewart]

    Thursday, July 6, 2017 4:23 PM
    Moderator
  • "-notlike" will not work with a RegEx pattern.  Use "-match" or "-notmatch".

    Address Bill's issue first.


    \_(ツ)_/

    Thursday, July 6, 2017 10:10 PM
  • This is the command that does not work:

    $MI = b
    IF ("$MI" -match '[^a-zA-Z]') {$nsinitials = "n"} else {$nsinitials = $MI}

    This is how the variable is coming in from the initialized data in Orchestrator.

    But I figure out that the following command gives me the desired results:  

    $nsinitials = "$MI" -replace "[^a-zA-Z]","n"

    • Marked as answer by ChadM74 Friday, July 7, 2017 1:01 AM
    Friday, July 7, 2017 1:01 AM
  • There's no way we could have figured that out from what you posted, because your post had double quotes (") around the letter b. Also you don't need the double quotes around $MI. But in any case it sounds like you have a solution so that's good.

    -- Bill Stewart [Bill_Stewart]

    Friday, July 7, 2017 1:56 PM
    Moderator