Created
June 7, 2022 07:54
-
-
Save fatherjack/69f191faf90ca840a8bcc1b2e1271d7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Format-QuotedString { | |
<# | |
.SYNOPSIS | |
Wraps a string in delimiters | |
.DESCRIPTION | |
Takes in a string, returns the same string wrapped in chosen delimiters. Made to fill a similar role as the TSQL QUOTENAME function. | |
.PARAMETER String | |
The string you want wrapped | |
.PARAMETER Delimiter | |
The delimiter that you want to wrap your string | |
.EXAMPLE | |
QuoteName "This is a string" | |
output: | |
'This is a string' | |
.EXAMPLE | |
QuoteName "This is a string" '/*' | |
output: | |
/*This is a string*/ | |
.NOTES | |
Author Jonathan Allen | |
Date June 2022 | |
#> | |
[Alias('Quotename')] | |
[cmdletbinding()] | |
param( | |
[string]$String, | |
[string]$Delimiter | |
) | |
$Lookup = @{ | |
"'" = "','" | |
'"' = '","' | |
'[' = "[,]" | |
'(' = '(,)' | |
'{' = '{,}' | |
'<' = '<,>' | |
'-' = '-,-' | |
'/*' = '/*,*/' | |
'<#' = '<#,#>' | |
} | |
if ($Delimiter -eq '') { $Delimiter = "'" } | |
$Delimiter1, $Delimiter2 = $Lookup[$Delimiter] -split ',' | |
# return the wrapped string | |
return "$Delimiter1$String$Delimiter2" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment