alanrenouf / vCheck-vSphere

vCheck Daily Report for vSphere
MIT License
733 stars 325 forks source link

MVC .Net and vCheck codebase integration issue #706

Closed mohammedamirk closed 4 years ago

mohammedamirk commented 4 years ago

Hi all, Iam facing an issue while integrating the MVC based asp.net application with the vSphere vCheck script Codebase. While executing the code from my controller in asp.net application iam able to generate the html output successfully from the code but unable to connect and fetch the details of my vCenter server using PowerCLI code.

MVC Controller code: try { TempData["Result"] = ""; // Clean the Result TextBox string ResultBox = string.Empty;

        // Initialize PowerShell engine
        var shell = PowerShell.Create();

        // Add the script to the PowerShell object
        shell.Commands.AddScript("C:\\Users\\Administrator\\source\\repos\\HCLAutomation\\AutomationWeb\\vCheck-vSphere-master\\vCheck.ps1");
        // Execute the script
        var results = shell.Invoke();

        // display results, with BaseObject converted to string
        // Note : use |out-string for console-like output
        if (results.Count > 0)
        {
            // We use a string builder ton create our result text
            var builder = new StringBuilder();

            foreach (var psObject in results)
            {
                // Convert the Base Object to a string and append it to the string builder.
                // Add \r\n for line breaks
                builder.Append(psObject.BaseObject.ToString() + "\r\n");
            }

            // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
            ResultBox = Server.HtmlEncode(builder.ToString());
            TempData["Result"] = ResultBox;
        }

        return RedirectToAction("SddcAdmin");
    }

    catch (System.Exception)
    {

        throw;
    }

================================================== Ouput: vcheck output

Iam able to generate the html output successfully from the code but unable to connect and fetch the details of my vCenter server using PowerCLI code. Looking for a possible solution on this.

lucdekens commented 4 years ago

Can you expand a bit more on what you mean by

... unable to connect and fetch the details of my vCenter server using PowerCLI code.

mohammedamirk commented 4 years ago

When i tried to run the vCheck script from the MVC.net code i can generate the HTML output but unable to get the correct output as you can see the output shows (No of Host-1, No of VMs-1). It seems my script is not fetching the VI-Server details

lucdekens commented 4 years ago

Did you try a run of vCheck.ps1 from a PowerShell prompt? To make sure the settings are correct. Does that produce a report that is correct?

Also, can you check the output of the vCheck script (stdout, stderr and verbose). See Get Powershell command's output when invoked through code

mohammedamirk commented 4 years ago

Yes. I tried running the vCheck.ps1 from the Powershell prompt and it is giving me the correct output. However while executing it from MVC code iam not getting the output as expected

lucdekens commented 4 years ago

Can you attach the output of the script when you run it from MVC?

mohammedamirk commented 4 years ago

vcheck output Here is the output

lucdekens commented 4 years ago

That is not what I mean. The vCheck.ps1 script produces input to stdout and possible stderr. This appears on the PS console when running directly from a PS session. That output, when running from your code, is what I would like to see.

mohammedamirk commented 4 years ago

Hi lucd 1) Attached here is output of the PS console when running directly from a PS session 2) Attached screenshot of the PSSession Html output

vcheck-PS Session html output vcheck PSSession output.docx

Please let me know if this is exactly what your are looking for?

lucdekens commented 4 years ago

I don't understand why you get the error on the Connect-VIServer cmdlet and still the script is able to produce a result. That could mean that there is an open connection (check with $global:defaultviservers).

But I would really need to see this same output when you run the script from your code, not from a PS console.

mohammedamirk commented 4 years ago

Yes. It seems there is an open connection

$OpenConnection = $global:DefaultVIServers | Where-Object { $_.Name -eq $VIServer } if($OpenConnection.IsConnected) { Write-CustomOut ( "{0}: {1}" -f $pLang.connReuse, $Server ) $VIConnection = $OpenConnection } else { Write-CustomOut ( "{0}: {1}" -f $pLang.connOpen, $Server )

$VIConnection = Connect-VIServer -Server $VIServer -Port $Port

$VIConnection = Connect-VIServer $VIServer –user “*****” –Pass “****” –Protocol Https }

Iam facing this issue while executing from MVC asp.net app.

lucdekens commented 4 years ago

That is why I would like to see the output from vCheck.ps1 when it runs from your code.

mohammedamirk commented 4 years ago

Hi lucd, Iam sharing you the steps recorded while debugging the vcheck scripts from my MVC .net code I would appreciate if you could please check and share ur inputs. MVC .net Integration issue rcd.zip

mohammedamirk commented 4 years ago

Hi lucd, Did u get any chance to look into this issue.

lucdekens commented 4 years ago

I did, but I'm afraid I don't see any obvious cause for what you are seeing. But then again, I'm not a developer.

What I could suggest is to try the same but with a simple PowerCLI script. Just a Connect-VIServer and for example a Get-VM. Then save the output to a CSV file.

See if that runs ok.

If it does, gradually make the script more complex.

mohammedamirk commented 4 years ago

Hi Lucd, I resolve the issue. Now the vcheck code is sucessfully integrated with MVC.Net code

lucdekens commented 4 years ago

Would you mind sharing your solution?

mohammedamirk commented 4 years ago

Sure. I figure out that my MVC.Net code is correct and there was some error due to multiple versions of powershell installed. So First I uninstall the powershell versions and reinstall again. It seems some execution policy was missing and self-sign certificates due to which it was not fetch the powerCLI modules. So Install a fresh powerCLI: Step-1: Install-Module -Name VMware.PowerCLI Step-2: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned Step-3: Import-Module VMware.VimAutomation.Core Step-4: Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false Step-5: Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false Step-6: Update-Module -Name VMware.PowerCLI Then try running it from my .net code. IT WORKS :)