私の目標は、vb.netアプリケーション内でPowerShellコマンドを実行することです。calc
PowerShell内でコマンドを実行しようとしています。
次のシステムがインポートされます。
Imports System.IO
Imports System.Collections.ObjectModel
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Imports System.Text
RunScript
PowerShellコマンドを実行する関数を作成しました。
Public Function RunScript(ByVal script As String) As String
Dim runspace As Runspace = RunspaceFactory.CreateRunspace()
runspace.Open()
Dim pipeline As Pipeline = Runspace.CreatePipeline()
pipeline.Commands.AddScript(script)
pipeline.Commands.Add("Out-String")
Dim results As Collection(Of PSObject) = pipeline.Invoke()
Runspace.Close()
Dim stringbuilder As StringBuilder = New StringBuilder()
For Each ps As PSObject In results
stringbuilder.AppendLine(ps.ToString())
Next
Return stringbuilder.ToString()
End Function
私が使用しているNuGetパッケージは次のとおりです:System.Management.Automation.dll(と混同しないでください System.Management.Automation
)
この関数を使用してPowerShellコマンドを実行する方法(calc
このインスタンスで実行:)
txtOutput.Text = RunScript("calc")
(注:txtOutput
PowerShell内で実行されたときのコマンドの出力を含むテキストボックスです)
ソリューションをビルドする前にエラーは表示されませんが、ソリューションをビルドするときに、次のエラーがコンソールに記録されました。
エラーBC30002:タイプ「Runspace」が定義されていません。
エラーBC30451: 'RunspaceFactory'が宣言されていません。保護レベルが原因でアクセスできない場合があります。
エラーBC30002:タイプ「パイプライン」が定義されていません。
エラーBC30002:タイプ「PSObject」が定義されていません。
エラーBC30002:タイプ「PSObject」が定義されていません。
VBでPowerShellスクリプトを実行する方法は次のとおりです。
Imports System.Management.Automation.Runspaces
''' <summary>
''' Allows running PowerShell snippets/scripts/commands.
''' </summary>
''' <param name="scriptText">
''' The PowerShell command to send.
''' </param>
''' <returns>
''' The result of the command as a string.
''' </returns>
''' <remarks>
''' Requires: reference to System.Management.Automation.dll, and Imports System.Collections.ObjectModel
''' </remarks>
Public Function RunPSScript(ByVal scriptText As String) As String
' Create a Powershell runspace
Dim pShellRunSpace As Runspace = RunspaceFactory.CreateRunspace()
' Open it
pShellRunSpace.Open()
' Create a pipeline and feed it the script text
Dim pShellPipeline As Pipeline = pShellRunSpace.CreatePipeline()
pShellPipeline.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.
pShellPipeline.Commands.Add("Out-String")
' Execute the script
Dim pShellResults As Collection(Of PSObject) = pShellPipeline.Invoke()
' Close the runspace
pShellRunSpace.Close()
' Convert the script result into a single string
Dim sb As New StringBuilder()
For Each obj As PSObject In pShellResults
sb.AppendLine(obj.ToString())
Next
' Return the results of the script that has now been converted to text
Return sb.ToString()
End Function
また、PowerShellが処理を完了するのを待つ(つまり、渡すtnc google.com -port 8080
)だけでも時間がかかり、UIが保持されることがよくあるので、BackgroundWorkerなどに呼び出しを渡して処理します。
また、このスレッドが役立つ場合があります。変数が宣言されていないため、保護レベルが原因でアクセスできない可能性があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加