Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在命令输出中正则化到目前为止写入的字符串?_Regex_Powershell - Fatal编程技术网

Regex 如何在命令输出中正则化到目前为止写入的字符串?

Regex 如何在命令输出中正则化到目前为止写入的字符串?,regex,powershell,Regex,Powershell,在相同的PowerShell脚本中,通过正则表达式找到ExitCode=1或更大值时,我将设置一个终止步骤 我将在jenkins作业中上传此内容,其中运行的其他第三方软件返回的Lastexitcode Powershell无法识别,因此我需要一个带有regex的函数来查找到目前为止编写的内容,并在找到后终止 我试过: [regex]::new('ExitCode=[1-9]*[0-9]') 我希望在运行该函数时,它会在整个脚本中找到所指示的正则表达式并终止该步骤,但我进入了第二步,错误在于它在

相同的PowerShell脚本中,通过正则表达式找到ExitCode=1或更大值时,我将设置一个终止步骤

我将在jenkins作业中上传此内容,其中运行的其他第三方软件返回的Lastexitcode Powershell无法识别,因此我需要一个带有regex的函数来查找到目前为止编写的内容,并在找到后终止

我试过:

[regex]::new('ExitCode=[1-9]*[0-9]')

我希望在运行该函数时,它会在整个脚本中找到所指示的正则表达式并终止该步骤,但我进入了第二步,错误在于它在第一步没有找到任何内容。

我认为变量
$env:termining
始终是空的。以下是我的作品:

function script:ThrowErrors {
  if ([regex]::new('ExitCode=[1-9]*[0-9]'))
  {
    'erorrrrr'
  }
  if ($env:Terminating -eq 1) {
    throw 'This error is terminating the step.'
  }
}

robocopy.exe ? | Out-Null
$env:Terminating = $LASTEXITCODE
$env:Terminating
ThrowErrors

PS C:\> ThrowErrors
erorrrrr

您创建了一个regex对象,但从未对其执行任何操作。正则表达式不会神奇地应用自身,特别是它不会将自身追溯应用于在对象创建之前出现和消失的输出。请提供更多关于您获得的输出以及输出来自何处的信息。嗨@AnsgarWiechers,关于您提到的内容,我几乎一无所知。试着解释一下:我有一份jenkins的工作,它运行多个powershell脚本。1.ps1、2.ps1、3.ps1等,在某个点上,其中一个脚本正在运行ThrowError功能。我需要帮助,以便该函数查找到目前为止由其他脚本(在Jenkins控制台输出中)编写的内容并终止。我需要以某种方式使用正则表达式来查找到目前为止在.ps1中编写的内容中ExitCode=1或更大的值。正如我所说,您不能对以前编写的输出应用正则表达式。如果你有一个日志文件中的输出,你可以解析这个日志文件。好的,我理解,有没有可能把所有写在日志文件中并检查regex(错误代码),然后在相同的.ps1中继续脚本?谢谢@Patrick的回复。但这不是我要找的,我需要它检查是否写在ErrorCode=1或更大的地方,而不是检查$Lasexitcode并在该行终止<代码>函数脚本:抛出者错误{if([regex]::new('ExitCode=[1-9]*[0-9]'){'Error''请停在这里'}if($env:termining-eq 1){throw'termining'}}robocopy.exe?|Out Null$env:Terminating=$LASTEXITCODE$env:Terminating'ExitCode=0'Throwers'ExitCode=1'Throwers'Nah I continue'如果运行此命令,输出将如下所示
16 ExitCode=1 ExitCode=0错误停止此处请Nah I continue
使用
[regex]::new('ExitCode=[1-9]*[0-9])
,因为条件是无意义的,因为(a)它不针对任何输入进行测试,(b)它总是正确的。这就是我想要的,谢谢大家提供的帮助。你的答案与你的问题没有明显的联系(未来的读者不必通过冗长的评论交流来收集更多信息)。
Step.log
来自哪里?哪里定义了
$终止
?您的意思是说,
$env:终止
?顺便说一句:您可以使用
-file
选项直接使
开关
迭代文件中的所有行-请参阅
function script:ThrowErrors {
  if ([regex]::new('ExitCode=[1-9]*[0-9]'))
  {
    'erorrrrr'
  }
  if ($env:Terminating -eq 1) {
    throw 'This error is terminating the step.'
  }
}

robocopy.exe ? | Out-Null
$env:Terminating = $LASTEXITCODE
$env:Terminating
ThrowErrors

PS C:\> ThrowErrors
erorrrrr

# Global enviroment variable
$env:Terminating = '0'


#Checks for ErrorCode value that has more than 0
#Checks for Terminating value


function script:CheckErrors 
{
  Get-Content -Path Step.log | ForEach-Object -Process {
    $CurrentErrorCode = $_

    switch -regex ($CurrentErrorCode) 
    {
      'ExitCode=[1-9][0-9]?' 
      {
        'Found a high error code'
        if ($Terminating -eq 1) 
        {
          'Terminating is enabled.'
          throw 'Terminated'
        }
        else
        {
          'Terminating is disabled.'
        }
      }
      'ExitCode=0'                
      {

      }
    }
  }
}


# All bellow steps can have parts of build scripts.
# Fake step with no errors.
function script:Step1 
{
  'Running Step1'
  "This step hasn't any errors"
  '1/1'
  1/1
  'ExitCode=0'
}

# Another step that has an error but it won't be terminating.
function script:Step2 
{
  'Running Step2'
  'This step has a non terminating error'
  '1/0'
  1/0
  'ExitCode=1'
}

# Another step that has an error but this time is terminating.
function script:Step3 
{
  'Running Step3'
  'This step has a terminating error'
  '1/0'
  1/0
  'ExitCode=10'
}

# This step is in case of the terminating step is bugged.
function script:Step4 
{
  'Running Step4'
  'End of the process.' 
}


# The actual steps.
# Jenkins job will look like this.

#Set-Location "D:\Build_scripts"
#.\NewErrorHandling.ps1


''
Step1 | Tee-Object -FilePath Step.log
$Terminating = '0'
CheckErrors
''
''
Step2 | Tee-Object -FilePath Step.log
$Terminating = '0'
CheckErrors
''
''
Step3 | Tee-Object -FilePath Step.log
$Terminating = '1'
CheckErrors
''
''
Step4 | Tee-Object -FilePath Step.log
$Terminating = '0'
CheckErrors
''
''

# Cleaner if you use ISE.
$Error.Clear()```