我有一个powershell函数,它从db获取值。有没有办法在检索到的值上添加$符号?
例如:在数据检索之后,如何添加$ sign必须具有代表成本的值。
function ParametersingData {
param([string]$tablename)
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=servername; Database=dbname; Integrated Security=true"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "SELECT * from $tablename"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.SelectCommand = $SqlCmd
try {
$rowcount = $SqlAdapter.Fill($DataSet)
} catch [Exception] {
Write-Error $_.Exception.Message
}
return ()
}
脚本文件如下:
Data $(ParametersingData "tablename")
答案 0 :(得分:2)
如果要在字符串中添加美元符号,只需在美元之前添加反引号,或者使用美元符号的单引号将其解释为表达式限定符。此外,如果美元符号没有左括号或文字前缀,则不会将其解释为特殊字符并按字面使用。一个例子:
$sum=5
"$sum$" # returns "5$"
"`$sum" # returns "$sum"
'$sum' # also returns "$sum"
"$sum`$(`$sum)" # returns "5$($sum)"
"$sum$(`$sum)" # throws exception, Name "$sum" undefined
"$sum$ (`$sum)" # returns "5$ ($sum)"