how to handle $ inside parameters in powershell

Welcome Forums General PowerShell Q&A how to handle $ inside parameters in powershell

Viewing 1 reply thread
  • Author
    Posts
    • #217602
      Participant
      Topics: 4
      Replies: 2
      Points: 40
      Rank: Member

      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!!

    • #217620
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      Use single qoutes to make it literal. If you use double qoutes Powershell will attempt to expand the $:

      $tbl = 'User Personalization'
      
      $temp = @'
      ...
      OR (OBJECT_ID(”dbo.{0}”,”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”
          )
      )
      '@ -f $tbl
      
      $temp
      
Viewing 1 reply thread
  • You must be logged in to reply to this topic.