vb.netコードで蹴りたいPowerShellスクリプトがありますが、パイプラインを呼び出す部分に到達するとエラーが発生します。「コマンドを処理できません。 1つまたは複数の欠落している必須パラメータのうち、地区郡FMS。 powershellスクリプトは非常に単純ですが、3つのパラメータとwrite-hostのパラメータ値をシェルに返します。かなり私の質問は、私はそれを動作させる方法ですか?みなさん、ありがとうございました。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"
なぜあなたは** get-process **を呼び出していますか? – ArcSet