Windows Vista Forums

Syntax checking of PS scripts
  1. #1


    =?Utf-8?B?QW5kcmV3IFdlYmI=?= Guest

    Syntax checking of PS scripts

    I create and open a Runspace. I set some variables. I create a pipeline
    with some script text. Then I invoke the pipeline. What would be
    fantastically useful for the people who write the scripts is if the
    runspace/pipeline could syntax-check the whole script. Then I can decline to
    invoke, and instead present the list of errors to the user for correction.
    Typically this will be done at design-time, and I won't want to invoke the
    pipeline anyway - just check the script.

    ----------------
    This post is a suggestion for Microsoft, and Microsoft responds to the
    suggestions with the most votes. To vote for this suggestion, click the "I
    Agree" button in the message pane. If you do not see the button, follow this
    link to open the suggestion in the Microsoft Web-based Newsreader and then
    click "I Agree" in the message pane.



    http://www.microsoft.com/communities...ows.powershell

      My System SpecsSystem Spec

  2. #2


    Bruce Payette [MSFT] Guest

    Re: Syntax checking of PS scripts

    The way to do this is to use the NewScriptBlock() convenience method on
    $ExecutionContext. Here's a short VB program that uses this to syntax-check
    a script without running it. Most of it is setup - creating the runspace,
    getting the file, etc. The code to do the syntax check once you have the
    object, is simply:

    Try
    ' compile the script and just ignore the scriptblock that's
    returned...
    powerShell.NewScriptBlock(textToParse)
    Catch ex As RuntimeException
    System.Console.WriteLine("{0}{1}", ex.Message,
    ex.ErrorRecord.InvocationInfo.PositionMessage)
    Environment.Exit(1)
    End Try

    Note that there are thread-safety limitations on this. You can't be checking
    one script while running another in the same runspace. For most
    applications, this won't be a problem but if it is, use the default runspace
    for syntax checks and create a second one for execution.

    Here's the program:
    -----------------------------------------------------------
    Imports System.Management.Automation
    Imports System.Management.Automation.Runspaces

    Module ParsePowerShell
    Sub Main(ByVal args As String())
    ' Verify that there is at least one argument...
    If (args.Length <> 1) Then
    System.Console.Error.WriteLine("usage: ParsePowerShell
    <filename>")
    Environment.Exit(2)
    End If

    ' read the file to parse (should validate extension but we don't
    Dim textToParse As String = ""
    Try
    textToParse = System.IO.File.ReadAllText(args(0))
    Catch ex As Exception
    System.Console.Error.WriteLine("parsing file '{0}': {1}",
    args(0), ex.Message)
    Environment.Exit(1)
    End Try

    ' Set up a runspace. make it the default runspace and open it
    Dim myRunspace As Runspace
    myRunspace = RunspaceFactory.CreateRunspace()
    Runspace.DefaultRunspace = myRunspace
    myRunspace.Open()

    ' Get the command invocation intrinsics proxy class so
    ' we can call NewScriptBlock() method directly...
    Dim executionContext As EngineIntrinsics =
    myRunspace.SessionStateProxy.GetVariable("ExecutionContext")
    Dim powerShell As CommandInvocationIntrinsics =
    executionContext.InvokeCommand

    ' Now use this method to parse the text we read earlier
    ' If there is a runtime error, display it along with the position
    message...
    Try
    powerShell.NewScriptBlock(textToParse)
    Catch ex As RuntimeException
    System.Console.WriteLine("{0}{1}", ex.Message,
    ex.ErrorRecord.InvocationInfo.PositionMessage)
    Environment.Exit(1)
    End Try
    Environment.Exit(0)
    End Sub

    End Module
    -----------------------------------------------------------

    -bruce

    --
    Bruce Payette [MSFT]
    Windows PowerShell Technical Lead
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.


    "Andrew Webb" <AndrewWebb@discussions.microsoft.com> wrote in message
    news:7C021470-E2A7-4681-8C27-9B11D63CB5E2@microsoft.com...
    >I create and open a Runspace. I set some variables. I create a pipeline
    > with some script text. Then I invoke the pipeline. What would be
    > fantastically useful for the people who write the scripts is if the
    > runspace/pipeline could syntax-check the whole script. Then I can decline
    > to
    > invoke, and instead present the list of errors to the user for correction.
    > Typically this will be done at design-time, and I won't want to invoke the
    > pipeline anyway - just check the script.
    >
    > ----------------
    > This post is a suggestion for Microsoft, and Microsoft responds to the
    > suggestions with the most votes. To vote for this suggestion, click the "I
    > Agree" button in the message pane. If you do not see the button, follow
    > this
    > link to open the suggestion in the Microsoft Web-based Newsreader and then
    > click "I Agree" in the message pane.
    >
    > http://www.microsoft.com/communities...ows.powershell




      My System SpecsSystem Spec

  3. #3


    =?Utf-8?B?QW5kcmV3IFdlYmI=?= Guest

    Re: Syntax checking of PS scripts

    Thanks Bruce, that works!

    BTW: you've got some \n characters in the
    ex.ErrorRecord.InvocationInfo.PositionMessage string. E.g. I typed in some
    random characters for my script, and the PositionMessage comes back as:

    "\nAt line:2 char:4\n+ a*&^ <<<< (E*&^WE"

    This will probably display fine in a message box and be OK when written to
    the console, but I'm logging error information to file. So the \n is
    displayed a non-printable character. Are you hard-coding \n instead of
    reading Environment.NewLine ?

    Of course I can replace \n with Environment.NewLine, but I don't want to do
    this if PowerShell is going to change its behaviour in this regard.

      My System SpecsSystem Spec

Syntax checking of PS scripts problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
run .cmd scripts on remote machine (automating the MOSS install via scripts) Zoran PowerShell 1 21 Jul 2009
Generic Interface syntax in VS 2005 using Old syntax Saad .NET General 1 09 Jun 2009
Help with syntax hansel PowerShell 7 14 Dec 2008
"invalid STORE command syntax invalid message set syntax" Catullus Nacakus Vista mail 6 26 Jan 2008
need syntax carmine934 Vista hardware & devices 0 02 Jun 2007