<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'PowerShell' and 'syntax'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=PowerShell,syntax&amp;orTags=0</link><description>Search results matching tags 'PowerShell' and 'syntax'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>A little PowerShell syntax tip : dealing with SQL Server named instances</title><link>http://sqlblog.com/blogs/aaron_bertrand/archive/2011/05/03/a-little-powershell-syntax-tip-dealing-with-sql-server-named-instances.aspx</link><pubDate>Tue, 03 May 2011 22:32:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:35415</guid><dc:creator>AaronBertrand</dc:creator><description>&lt;p&gt;This afternoon I was working on a little PowerShell script to collect some PerfMon counters. I borrowed some code from myself, and had counters represented in strings like the following (this is a greatly simplified version): &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;Get-Counter&lt;/font&gt; &lt;font color="navy"&gt;-Counter&lt;/font&gt; &lt;font color="maroon"&gt;"\SQLServer:Wait Statistics(Average wait time (ms))\Lock waits"&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;The above code is for a default instance, but in this case I was dealing with a named instance (SQL2008R2). So I simply replaced "SQLServer:" with "MSSQL$SQL2008R2":&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;Get-Counter&lt;/font&gt; &lt;font color="navy"&gt;-Counter&lt;/font&gt; &lt;font color="maroon"&gt;"\&lt;b&gt;MSSQL$SQL2008R2&lt;/b&gt;:Wait Statistics(Average wait time (ms))\Lock waits"&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;


&lt;p&gt;When I ran this code, I received the following error: &lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="color:red;padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;Get-Counter : The specified object was not found on the computer.
At line:1 char:12
+ Get-Counter &amp;lt;&amp;lt;&amp;lt;&amp;lt;  -Counter "\MSSQL$SQL2008R2:Wait Statistics(*)\Lock waits"
    + CategoryInfo          : InvalidResult: (:) [Get-Counter], Exception
    + FullyQualifiedErrorId : CounterApiError,Microsoft.PowerShell.Commands.GetCounterCommand &lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;

&lt;p&gt;The PowerShell gurus will spot the problem instantly, but &lt;b&gt;play along&lt;/b&gt;, okay? I verified the format of the object, instance and counter name using Performance Monitor, going so far as validating exact case:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;img src="http://bertrandaaron.files.wordpress.com/2011/05/perfcounter.png" border="1" height="41" width="390"&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I even copied the properties to Notepad to make sure I wasn't being dumb and transcribing incorrectly:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;img src="http://bertrandaaron.files.wordpress.com/2011/05/pcproperty.png" border="1" height="18" width="769"&gt;&amp;nbsp;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I quickly searched for examples of folks using PowerShell to pull performance counter data from named instances of SQL Server, but either there aren't many out there, or my Google-fu was weak today.&lt;/p&gt;

&lt;p&gt;I am not sure exactly why, but I looked at the Get-Counter line again, and &lt;b&gt;the $ sign jumped out at me&lt;/b&gt;. Time is money; not sure if there is a correlation there. Anyway, when I properly escaped the dollar sign with a backward apostrophe (grave accent), it worked just fine, even though it looks funny:&lt;/p&gt;

&lt;blockquote&gt;
&lt;table bgcolor="#eeeeee" cellpadding="0" cellspacing="0"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre style="padding:10px 20px;font-size:12px;font-family:consolas,lucida console,courier new,courier;-moz-background-inline-policy:continuous;"&gt;&lt;font color="blue"&gt;Get-Counter&lt;/font&gt; &lt;font color="navy"&gt;-Counter&lt;/font&gt; &lt;font color="maroon"&gt;"\MSSQL&lt;b&gt;`&lt;/b&gt;$SQL2008R2:Wait Statistics(Average wait time (ms))\Lock waits"&lt;/font&gt;&lt;br&gt;&lt;font color="green"&gt;#---------------------------^&lt;/font&gt;&lt;br&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/blockquote&gt;Of course, looking back, it's blindingly obvious. I just won't get those 15 minutes back, and hope to save someone else the same 15 minutes. &lt;p&gt;[The error message does not seem to indicate that it tried to 
substitute the variable with anything, or that the counter wasn't found 
because the name wasn't complete. So I spent most of my time chasing 
other avenues. I'm going to leave that rabbit hole for another day, as I spent more time writing this post than solving the original issue.]&lt;/p&gt;&lt;p&gt;&amp;nbsp; &lt;/p&gt;</description></item></channel></rss>