VMware Cloud Community
Daithi_N
Enthusiast
Enthusiast
Jump to solution

PowerCLI within APS.NET

Hi,

need some help with regards to calling powercli from the ASP.NET C#

Even if i do a Get-VM nothing shows: p

        protected void Button1_Click(object sender, EventArgs e)

        {

            var shell = PowerShell.Create();

            string vmware = "";

            string impcred = "C:\\user\\demo\\desktop\\demo.cred";

            shell.Commands.AddScript("$creds = import-clixml -Path" + impcred + "| Connect-viserver -server" + vmware + "-Credential $creds | Remove-VM testimage -DeletePermanently | out-string");

            var results = shell.Invoke();

            if (results.Count > 0)

            {

                var builder = new StringBuilder();

                foreach (var psObject in results)

                {

                    builder.Append(psObject.BaseObject.ToString() + "\r\n");

                }

                ResultBox.Text = Server.HtmlEncode(builder.ToString());

             

}       

    }

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
evilensky
Enthusiast
Enthusiast
Jump to solution

Hi Daithi,

Have you looked into using the .net assemblies directly from within C#?  For example: https://stackoverflow.com/a/5493969

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Can you run a regular PowerShell cmdlet in your setup?
For example Get-Process.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Daithi_N
Enthusiast
Enthusiast
Jump to solution

Hi Luc, Yes I am able to get Powershell commands working. I.E dir C:\ or Get-Process which displays the results to the TextArea.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm definitely not a C# specialist, but why are you using the pipeline symbol (|) between the commands instead of the semi-column (;)?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

evilensky
Enthusiast
Enthusiast
Jump to solution

Hi Daithi,

Have you looked into using the .net assemblies directly from within C#?  For example: https://stackoverflow.com/a/5493969