Powershell 字符串保持 CR 和 LF

时间:2021-07-15 12:26:01

标签: mysql string powershell newline quotes

我想通过“MySql.Data.MySqlClient.MySqlConnection”在 MySQL 数据库中插入一个 Blob,它工作正常,但 Powershell 从我的变量中删除了换行符:

$configString = Get-Content config.xml 
$configString > test5.xml
$strAddConfig = "INSERT INTO config_xml(Version,ConfigXML,MD5,Comment,ClientMinVersion) VALUES('2','" + $($configString) + "','$configMD5','BLABLA','5.0.0.0')"

当我将 $configString 变量通过管道传输到文本文件时,CR&LF 字符将保留,当我通过复制粘贴输入 blob 时也会考虑换行符。我尝试了“strAddConfig”行与不同的报价组合,但没有成功。

感谢帮助

1 个答案:

答案 0 :(得分:0)

# Read the text content *as a single, multi-line string*, using -Raw
$configString = Get-Content -Raw config.xml

# Use an expandable here-string for better formatting.
$strAddConfig = @"
INSERT INTO config_xml(Version,ConfigXML,MD5,Comment,ClientMinVersion)
VALUES('2','$configString','$configMD5','BLABLA','5.0.0.0')
"@

您的(主要)问题是使用了 Get-Content config.xml without -Raw,它在 $configString 中存储了一个 array 行,并且当数组用于 expandable string "...",带字符串插值)中时,其(字符串化)元素以 空格 分隔:

PS> $array = 'one', 'two'; "$array"
one two 
相关问题