none
PowerShell Script to get Memory\Available MBytes RRS feed

  • Question

  • I have a SQL Agent job with 3 separate Operating System Command Steps.

    PowerShell.exe Get-Date
    PowerShell.exe Get-Counter "\\mmserver\Memory\Pages/sec" | Format-table -auto
    PowerShell.exe Get-Counter "\\myserver\Memory\Available MBytes" | Format-Table -auto

    The first two steps work fine.  The third fails.  I have tried reformatting the counter name in any number of ways including escaping the backslashes, adding single quotes around various pieces, and I tried creating a string variable for the counter name.

    The failure message varies a little depending on what I change but basically it seems to be trying to interpret "MBytes" as a separate parameter.  For example:

    The error information returned by PowerShell is: 'Get-Counter : A positional parameter cannot be found that accepts argument 'MBy  '

    Followed immediately in the log file with a second error on the remainder of the command string.

    The error information returned by PowerShell is: 'tes'.  At line:1 char:12  + Get-Counter <<<<  \\myserver\Memory\Available MBytes      + CategoryInfo          : InvalidArgument: (:) [Get-Counter], ParameterBindingException    '

    Other errors indicate the name of the counter is wrong in someway.  For example, I found I had to include the server name even though the script is running locally and it works find from the ISE.

    I appreciate any assistance :)


    Ray

    Tuesday, December 17, 2013 12:27 AM

Answers

  • Here's what worked for me from a command prompt, though I'm not sure why you were seeing that behavior when the value appeared to be quoted properly. Maybe cmd.exe and/or SQL Server was stripping the quotes before sending the string to PowerShell.exe:

    PowerShell.exe "Get-Counter '\\myserver\Memory\Available MBytes' | Format-Table -auto"

    • Marked as answer by R Herring Tuesday, December 17, 2013 2:59 PM
    Tuesday, December 17, 2013 1:01 AM

All replies

  • Here's what worked for me from a command prompt, though I'm not sure why you were seeing that behavior when the value appeared to be quoted properly. Maybe cmd.exe and/or SQL Server was stripping the quotes before sending the string to PowerShell.exe:

    PowerShell.exe "Get-Counter '\\myserver\Memory\Available MBytes' | Format-Table -auto"

    • Marked as answer by R Herring Tuesday, December 17, 2013 2:59 PM
    Tuesday, December 17, 2013 1:01 AM
  • Hi David,

    Thanks for the reply.  I put your exact text into the job step and it worked !

    I don't know how I missed that particular format of single and double quotes. I thought I had tried them all and I missed the most obvious.


    Ray

    Tuesday, December 17, 2013 3:01 PM