1

我正在开发一个实用程序,它将获取 .ps1 脚本 (Powershell) 的内容,并在 VB.NET Winform 的上下文中运行它。基本上,它通过引用一些 Powershell DLL、打开 .ps1 文件、读取一行文本并将其输入到将在按钮单击事件上运行的内部字符串来实现这一点。

这是我到目前为止的完整代码:

    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()

对不起,冗长的东西,但我很好奇的台词是curLine.Contains部分。我要做的是让解析器检测该行是否是特定行(读取$Folder = "")并将空引号替换为存储在文本框(tbpath.selectedtext)中的文件夹路径。不幸的是,由于 Powershell 脚本需要在路径字符串周围加上引号(如果有空格),我无法弄清楚如何做我想要它做的事情。

我应该在那里做什么?我应该将我想要的东西构建到一个新变量(也许vbPath = tbpath.selectedtext)中并将其放入新行吗?有没有“最佳实践”?

4

2 回答 2

3

双引号

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

小号 =

$文件夹 = ""

在您的代码中

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

    End If
于 2013-05-20T16:51:11.977 回答
1

您要curLine.Contains()检查文字字符串$Folder=""吗?如果是这样,您需要将整个字符串放在双引号中并转义内部的。双引号通过将它们加倍来进行转义。另外,如果你想插入一个变量,你需要将它与字符串文字连接起来,所以命令应该看起来像这样:

If curLine.Contains ("$Folder = """"") Then
  fileContents.Append("$Folder = """ & tbpath.selectedpath & """")
End If
于 2013-05-20T16:51:26.020 回答