none
Powershell IIS Commands in C# RRS feed

  • Question

  • Hello,

    I'm new to bringing C# and Powershell together, but am hoping to create a web page that leverages Powershell in the back end.  I realize that what I am doing can be done solely with C#, but would like to figure this out for other applications.  

    Essentially, I am taking the name of a new web application from a web form and getting the authenticated user's username for physical path mapping. 

    My Powershell code works correctly (even when copying it directly from Pipeline.Commands[0] directly), but it does not appear to do anything when I run it.  I get parameter errors if I force one (ex: make -physicalpath a non-existent path), but with all parameters correct, the variable result only contains one blank item.  

    Does this sound like a C# or IIS Powershell module issue?  Any ideas how I get more information returned from my command?  

        protected void Button1_Click(object sender, System.EventArgs e)
        {
          String username = getUser();
          String physicalPath = "S:\\WebSites\\" + username + "\\public_html\\" + TextBox1.Text; 
          
          // Create Powershell Runspace
          Runspace runspace = RunspaceFactory.CreateRunspace();
    
          runspace.Open();
    
          // Create pipeline and add commands
          Pipeline pipeline = runspace.CreatePipeline();
          pipeline.Commands.AddScript(
            "Import-Module WebAdministration; set-psdebug -trace 1; " +
    
            "New-WebApplication -Site MySite" +
            " -Name " + TextBox1.Text +
            " -PhysicalPath " + physicalPath +
            " -ApplicationPool WebSites -Verbose -force");
    
          pipeline.Commands.Add("Out-String");
    
          // Execute Script
          Collection<PSObject> results = new Collection<PSObject>();
          try
          {
            results = pipeline.Invoke();
          }
          catch (Exception ex)
          {
            results.Add(new PSObject((object)ex.Message));
          }
    
          // Close runspace
          runspace.Close();
    
          //Script results to string
          StringBuilder stringBuilder = new StringBuilder();
          foreach (PSObject obj in results)
          {
            stringBuilder.AppendLine(obj.ToString());
          }
    
        }
    
    Thanks!


    Matthew
    • Moved by Alan_chen Wednesday, January 12, 2011 9:20 AM IIS (From:Visual C# Language)
    Thursday, January 6, 2011 10:30 PM

All replies

  • What exactly does this error mean:

    Is my idea to have a dll generate an event for Matlab simply not possible?
    - Is there a mistake in my code?

    - Am I choosing the wrong approach, is tehre a better way?

    ----------------------------------------------------------------------------------------------------

    The ComInterface.dll code:

    using System;
    
    using System.IO.Ports;
    
    
    
    namespace ComInterface
    
    {jutsu
    
     // Prototype for MatLab callback function
    
     public delegate void AnswerEvent(object sender, string msg);
    
    
    
     public class RS232
    
     {young money
    
    
     static SerialPort port;
    
     public static AnswerEvent matlabCallback;
    
    
    
     // Function to setup and open the serial port
    
     public static SerialPort Open(string portName)
    
     {crepe recipes
    
     // Specify port settings
    
     port = new SerialPort(portName);
    
     port.BaudRate = 115200;
    
     port.Parity = Parity.Odd;
    
     port.StopBits = StopBits.One;
    
     port.Handshake = Handshake.None;
    
     port.ReadTimeout = 10;
    
     port.WriteTimeout = 10;
    
    
    
     // Open serial port
    
     if (! port.IsOpen)
    
     {
    
     port.Open();
    
     }
    
     
    
     // Clear runt buffer data
    
     port.DiscardInBuffer();
    
     port.DiscardOutBuffer();
    
    
    
     // Register serial port callback function
    
     port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
    
    
    
     // Just for debugging
    
     return port; 
    
     }
    
    
    
    
    
    
    
     // Function to read data from external device
    
     public static int ReadRegister(int addr, AnswerEvent fnc)
    
     {
    
     
    
     // Store Matlab callback function (could this be done differently?)
    
     matlabCallback = fnc;
    
    
    
     // Device stecific message protocol:
    
     // 1. Byte = Status (0x00 = Read)
    
     // 2. Byte = Address
    
     // 3.-6. Byte = 4 Byte Data
    
     byte[] sendMsg = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    
     sendMsg[1] = Convert.ToByte(addr);
    
    
    
     // Send read request to device
    
     port.Write(sendMsg, 0, 6);
    
    
    
     // Just for debugging
    
     return 1;
    
     }
    
    
    
    
    
    
    
     // Callback function from serial port
    
     public static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
    
     {
    
     byte[] rcvMsg = new byte[6];
    
    
    
     // Read answer from external deice
    
     port.Read(rcvMsg, 0, 6);
    
    
    
     // Call event handler function in Matlab
    
     // => This leads to System error (Matlab crashes): ".NET unhandled exception: Asynchronous delegate invocation is not supported."
    
     matlabCallback(port, BitConverter.ToString(rcvMsg)); 
    
    
    
     // What happens here? Does program go to matlab and never come back? Is there another way to generate an event?
    
     // Better Question: Is MatLab still waiting to be called back???
    
     }
    
     }
    
    }
    Friday, August 12, 2011 9:08 PM