Domanda

I am working on a utility that will take the contents of a .ps1 script (Powershell), and run it in the context of a VB.NET Winform. Basically it does this by referencing some Powershell DLLs, opening the .ps1 file, reading a line of text and feeding it into an internal string that will run on a button click event.

Here's the full code I have so far:

    Private Function RunScript(ByVal scriptText As String) As String

    ' create Powershell runspace 
    Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()

    ' open it 
    MyRunSpace.Open()

    ' create a pipeline and feed it the script text 
    Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()

    MyPipeline.Commands.AddScript(scriptText)

    ' add an extra command to transform the script output objects into nicely formatted strings 
    ' remove this line to get the actual objects that the script returns. For example, the script 
    ' "Get-Process" returns a collection of System.Diagnostics.Process instances. 
    MyPipeline.Commands.Add("Out-String")

    ' execute the script 
    Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

    ' close the runspace 
    MyRunSpace.Close()

    ' convert the script result into a single string 
    Dim MyStringBuilder As New StringBuilder()

    For Each obj As PSObject In results
        MyStringBuilder.AppendLine(obj.ToString())
    Next

    ' return the results of the script that has 
    ' now been converted to text 
    Return MyStringBuilder.ToString()

End Function
' helper method that takes your script path, loads up the script 
' into a variable, and passes the variable to the RunScript method 
' that will then execute the contents 
Private Function LoadScript(ByVal filename As String) As String

    Try

        ' Create an instance of StreamReader to read from our file. 
        ' The using statement also closes the StreamReader. 
        Dim sr As New StreamReader(filename)

        ' use a string builder to get all our lines from the file 
        Dim fileContents As New StringBuilder()

        ' string to hold the current line 
        Dim curLine As String = ""

        ' loop through our file and read each line into our 
        ' stringbuilder as we go along 
        Do
            ' read each line and MAKE SURE YOU ADD BACK THE 
            ' LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF 
            curLine = sr.ReadLine()
            If curLine.Contains("") Then
                fileContents.AppendLine("$Folder=tbpath.selectedpath")
            Else
                fileContents.Append(curLine + vbCrLf)
            If curLine.Contains ($Folder="") Then
        Loop Until curLine Is Nothing

        ' close our reader now that we are done 
        sr.Close()

        ' call RunScript and pass in our file contents 
        ' converted to a string 
        Return fileContents.ToString()

Sorry for the lengthy stuff, but the lines I am curious about are the curLine.Contains part. What I'm trying to do is have the parser detect whether or not the line is a specific one (which reads $Folder = "") and replace the empty quotes with a folder path that is stored in a text box (tbpath.selectedtext). Unfortunately, since the Powershell script requires quotes around the path string (in case there are spaces) I am having trouble figuring out how to do what I want it to do.

What should I do there? Should I build what I want into a new variable (maybe vbPath = tbpath.selectedtext) and put it into the new line? Are there any "best practices"?

È stato utile?

Soluzione

Double up the quotes

    Dim s As String = "$Folder = """""

s =

$Folder = ""

In your code

    If curLine.Contains("$Folder = """"") Then

    End If

Altri suggerimenti

Do you want curLine.Contains() to check for a literal string $Folder=""? If so, you need to put the whole string in double quotes and escape the inner ones. Double quotes are escaped by doubling them. Also, if you want to insert a variable, you need to concatenate it with the string literals, so the command should probably look like this:

If curLine.Contains ("$Folder = """"") Then
  fileContents.Append("$Folder = """ & tbpath.selectedpath & """")
End If
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top