Export function

后端 未结 2 1320
花落未央
花落未央 2021-01-26 14:06

I am trying to create a function in which a user has to give a filename that can not containt an empty string. Besides that, the string can not contain a dot. When I run this fu

相关标签:
2条回答
  • 2021-01-26 14:37

    The -match operator checks against a regular expression, so this:

    $script:newLog -match ".*"
    

    is testing if the filename contains any charachter except newline (.) 0 or more times (*). This condition will always be true, thus creating an infinite loop.

    If you want to test for a literal dot, you must escape it:

    $script:newLog -match '\.'
    

    As for your other question, you're misunderstanding how logical and comparison operators work. $exportInvoke -eq "Y" -or "N" does not mean $exportInvoke -eq ("Y" -or "N"), i.e. variable equals either "Y" or "N". It means ($exportInvoke -eq "Y") -or ("N"). Since the expression "N" does not evaluate to zero, PowerShell interprets it as $true, so your condition becomes ($exportInvoke -eq "Y") -or $true, which is always true. You need to change the condition to this:

    $exportInvoke -eq "Y" -or $exportInvoke -eq "N"
    
    0 讨论(0)
  • 2021-01-26 14:45

    Use this to test your input:

    !($script:newLog.contains('.')) -and !([String]::IsNullOrEmpty($script:newLog)) -and !([String]::IsNullOrWhiteSpace($script:newLog))

    Your regular expression (-match ".*" is essentially matching on everything.

    0 讨论(0)
提交回复
热议问题