Escape caracteres ao gerar scripts Powershell usando c #
Eu uso o VS2010, C #, .NET 3.5 para gerar scripts Powershell (arquivos ps1).
Então, é necessário caracteres de escape para o Powershell.
Alguma sugestão sobre isso para desenvolver um bom método que escape caracteres?
public static partial class StringExtensions
{
/*
PowerShell Special Escape Sequences
Escape Sequence Special Character
`n New line
`r Carriage Return
`t Tab
`a Alert
`b Backspace
`" Double Quote
`' Single Quote
`` Back Quote
`0 Null
*/
public static string FormatStringValueForPS(this string value)
{
if (value == null) return value;
return value.Replace("\"", "`\"").Replace("'", "`'");
}
}
Uso:
var valueForPs1 = FormatStringValueForPS("My text with \"double quotes\". More Text");
var psString = "$value = \"" + valueForPs1 + "\";";