使用vb.net运行带有参数的powershell脚本

时间:2017-10-12 15:36:50

标签: vb.net powershell

我有一个powershell脚本,我想用vb.net代码开始,但是当它到达我调用管道的部分时,我得到错误“由于一个或多个缺少必需参数而无法处理命令:区郡FMS。“ powershell脚本非常简单,它需要3个参数,并且将主机的param值写回shell。几乎我的问题是,我如何使它工作?谢谢大家的时间。

vb.net(我使用的其他专业人员将错误归结为'get-process'不是我的scriptParams变量的正确命令,但我们不确定使用哪个命令)

Sub Main()
    Console.WriteLine(RunScript(LoadScript(Directory.GetCurrentDirectory() + "/CreateProject.ps1 ")))
    Console.ReadLine()
End Sub
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")

    '[ay]create a command object by bassing the command to the constructor
    Dim scriptParams As New Command("get-process")
    '[ay]pass parameters to the command
    scriptParams.Parameters.Add("District", "D1")
    scriptParams.Parameters.Add("County", "Lee")
    scriptParams.Parameters.Add("FMS", "101000")
    MyPipeline.Commands.Add(scriptParams)

    ' 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

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()
            fileContents.Append(curLine + vbCrLf)
        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()

    Catch e As Exception
        ' Let the user know what went wrong. 
        Dim errorText As String = "The file could not be read:"
        errorText += e.Message + "\n"
        Return errorText
    End Try

End Function

PowerShell的

    [CmdletBinding()]

   Param(
   [Parameter(Mandatory = $True)]
   [string]$District, 
   [Parameter(Mandatory = $True)]
   [string]$County,
   [Parameter(Mandatory = $True)] 
   [string]$FMS 

   )

Write-Host "District: $Dsistrict"
Write-Host "County: $County"
Write-Host "FMS: $FMS"

2 个答案:

答案 0 :(得分:0)

这些示例在C#中,但它是相同的API。

https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/

更多C#示例,但这些示例使用您已在管道中使用的RunspaceFactory API。 Execute PowerShell Script from C# with Commandline Arguments

答案 1 :(得分:0)

我是PowerShell的新手,但这可能对您有所帮助

代码

scriptParams.Parameters.Add("District", "D1")

scriptParams.Parameters.Add("County", "Lee")

scriptParams.Parameters.Add("FMS", "101000")

MyPipeline.Commands.Add(scriptParams)

我认为这会为您的脚本添加新的参数,因此已经退出的参数值未设置

您需要将param值传递给脚本而不添加新参数