提取子字符串

时间:2017-06-20 10:26:08

标签: powershell

我正在尝试获取描述中存在的正确错误消息。但是当我的调试器进入substring行时,它会毫无理由退出。

$job= submitting the job 2fdab2d5-f09c-4392-953e-8b85f90d76eb ... 
      client version: 10.2.2.0 
      target: cluster 
      stat: simulatelarge 
      skippath: true error submitting job.  
      vcclientexceptions.vcclientexception: [httpstatuscode = 0; description = e_csc_user_syntaxerror: syntax error. expected one of:_all _and ';' ')' ','  
      description: invalid syntax found in the script. 
      resolution: correct the script syntax, using expected token(s) as a guide.... at token [output], line 13 near the ###: 

$descpos = $job.IndexOf("description:")
$resopos = $job.IndexOf("resolution:")

$descmsg = $descmsg.Substring($descpos) 
Write-Host $descmsg
$ferrormsg = $job.Substring($msgpos,$respos+1)
Write-Host $ferrormsg 

工作 - 代码:

 [string]$Result=$job
            $Result= $Result -Replace "[\{]|(\{)|[\}]|(\})|[\""]|(\"")",''
            $msgpos = $Result.IndexOf(("message:") )           
            $resopos = $Result.IndexOf(("resolution:") )            
            $descpos = $Result.IndexOf(("description:") )                
            $ferrormsg = $Result.Substring($msgpos,($descpos-$msgpos) ) 
            $ferrormsg = $ferrormsg -Replace "(\,)|(\')|(\-)|(\=)",''

            Write-Host $ferrormsg

感谢所有, 替换我的字符串工作正常。

2 个答案:

答案 0 :(得分:1)

试试这个

$job= "submitting the job 2fdab2d5-f09c-4392-953e-8b85f90d76eb ... 
      client version: 10.2.2.0 
      target: cluster 
      stat: simulatelarge 
      skippath: true error submitting job.  
      vcclientexceptions.vcclientexception: [httpstatuscode = 0; description = e_csc_user_syntaxerror: syntax error. expected one of:_all _and ';' ')' ','  
      description: invalid syntax found in the script. 
      resolution: correct the script syntax, using expected token(s) as a guide.... at token [output], line 13 near the ###:" 

$descpos = $job.IndexOf("description:")
$resopos = $job.IndexOf("resolution:")
$ferrormsg = $job.Substring($descpos,$resopos-$descpos)
Write-Host $ferrormsg

答案 1 :(得分:0)

其他解决方案:

$job= @"
submitting the job 2fdab2d5-f09c-4392-953e-8b85f90d76eb ... 
client version: 10.2.2.0 
target: cluster 
stat: simulatelarge 
skippath: true error submitting job.  
vcclientexceptions.vcclientexception: [httpstatuscode = 0; description = e_csc_user_syntaxerror: syntax error. expected one of:_all _and ';' ')' ','  
description: invalid syntax found in the script. 
resolution: correct the script syntax, using expected token(s) as a guide.... at token [output], line 13 near the ###:
"@ 

$Result=$job -split "`n" | ? {$_ -like "*:*"} | %{$_.replace(':', '=')} | ConvertFrom-StringData

$Result.description