2017-10-12 301 views
0

我有一个powershell脚本,我想用vb.net代码启动,但是当它到达我调用管道的部分时,出现错误“Can not process command because一个或多个缺失的强制性参数:区县FMS“。 powershell脚本非常简单,它需要3个参数,并将写入主机的参数值返回给shell。几乎我的问题是,我如何使它工作?感谢大家为你的时间。使用vb.net参数运行powershell脚本

vb.net(其他专业人士我煮完错误坐下来工作“得到处理”不是我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" 
+0

你为什么要调用** get-process **? – ArcSet

回答