Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to load a .NET assemblies in my Powershell script

$
0
0

Hi,

I'm trying to use the C# code snippets directly in Powershell. The issue which I'm facing at present is a run time error: "Cannot add type. One or more required assemblies are missing" in the statement " Add-Type -AssemblyName 'System.IO'"

What I'm trying to achieve here is to start a NamedPipeServer using Powershell, which is being coded in C#.

Here goes the way I'm trying to use it:

$Source = @"

public class PipeServer
{
    private static int numThreads = 1;

    public static void startingPoint()
    {
        int i;
        Thread[] servers = new Thread[numThreads];

        Console.WriteLine("Waiting for moderator to connect...\n");

        for (i = 0; i < numThreads; i++)
        {
            servers[i] = new Thread(ServerThread);
            servers[i].Start();
        }
        Thread.Sleep(250);

        /*while (i > 0)
        {
            for (int j = 0; j < numThreads; j++)
			{
                if (servers[j] != null)
                {
                    if (servers[j].Join(250))
                    {
                        Console.WriteLine("Server thread[{0}] finished.", servers[j].ManagedThreadId);
                        servers[j] = null;
                        i--;    // decrement the thread watch count
                    }
                }
            }
        }*/
        //Console.WriteLine("\nServer threads exhausted, exiting.");
    }

    private static void ServerThread(object data)
    {
        NamedPipeServerStream server =
            new NamedPipeServerStream("moderatorpipe", PipeDirection.InOut, numThreads);

        int threadId = Thread.CurrentThread.ManagedThreadId;

        // Wait for a client to connect
        server.WaitForConnection();


        while (true)
        {
            Console.WriteLine("Moderator connected on thread[{0}].", threadId);
            try
            {
                // Read the request from the client. Once the client has 
                // written to the pipe its security token will be available.

                var br = new BinaryReader(server);
                var bw = new BinaryWriter(server);

                // Verify our identity to the connected client using a 
                // string that the client anticipates.

                var len = (int)br.ReadUInt32();                 // Read string length
                var str = new string(br.ReadChars(len));        // Read string

                Console.WriteLine("Read: \"{0}\"", str);

                //str = new string(str.Reverse().ToArray());    // Aravind's edit: since Reverse() is not working, might require some import. Felt it as irrelevant

                var strTobePassed = "Ready to start the \"{0}\" unittest";
                //var buf = Encoding.ASCII.GetBytes(str);       // Get ASCII byte array
                var buf = Encoding.ASCII.GetBytes(strTobePassed);
                bw.Write((uint)buf.Length);                     // Write string length
                bw.Write(buf);                                  // Write string
                Console.WriteLine("Wrote: \"{0}\"", strTobePassed);
            }
            // Catch the IOException that is raised if the pipe is broken
            // or disconnected.
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
            server.Flush();
        }
    }
}"@


// For this C# code, I have to use System, System.IO, System.IO.Pipes, System.Text and System.Threading namespaces. // So for that to work in Powershell I tried to add the assemblies here like:

Add-Type -AssemblyName 'System', 'System.IO', 'System.IO.Pipes', 'System.Text', 'System.Threading'


which gives me this errror : "Cannot add type. One or more required assemblies are missing"

Thanks,

Aravind


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>