CLR error when creating Runspace in vb.net project in visual studio 2022

ABRAHAM OPOKU 0 Reputation points
2023-05-14T22:30:40.2766667+00:00

Hi all,

I am trying to run a simple ps from my vb.net project (visual studio 2022) but unfortunately Im receiving a CLR when I try to create Runspace....

my imports

Imports System.DirectoryServices
Imports System.IO
Imports Microsoft.VisualBasic.FileIO
Imports System.Collections.ObjectModel
Imports System.Management.Automation
Imports System.Management.Automation.Runspaces
Imports System.Text


Private Function RunScript(ByVal scriptText As String) As String
      


     
        Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
            MyRunSpace.Open()
            Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline
            MyPipeline.Commands.AddScript(scriptText)
            Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
            MyRunSpace.Close()


            Dim MyStringBuilder As New StringBuilder()
            For Each obj As PSObject In results
                MyStringBuilder.AppendLine(obj.ToString())
            Next

      
            
            Return MyStringBuilder.ToString()
           

            'End Try
        End Using
    End Function

Screenshot 2023-05-15 at 00.13.54

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,755 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,681 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,421 Reputation points
    2023-05-18T15:10:53.8233333+00:00

    Hello there,

    Runspaces.RunspaceFactory.CreateRunspace() For this case the solution was to edit the project file and make sure the reference to related asembly looks exactly so:

    <Reference Include="System.Management.Automation" />

    Similar discussion here https://social.msdn.microsoft.com/Forums/office/en-US/6d8fd57d-6d44-4b8e-a9e1-8b5b5ad17aab/runspacefactorycreaterunspace-causes-clr-error?forum=msbuild

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--


  2. Jiachen Li-MSFT 32,926 Reputation points Microsoft Vendor
    2023-05-22T09:35:26.4233333+00:00

    Hi @ABRAHAM OPOKU ,

    Make sure that the operating system version and hardware configuration of the virtual machine are compatible with the PowerShell version and related dependencies that you want to run.

    Supported versions of Windows

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.