2013-02-11 168 views
7

我使用Get-EventLog設置變量,然後使用事件ID描述設置另一個變量。然後我使用blat.exe將這些信息通過電子郵件發送給一個組。PowerShell:從變量中刪除或替換引號

說明中包含引號。引號引起錯誤退出。

有沒有辦法從event.Message中刪除引號並用空格或其他東西替換它們?

回答

7

我其實已經明白了。引號和雙引號的數量令我困惑,但這已經奏效,並且沒有錯誤。

$var -replace '"', "" 

這些引號是:single,double,single,comma,double,double。

13

如果變量是一個String對象,那麼你可以做到以下幾點:

$Variable.Replace("`"","") 
3

如果您使用PowerShell的內置send-mailmessage(需要2.0),可以消除你對blat.exe依賴,並妥善處理這一問題無需編輯事件日誌中的描述。

+0

大點。將查看該問題 – Pat 2013-02-11 17:15:19

2

問題是,一個簡單的替換會清除每個引號字符,即使轉義(加倍)。 以下是我爲我創建的功能:

  • 一個只刪除孤立引號的功能。
  • 其中一個逃脫他們

我也讓他們通用的管理其他字符,與optionnal $ charToReplace參數

#Replaces single occurences of characters in a string. 
#Default is to replace single quotes 
Function RemoveNonEscapedChar { 
    param(
     [Parameter(Mandatory = $true)][String] $param, 
     [Parameter(Mandatory = $false)][String] $charToReplace 
    ) 

    if ($charToReplace -eq '') { 
     $charToReplace = "'" 
    } 
    $cleanedString = "" 
    $index = 0 
    $length = $param.length 
    for ($index = 0; $index -lt $length; $index++) { 
     $char = $param[$index] 
     if ($char -eq $charToReplace) { 
      if ($index +1 -lt $length -and $param[$index + 1] -eq $charToReplace) { 
       $cleanedString += "$charToReplace$charToReplace" 
       ++$index ## /!\ Manual increment of our loop counter to skip next char /!\ 
      } 
      continue 
     } 
     $cleanedString += $char 
    } 
    return $cleanedString 
} 
#A few test cases : 
RemoveNonEscapedChar("'st''r'''i''ng'")        #Echoes st''r''i''ng 
RemoveNonEscapedChar("""st""""r""""""i""""ng""") -charToReplace '"' #Echoes st""r""i""ng 
RemoveNonEscapedChar("'st''r'''i''ng'") -charToReplace 'r'   #Echoes 'st'''''i''ng' 


#Escapes single occurences of characters in a string. Double occurences are not escaped. e.g. ''' will become '''', NOT ''''''. 
#Default is to replace single quotes 
Function EscapeChar { 
    param(
     [Parameter(Mandatory = $true)][String] $param, 
     [Parameter(Mandatory = $false)][String] $charToEscape 
    ) 

    if ($charToEscape -eq '') { 
     $charToEscape = "'" 
    } 
    $cleanedString = "" 
    $index = 0 
    $length = $param.length 
    for ($index = 0; $index -lt $length; $index++) { 
     $char = $param[$index] 
     if ($char -eq $charToEscape) { 
      if ($index +1 -lt $length -and $param[$index + 1] -eq $charToEscape) { 
       ++$index ## /!\ Manual increment of our loop counter to skip next char /!\ 
      } 
      $cleanedString += "$charToEscape$charToEscape" 
      continue 
     } 
     $cleanedString += $char 
    } 
    return $cleanedString 
} 
#A few test cases : 
EscapeChar("'st''r'''i''ng'")        #Echoes ''st''r''''i''ng'' 
EscapeChar("""st""""r""""""i""""ng""") -charToEscape '"' #Echoes ""st""r""""i""ng"" 
EscapeChar("'st''r'''i''ng'") -charToEscape 'r'   #Echoes 'st''rr'''i''ng' 
0

上述答案的工作爲了我。所以,我創建了以下解決方案......

搜索和替換字符單引號 「'」 ASCII字符(39)與空間 「」 ASCII字符(32)

$strOldText = [char] 39 
$strNewText = [char] 32 

$Variable. = $Variable..Replace($strOldText, $strNewText).Trim() 
+0

此問題已在4年前回答。也就是說,你的新答案不提供上下文。你能編輯和解釋你在說什麼嗎? – Pat 2017-04-20 02:14:16

+0

如果在過去的4年中未搜索或打開此問題,嘗試提供更好的/替代的答案只是無用的。 Trim()實際上是用於從字符串的開始/結尾「修剪」字符的方法。在這種情況下,它仍然不完美,因爲還有更適合的重載版本Trim(Char [])。更簡單,不會從字符串中刪除轉義引號,也不會刪除可能的空白字符。看看我的答案。 – papo 2017-07-29 02:08:07

0

簡單,使用Trim(Char[]) method
...刪除所有開頭和結尾出現...

e.g. $your_variable.Trim('"') 

它會繼續,躲過任何報價或不是,在字符串中的位置:

PS C:\> $v.Trim('"') # where $v is: "hu""hu"hu'hu" 
hu""hu"hu'hu 

拿一點,它也將刪除孤立的雙引號:

PS C:\> $v.Trim('"') # where $v is: "hu""hu"hu'hu 
hu""hu"hu'hu