Get-EventLogを使用して変数を設定してから、イベントIDの説明を使用して別の変数を設定しています。次に、blat.exeを使用して、この情報をグループに電子メールで送信します。
説明には引用符が含まれています。引用符が原因で、blatがエラーで終了します。
event.Messageから引用符を削除し、スペースなどに置き換える方法はありますか?
Get-EventLogを使用して変数を設定してから、イベントIDの説明を使用して別の変数を設定しています。次に、blat.exeを使用して、この情報をグループに電子メールで送信します。
説明には引用符が含まれています。引用符が原因で、blatがエラーで終了します。
event.Messageから引用符を削除し、スペースなどに置き換える方法はありますか?
変数がStringオブジェクトの場合、次の操作を実行できます。
$Variable.Replace("`"","")
私は実際にそれを手に入れました。引用符と二重引用符の数は私を混乱させましたが、これは機能し、blatはエラーになりませんでした。
$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の組み込みsend-mailmessage
(2.0が必要)を使用する場合は、イベントログの説明を編集しなくても、依存関係を排除してblat.exe
この問題を適切に処理できます。
問題は、単純な置換では、エスケープ(2倍)された場合でも、すべての引用符文字が消去されることです。これが私が使用するために作成した関数です:
また、オプションの$ 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)を検索してスペース "" ascii Character(32)に置き換えます
$strOldText = [char] 39
$strNewText = [char] 32
$Variable. = $Variable..Replace($strOldText, $strNewText).Trim()