0

i have a powershell script that i want to kick off with vb.net code, however when it gets to the part where i invoke the pipeline i get the error "Cannot process command because of one or more missing mandatory parameters: District County FMS." the powershell script is very simple, it takes 3 parameters and write-host's the param values back to the shell. pretty much my question is, how do i make it work? thank you everyone for you time.

vb.net(the other professionals i work with have boiled the error down to 'get-process' not being the right command for my scriptParams variable, but we're not sure which command to use)

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"
Andy Yau
  • 1
  • 1
  • 1

3 Answers3

0

These examples are in C#, but it's the same API.

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

More C# examples, but these are using the RunspaceFactory API you already have in your pipeline. Execute PowerShell Script from C# with Commandline Arguments

0

I'm new to PowerShell but may this help you

in your code

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

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

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

MyPipeline.Commands.Add(scriptParams)

I think this ADD new params to your script so value for already exit params not set

You need to pass param values to your script not add new param

E R
  • 480
  • 1
  • 8
  • 20
0

I know this is an old question but there was no good answer and I just spent too long getting this to work to not share my knowledge.

In the VB code, you'd remove the following:

MyPipeline.Commands.AddScript(scriptText)
MyPipeline.Commands.Add("Out-String")

Then, in place of the Out-String addition, add:

Dim myCommand As Command = New Command(scriptText, True)

' Add your parameters here.
' Obviously you can add variables instead of static strings if needed.
myCommand.Parameters.Add("District", "D1")
myCommand.Parameters.Add("County", "Lee")
myCommand.Parameters.Add("FMS", "101000")

' You can add switches in this manner as well. For example, a switch "I"
' You need to include the - with switches
myCommand.Parameters.Add("-i")

' Finally, apply your command object to the pipeline and proceed as normal    
MyPipeline.Commands.Add(myCommand)
superluminal
  • 101
  • 4