Backtick

This topic contains 4 replies, has 3 voices, and was last updated by

 
Participant
1 week, 2 days ago.

  • Author
    Posts
  • #112168

    Participant
    Points: 0
    Rank: Member

    Import-Module operationsmanager

    New-SCmanagementGroupConnection -ComputerName "[server name]" -Credential (Get-Credential [username])

    $SCOMClass = Get-SCOMClass -Name microsoft.sqlserver.2005.dbengine
    $SCOMClassInstance = $SCOMClass| Get-SCOMClassInstance

    This line retunrs the property as a value
    $SCOMClassInstance | Select-Object {$_.'[Microsoft.Windows.Computer].PrincipalName'}

    This line retuns the property as an object
    $SCOMClassInstance | Select-Object ``[Microsoft.Windows.Computer`].PrincipalName

    Could someone please explain what the purpose is of the "Backticks" in the last line of code?
    Is there a different syntax to get the same result?

  • #112181

    Participant
    Points: 0
    Rank: Member
    Import-Module operationsmanager
    
    New-SCmanagementGroupConnection -ComputerName "[server name]" -Credential (Get-Credential [username])
    
    $SCOMClass = Get-SCOMClass -Name microsoft.sqlserver.2005.dbengine
    $SCOMClassInstance = $SCOMClass| Get-SCOMClassInstance
    
    This line retunrs the property as a value
    $SCOMClassInstance | Select-Object {$_.'[Microsoft.Windows.Computer].PrincipalName'}
    
    This line retuns the property as an object
    $SCOMClassInstance | Select-Object ``[Microsoft.Windows.Computer`].PrincipalName

    Could someone please explain what the purpose is of the "Backticks" in the last line of code?
    Is there a different syntax to get the same result?

  • #112189

    Participant
    Points: 0
    Rank: Member

    It's a bit weird 🙂

    in this case, notice the backtick

    1 | select ``[iam`].weird
    
    `[iam].weird
    ------------
    

    What I see happening in this case is that first two backticks get interpretted as a single backtick, because backtick escaping a backtick is a backtick. The last backtick escapes ], `] has no special meaning, unlike for example `n -> a new line, so it's interpreted simply as ]. Then that name is used as the selector.

    but

    when the object actually has such weird property it starts working differently
    the select returns incorrect values when provided the name without backticks
    but correct when provided two backticks

    $a = [pscustomobject]@{ "[iam].weird" = 1 }
    $a | select [iam].weird
    
    [iam].weird
    -----------
    
    
    $a | select `[iam`].weird
    
    [iam].weird
    ----------- 
     
    
    $a | select ``[iam`].weird
    
    [iam].weird
    -----------
              1
    

    All in all the runtime interprets this as a pattern that means => match any of the three characters i a or m.

    $b = [pscustomobject]@{ "[iam].weird" = 1; "i.weird" = 2 }
    $b | select '[iam].weird'
    
    i.weird
    -------
          2
    

    And the backticks are escapting that pattern matching.

    Either you do '`[iam].weird', or you do ``[iam`].weird which is parsed twice, once by the parameter parser and once by the select internals. You can also see error about the wildcard if you do this incorrectly and provide incomplete pattern for example by escaping only the last ]

    $b | select '[iam`].weird'

    Why the parse works differently in the first case I don't know, probably it handles values and objects differently.

    • #112262

      Participant
      Points: 0
      Rank: Member

      Nohwnd, thank you for taking the time to explain this weirdness! It is much appreciated.

      I have tried your syntax and sure it works!! 🙂

      $DBEngines = (Get-SCOMClassInstance -Class $SCOMClass) | Select-Object '`[Microsoft.Windows.Computer].PrincipalName', '`[Microsoft.SQLServer.ServerRole].InstanceName'

      Is this backtick something that only applies to that class or does this apply to all PowerShell classes?

  • #113753

    Participant
    Points: 0
    Rank: Member

    The backtick is PowerShell's escape character. It either adds special meaning to a character that is not typically special, or it removes special meaning from a character that usually is special.

    You'll sometimes see it at the end of lines to escape the special meaning of the enter key in order to wrap commands on multiple lines. Complicated hash tables use this a lot to make the lines line up for easy reading.

    Get-Service `
    Bits
    

    These lines would normally cause an error (Get-Service would return all services but then the Bits line would fail because it isn't recognized as the name of a command). By including the backtick PowerShell appends the 2nd line to the first line and runs them together as if I had written:

    Get-Service Bits
    

    You'll also see them a lot in strings of text for various reasons.

    "If I do this `n then I get a new line"
    "And this will `t tab for me"
    "And this will escape the special meaning of a dollar sign `$ so that PowerShell writes it rather than tries to find a variable"
    "And like you saw before a double backtick `` will write a literal backtick"
    "Fred said, `"Whoah`""
    

    In the first line the backtick adds special meaning to the n character which normally is just an n. A `n will make a new line just like a `t will do a tab. In the 3rd line I removed the special meaning of $. And in the 4th line I escaped the special meaning of a backtick to print a backtick. In that last example I had to backtick my quotes within the string so PowerShell wouldn't see 2 sets of quotes. With the backtick in there my string can contain literal quotes.

You must be logged in to reply to this topic.