Hi, I may be posting this in the wrong forum so I apologize, please move it to the appropriate forum or let me know and I'll post there.
A couple of months ago one of the Platform guys I work with sent me an IM asking if there was a way to run powershell inside a webpage. After a bit of googling I found that you could indeed load powershell into a webapge and make it do things. Now the reason he asked me this is because I do nearly everything possible in Powershell, so he figured I was a good resource. So why did he ask?
We manage roughly 1100 vm's and w're anticpiating onboarding about 600-700 more over the next few months. They want a method by which the average tech could easily create a VM in the appropriate cluster with the proper settings. The idea is that the user would pick from a list of "Golden Images" (pre-configured vm's) and then apply a specific customization file to it. We worked out a rather lengthy script that works perfectly well for both the linux and windows vm's.
Once we had a functional script worked out I started working that into a webpage. I reference the powershell sdk so i can create a powershell instance, then i add-psnappin for the powercli stuff. From there I populate the script with entries from various textboxes and dropdowns. During testing this appeared to work flawlessly. My test environment is a windows 8 laptop with vs2012 loaded, so the page is loaded up in iis express 8.
Once we uploaded that to the server, i had to install the powershell sdk and the powercli on the server first. it seemed to work, now all of his work is in linux so he tested it several different ways, handed it off to one of the linux guys he works with until we got all the kinks worked out. when we passed it over to the windows guys that's where things seemed to go wrong. At the moment I have a fully functional web app that will happily build you linux vm's all the live long day, but on the windows side i routinely get stuck on the unattend portion. it fails every single time,this got me thinking that perhaps there was some difference between iis express and iis but thus far i've been unable to find anything.
So then i started thinking that perhaps attempting to run this through in essence two layers, powershell and powercli was a bit much. So i started to mess around with vmware.vim, this seems to me to be how the powercli works. i've found several very wonderful articles and this one today was especially helpful (http://velemental.com/2012/03/09/a-deep-dive-doing-it-the-manual-way-with-vmware-vim-and-powershell/) I've been going through and reworking his powershell code into csharp, which on the whole isn't too hard, but i've come across my first stumbling block.
this one line of code is quite angry
vimClient.VimService.RetrieveProperties(vimServiceContent.propertyCollector,pfs);
according to vs2012, RetrieveProperties is not a valid method. here is the error message below
I have tried referencing the vmware-vim.dll that is available in the GAC as well as the one found in Program Files (x86) to no avail. Now the reason this bothers me is because it's obviously there as the powershell code in the article above works flawlessly, and when i access https://server/sdk/vim.wsdl i see information for retrieveproperties.
i was hoping that i could keep this relatively simple without having to go the sdk, if someone can help me out with either the unattend error, which doesn't actually appear to be an error. or the retrieveproperties error i would really appreciate it. I'll include the code i have below so you can load it up and look at it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VMware.Vim;
public partial class _Default : System.Web.UI.Page
{
VimClient vimClient = new VimClient(); ServiceContent vimServiceContent = new ServiceContent(); protected void Page_Load(object sender, EventArgs e) { } protected void cmdConnect_Click(object sender, EventArgs e) { string sViServer = txtViServer.Text.ToString(); string sUsername = txtUsername.Text.ToString(); string sPassword = txtPassword.Text.ToString(); // // Connect to vmware sdk service // vimClient.Connect("https://" + sViServer + "/sdk"); vimClient.Login(sUsername, sPassword); vimServiceContent = vimClient.ServiceContent; // // Get a list of datacenters // object DataCenter = getFolder(new string[] {"childEntity"}, "Folder", vimServiceContent.RootFolder.Type, vimServiceContent.RootFolder.Value); } protected object getFolder(string[] pathSet, string psType, string morefType, string morefValue) { PropertySpec[] ps = new PropertySpec[1]; ps[0].Type = psType; ps[0].PathSet = pathSet; ObjectSpec[] os = new ObjectSpec[1]; os[0].Obj.Type = morefType; os[0].Obj.Value = morefValue; PropertyFilterSpec[] pfs = new PropertyFilterSpec[1]; pfs[0].PropSet = ps; pfs[0].ObjectSet = os; ObjectContent[] vmFoldersObjectContent = new ObjectContent[1]; vimClient.VimService.RetrieveProperties(vimServiceContent.propertyCollector,pfs); return vmFoldersObjectContent[0].PropSet[0].Val; }
}