Monitoring Studio: Executing Commands in Windows PowerShell Using the Command-line Analysis Tool
KB1111 - Feb 21, 2013
Type: Best Practice
Description: Use the command-line analysis tool in Monitoring Studio to execute commands in Windows PowerShell.
Additional Keywords: Command Line, PowerShell
This document is intended as a supplement to Command-line Analysis monitoring in Monitoring Studio, where the commands are to be run in the Windows PowerShell.
The option 'inputformat none' should be used with the PowerShell command.
The Command-line Analysis monitoring feature is detailed in the product documentation. Click here to open the related topic of the online help.
Settings up Windows PowerShell to allow scripts with extension .ps1 to be run
Verify that the PowerShell script can be called from a Windows command prompt.
In a normal Windows command prompt (not elevated) logged on as Administrator, the PowerShell script should be able to execute.
> powershell c:\my_script.ps1
The absolute path needs to be specified. Specifying a relative path will attempt to execute a script with the same name (if one exists) from a directory in the Windows PATH environment. If the script runs, then the ExecutionPolicy is set appropriately to call PowerShell commands in Command-line Analysis of Monitoring Studio.
If the following message appears, the ExecutionPolicy will need to be modified.
The term 'my_script.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:14
+ my_script.ps1 <<<<
+ CategoryInfo : ObjectNotFound: (my_script.ps1:String) , CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Logged on as Administrator, in a Windows PowerShell, the execution policy can be verified thus:
There are four possible settings of the Execution Policy:
- Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode.
- AllSigned - Only scripts signed by a trusted publisher can be run.
- RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.
- Unrestricted - No restrictions; all Windows PowerShell scripts can be run.
As explained in the descriptions above, only the Execution Policies RemoteSigned and Unrestricted will allow PowerShell scripts to run Command-line Analysis monitoring in Monitoring Studio.
To set the Execution Policy
PS> Set-ExecutionPolicy unrestricted
After setting the ExecutionPolicy, the change will take effect immediately in the PowerShell, but any standard command prompt will need to be restarted.
Executing a command in Windows PowerShell with Monitoring Studio
If individual commands are required to be run on PowerShell, use the 'inputformat none' command like so
@powershell -inputformat none echo at-powershell-test
Executing a PowerShell script using Command-Line Analysis in Monitoring Studio
Use the 'inputformat none' option when referencing the .ps1 script in the 'Command line to execute' in Monitoring Studio.
If a .ps1 script file is to be called, use the 'inputformat none' option.
powershell -inputformat none "c:\powershell_script.ps1"