Noob question on function calls

I just converted a script from VBScript and wrote the function calls to look the same. The problem is the functions don't work as expected.

Here is my function to delete some characters I don't want from a string, and return the string back to me. I'm sure there is some very simple issue with the variable / object references that is the reason why the input string gets passed back unaltered, even if it contains spaces or single quote marks.

This function is part of a section of code which converts people's names into logon names, by removing spaces and illegal characters from the names.

function CleanString($InputStr)
{
    $DirtyChars = "?", "/", "\", ":", "*", '", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+", "_", ".", `', " "
    foreach ($char in $DirtyChars)
    {
        $InputStr = $InputStr.Replace($char,"")
    }
    return $InputStr
}


February 17th, 2015 2:49am

Hi there,

It look like there's a problem with your $DirtyChars variable:
DirtyChars = "?", "/", "\", ":", "*", '", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+", "_", ".", `', " "


Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 3:50am

Hi there,

It look like there's a problem with your $DirtyChars variable:
DirtyChars = "?", "/", "\", ":", "*", '", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+", "_", ".", `', " "


February 17th, 2015 3:51am

Linear conversion is usually not ver good and creates poor scripts:

Try this:

'my str\i/n?g wi;th? bad :cha*racters' -replace '[?|\|/*\\:;<>{}+-=]',''

Even in VBScript this would have been a better method.

Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 6:16am

Hi there,

It look like there's a problem with your $DirtyChars variable:
DirtyChars = "?", "/", "\", ":", "*", '", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+", "_", ".", `', " "


February 17th, 2015 11:47am

Hi there,

It look like there's a problem with your $DirtyChars variable:
DirtyChars = "?", "/", "\", ":", "*", '", "<", ">", ",", "&", "#", "~", "%", "{", "}", "+", "_", ".", `', " "


Free Windows Admin Tool Kit Click here and download it now
February 17th, 2015 11:47am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics