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