我正在使用 Get-EventLog 设置一个变量,然后使用事件 ID 描述设置另一个变量。然后我使用 blat.exe 将此信息通过电子邮件发送给一个组。
描述包含引号。引号导致 blat 退出并出现错误。
有没有办法从 event.Message 中删除引号并用空格或其他东西替换它们?
我正在使用 Get-EventLog 设置一个变量,然后使用事件 ID 描述设置另一个变量。然后我使用 blat.exe 将此信息通过电子邮件发送给一个组。
描述包含引号。引号导致 blat 退出并出现错误。
有没有办法从 event.Message 中删除引号并用空格或其他东西替换它们?
如果变量是 String 对象,那么您可以执行以下操作:
$Variable.Replace("`"","")
我其实刚收到。引号和双引号的数量让我感到困惑,但这已经奏效并且没有错误。
$var -replace '"', ""
这些引号是:单,双,单,逗号,双,双。
根据具体情况,使用Trim(Char[]) 方法可能更简单:
...删除所有前导和尾随事件...
e.g. $your_variable.Trim('"')
它只会从 $your_variable 的开头和结尾删除引号。它将保留 $your_variable 文本中的任何引号,无论是否转义:
PS C:\> $v.Trim('"') # where $v is: "hu""hu"hu'hu"
hu""hu"hu'hu
您可以使用Trim('"')
, Trim("'")
,也可以同时使用:Trim("`"'")
请注意,Trim() 不关心引号是否是孤立的,这意味着它会删除结束或开始的引号,无论它在字符串的另一侧是否有成对的引号。
PS C:\Users\Papo> $hu = "A: He asked `"whos this sofa?`" B: She replied: `"Chris'`""
PS C:\Users\Papo> $hu
A: He asked "whos this sofa?" B: She replied: "Chris'"
PS C:\Users\Papo> $hu.trim('"')
A: He asked "whos this sofa?" B: She replied: "Chris'
PS C:\Users\Papo> # and even worse:
PS C:\Users\Papo> $hu.trim("'`"")
A: He asked "whos this sofa?" B: She replied: "Chris
如果您使用 Powershell 的内置(需要 2.0),则无需编辑事件日志中的描述send-mailmessage
即可消除对该问题的依赖并正确处理此问题。blat.exe
问题是简单的替换会清除每个引号字符,即使转义(加倍)也是如此。以下是我为自己使用而创建的功能:
我还使用可选的 $charToReplace 参数使它们通用以管理其他字符
#Replaces single occurrences 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 Character (39) with a space " " ascii Character (32)
$strOldText = [char] 39
$strNewText = [char] 32
$Variable. = $Variable..Replace($strOldText, $strNewText).Trim()