CLR error when creating Runspace in project in visual studio 2022

ABRAHAM OPOKU 0 Reputation points

Hi all,

I am trying to run a simple ps from my 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()
            Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline
            Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

            Dim MyStringBuilder As New StringBuilder()
            For Each obj As PSObject In results

            Return MyStringBuilder.ToString()

            'End Try
        End Using
    End Function

Screenshot 2023-05-15 at 00.13.54

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

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,046 Reputation points

    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

    Hope this resolves your Query !!

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

  2. Jiachen Li-MSFT 27,581 Reputation points Microsoft Vendor


    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