我使用Get-EventLog設置變量,然後使用事件ID描述設置另一個變量。然後我使用blat.exe將這些信息通過電子郵件發送給一個組。PowerShell:從變量中刪除或替換引號
說明中包含引號。引號引起錯誤退出。
有沒有辦法從event.Message中刪除引號並用空格或其他東西替換它們?
我使用Get-EventLog設置變量,然後使用事件ID描述設置另一個變量。然後我使用blat.exe將這些信息通過電子郵件發送給一個組。PowerShell:從變量中刪除或替換引號
說明中包含引號。引號引起錯誤退出。
有沒有辦法從event.Message中刪除引號並用空格或其他東西替換它們?
我其實已經明白了。引號和雙引號的數量令我困惑,但這已經奏效,並且沒有錯誤。
$var -replace '"', ""
這些引號是:single,double,single,comma,double,double。
如果變量是一個String對象,那麼你可以做到以下幾點:
$Variable.Replace("`"","")
如果您使用PowerShell的內置send-mailmessage
(需要2.0),可以消除你對blat.exe
依賴,並妥善處理這一問題無需編輯事件日誌中的描述。
問題是,一個簡單的替換會清除每個引號字符,即使轉義(加倍)。 以下是我爲我創建的功能:
我也讓他們通用的管理其他字符,與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'
上述答案的工作爲了我。所以,我創建了以下解決方案......
搜索和替換字符單引號 「'」 ASCII字符(39)與空間 「」 ASCII字符(32)
$strOldText = [char] 39
$strNewText = [char] 32
$Variable. = $Variable..Replace($strOldText, $strNewText).Trim()
簡單,使用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
大點。將查看該問題 – Pat 2013-02-11 17:15:19