简体   繁体   English

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

[英]running powershell script with parameters with vb.net

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." 我有一个Powershell脚本,我想用vb.net代码开始,但是当它到达我调用管道的部分时,出现错误“由于一个或多个缺少必需参数而无法处理命令:District County FMS。 ” the powershell script is very simple, it takes 3 parameters and write-host's the param values back to the shell. powershell脚本非常简单,它需要3个参数,并将写入主机的参数值返回给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) 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"

These examples are in C#, but it's the same API. 这些示例在C#中,但是使用相同的API。

https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/ 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. 更多C#示例,但这些示例使用的是管道中已有的RunspaceFactory API。 Execute PowerShell Script from C# with Commandline Arguments 使用命令行参数从C#执行PowerShell脚本

I'm new to PowerShell but may this help you 我是PowerShell的新手,但这可能对您有帮助

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 您需要将参数值传递给脚本,而不添加新的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM