2017-06-20 44 views
-1

我想獲得正確的錯誤消息,它在描述中。但是當我的調試器進入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 

工作 - CODE:

[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 

感謝所有, 保換我的字符串是工作的罰款。

+1

是'$ job'字符串數組或用換行一個字符串? – vonPryz

+0

'$ job =提交工作2fdab2d5-f09c-4392-953e-8b85f90d76eb ...'會拋出一個'CommandNotFoundException' – Clijsters

+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 
+2

它有助於如果你解釋你的答案,以便其他人可以理解你的建議爲什麼。 –

+0

當然,基本上你想要介於(「description:」)和(「resolution:」)之間的所有東西,因此First讓我們得到它們中的每一個的索引。然後使用Substring(StartIndex,第一個字符後的字符數)。是: - $ descpos和$ resopos分別爲 –

+0

@ChetanKulkarni Thnaks for reply,但是當我輸入$ ferrormsg = $ job.Substring($ descpos,$ resopos- $ descpos)時,它已經退出。 –

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 
+0

感謝您的答案,嘗試這種方式 –

相關問題