VMware Cloud Community
idenjoe
Contributor
Contributor

Retrieve data from PowerCLI to Web

Hi,

For two weeks I have been looking for a way to retrieve information from PowerCLI to a web.Nothing found and wanted to know if it's possible in an easy way. I want to get some information from VMWare View into website and then run PowerCLI cmdlets according to some actions that users make on the app. Another problem i have is that the web is in another Linux machine than my VMWare View server so i have to do it remotely.

So, the question is, is possible to do what i'm searching for??

Regards.

Reply
0 Kudos
17 Replies
aerodevil
Hot Shot
Hot Shot

If you are looking to place data that you get from PowerCLI onto a web page there are a couple of ways you could consider doing it.

  1. Use ConverTo-HTML cmdlet.  Check out get-help  converto-html -full for usage and examples.
  2. You can always place the results into a .txt file and store it on a web server.  We use this method a lot and it allows you to later import that content pretty easily.  We came across a snippet that made getting info from a web page pretty easy.  I'll see if i can dig that up again and will post it.
Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
Reply
0 Kudos
CRad14
Hot Shot
Hot Shot

Full Help can also be found online....

http://technet.microsoft.com/en-us/library/dd347572.aspx

or from powershell

get-help convertto-html -online

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! :slightly_smiling_face:
Reply
0 Kudos
idenjoe
Contributor
Contributor

Hi,

Thanks to all. I'm trying to do something different now, because the convertto-html doesnt works for me. I'm trying to use convertto-csv to create a csv file in the local machine(Windows Server 2008). So i'm trying to do the next:

In a web server in the local machine (is only an example):

<?php

     shell_exec('powershell.exe -psc "console.psc1" -c Get-Pool ');

?>

So, this code should execute the command and return the pools in view server. If i launch this command from cmd(only 'powershell.exe -psc "console.psc1" -c Get-Pool'), it works fine, but if i try to do this trouhght php returns:

WARNING: The following errors occurred when loading console C:\scripts\miConsola.psc1: Cannot load Windows PowerShell snap-in VMware.View.Broker because of the following error: The Windows PowerShell snap-in 'VMware.View.Broker' is not installed on this machine.

Why is it happening???

Regards.

P.D: Console.psc1 is a console generated with Export-Console that opens a PowerCLI console instead of a PowerShell console.

Reply
0 Kudos
idenjoe
Contributor
Contributor

Hi,

I'm still unable to execute my PowerCLI scripts from PHP. If i execute my scripts from local machine by double-clicking, or launching them from cmd or powershell it works, but if i try to launch them from PHP with shell_exec() or exec() it doesn't works. So, what i'm suspecting is that if i try to launch them from PHP, the script will be launched with apache user permissions(www-data) and thats why powershell cannot execute fine them. The error is that powershell can not load the VMWare.View.Broker snapin into powershell so i can't use PowerCLI cmdlet's. Someone has any idea how can i solve this problem??

Regards.

P.D: I put the Add-PSSnapin in my scripts to try load the snapin but, powershell returns that this snapin is not installed.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Did you install the VMware View part that installs the VMWare.View.Broker snapin on the webserver where you run this script?

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
idenjoe
Contributor
Contributor

The webserver from where i execute my PowerCLI scripts is the same as the View Server.

Regards.

P.D: If i try to launch Powershell scripts (cmdlets that are strictly for powershell) works fine, the problem is with PowerCLI.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Did you upgrade View from v4.5 to v4.6? Then you should check Running the Add-PSSnapin VMware.View.Broker PowerShell command fails after upgrading to View 4.6. You then need to load the View cmdlets by calling the add-snapin.ps1 script.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
idenjoe
Contributor
Contributor

I didn't upgraded. I installed 4.6 directly. In spite of this, i'm putting the Add-PSSnapin VMWare.View.Broker in the first line of my scripts. In fact, is this line which causes the error.... Powershell can't load the Snapin if the script is launched from PHP with exec or shell_exec, but it works if i launch the script directly double-clicking....It's crazy.

Regards.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Can you try what happens if you change the Add-PSSnapin command into a call of the add-snapin.ps1 script as described in th KB article?

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
idenjoe
Contributor
Contributor

OK. This has been useful. Now i can load the snapin, but still can't execute PowerCLI cmdlets. What i have now is a php file with the next sentence:

     echo shell_exec('powershell -c "C:\scripts\myScript.ps1"');

And the script contains this:

    . /PathToPowerCLISnapin

     Get-Pool | Out-File "C:\scripts\myFile.txt"

This should create a file called myFile.txt which should have the content returned by Get-Pool cmdlet, but it doesn't. This cmdlet is not executed... Why??

Regards.


Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

What happens when you run the 'powershell -c "C:\scripts\myScript.ps1"' command from a command prompt on the webserver?

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
idenjoe
Contributor
Contributor

It works fine if i do it from command prompt, that what's strange...

Reply
0 Kudos
Zsoldier
Expert
Expert

Does the php page run under the context of a system account or does it impersonate?

Is ExecutionPolicy set to remotesigned or other less restrictive policy?

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
idenjoe
Contributor
Contributor

The executionPolicy is set to ByPass(I know that it is dangerous but it is for testing why the error is happening..). The php page is a simple page under the htdocs in apache local server only for testing if i can launch powershell scripts in php, so it doesn't run under a system account, but one of the things i doubt is that if i use the exec or shell_exec from php, it will launch the command with the apache user privileges (www-data normally), so i'm not really sure if this is causing the problem.

Regards.

Reply
0 Kudos
Zsoldier
Expert
Expert

I believe the execution policy is per user.  So if it runs in context of the apache account, that account would have to set it's execution policy.

The other thing is whether or not it's running in context of on the server or your local system.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
Reply
0 Kudos
idenjoe
Contributor
Contributor

Hi,

Finally i could execute a the powerCLi script with PHP, but now i have another problem(sorry...). When it executes, doesn't finsh until i close the ssh session i have opened previously. The code is this: 

                                                                                                                                                                        
<?php                                                                                                                                                                     
                                                                                                                                                                          
        if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");                                                                                 
        // log in at server1.example.com on port 22                                                                                                                       
        if(!($con = ssh2_connect($IP,22))){                                                                                                                  
            echo "fail: unable to establish connection\n";                                                                                                                
        } else {                                                                                                                                                          
            // try to authenticate with username root, password secretpassword                                                                                            
            if(!ssh2_auth_password($con, "myUser", "pass")) {                                                                                 
                echo "fail: unable to authenticate\n";                                                                                                                    
            } else {                                                                                                                                                      
                // allright, we're in!                                                                                                                                    
                echo "okay: logged in...";                                                                                                                                
                echo "<br>";                                                                                                                                              
                                                                                                                                                                          
                // execute a command                                                                                                                                      
                if (!($stream = ssh2_exec($con,'powershell "C:\scripts\pruebaPowerCLI.ps1"'))) {                                                                          
                    echo "Error: imposible ejecutar comando";                                                                                                             
                        echo "<br>";                                                                                                                                      
                } else {                                                                                                                                                  
                    // collect returning data from command                                                                                                                
                        echo "El comando se ha ejecutado <br>";                                                                                                           
                        stream_set_blocking($stream,true);                                                                                                                
                        $line = stream_get_contents($stream);                                                                                                             
                                                                                                                                                                          
                        echo "Salida estandar: " . $line . "<br>";                                                                                                        
                    fclose($stream);                                                                                                                                      
                }                                                                                                                                                         
            }                                                                                                                                                             
        }                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                
?>

It create a file in the windows machine, so i know my script has been succesfuly executed, but my php page is still waiting for return and only finishes when i force close session from the remote ssh server.
Regards.
Reply
0 Kudos
idenjoe
Contributor
Contributor

Hi. I have resolved the problem. I have added after my command an echo statemnet with some string and capture the output and when i see this string i can stop reading.

Thanks to all.

Reply
0 Kudos