I am trying to write a program to browse to a WSP file and then run the STSADM to make it easier to add solutions to our sharepoint site. The STSADMN is located in c:\program files\common files\microsoft shared\web server extensions\14\bin\stsadmn.exe.

stsadm -o addsolution -filename "filename that was selected"

This currently has to run in powershell.

After that completes I need it to run this:

stsadm -o deploysolution -name "selected WSP" -allowgacdeployment -immediate

I am just not sure how to call powershell and then run this command. and of course a messagebox.show "Completed". All I have right now is a form that browses to a WSP

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Open WSP File";
            fDialog.Filter = "WSP Files|*.wsp";
            fDialog.InitialDirectory = @"C:\";
            if(fDialog.ShowDialog() == DialogResult.OK) 
            {

            MessageBox.Show(fDialog.FileName.ToString());

            }




        }
    }
}

I got this far as figuring out that I need system.Diagnostics reference.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace callprogram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "powershell.exe";
            p.Start();
            p.WaitForExit();
            string output = p.StandardOutput.ReadToEnd();
            return;
 
        }
    }
}

Powershell actually not executes but of course I need to run the commands in the push of a button. I am new to C# and still learning.

so if I make a script, how do I accomplish the above with it?

Get your program to dynamically create the script before calling it.

Can you give me some sample code? I am no c# expert

There is an example right on the page I linked.

I see that example, but how would my code look? Again I am new so I have no clue

I see that example, but how would my code look? Again I am new so I have no clue

Error 1 The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?) \\computer\users\user\documents\visual studio 2010\Projects\powershellsts\powershellsts\Form1.cs 10 25 powershellsts

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

namespace powershellsts
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

I created a new project. Did I need to install something?

Well I can get that powersell gui to work but it's not even close what I am trying to do. How do I combine the buttons I want with the commands I need? I'm lost.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HowToRunPowerShell
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FormPowerShellSample());
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.