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?
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
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.
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.