0

How to handle a $ sign inside a parameter used for SQL code. If I am going with code all My $Some_text (Prefix with $) with Null (Noting Just a space)

POSH command used :

    $attachSQLCMD1 = @" .........  ....
...................................................
OR (OBJECT_ID(''dbo.User Personalization'',''U'') IS NOT NULL AND EXISTS(SELECT 1 FROM sys.all_objects (NOLOCK) WHERE type=''U'' AND (name like ''%**$**G[_]L Entry'' OR name LIKE ''%**$**Item Ledger Entry'')))
.................................................
..........................................................

$partitions = Get-WmiObject -computername $serverName -query "SELECT * FROM Win32_DiskPartition"
foreach ($partition in $partitions)
{
[string] $diskpart = "{0}_{1};{2}" -f $partition.DiskIndex,$partition.Index,$partition.StartingOffset
Write-Output $diskpart
 "@





 Invoke-Sqlcmd $attachSQLCMD1 -QueryTimeout 0 -ServerInstance $SQLServer -maxcharlength ([int]::MaxValue) 

Thanks In advance!!

Ganesh
  • 1
  • 1
    I hope the [linked answer](https://stackoverflow.com/a/55614306/45375) solves your problem. – mklement0 Apr 10 '20 at 19:15
  • No, ` not accepted by SQL while I'm running invoke-sqlcmd – Ganesh Apr 10 '20 at 19:53
  • 1
    SQL wouldn't see the `\`` - it gets removed during PowerShell's string parsing. Please update your question with a minimal example. Currently, you have PowerShell code inside your here-string. Is that the intent? In order to embed the _output_ from a PowerShell command in an expandable (here) string, you'd have to wrap it in `$(...)`. – mklement0 Apr 10 '20 at 20:03

0 Answers0