none
get-vmnetworkadapterextendedacl cmdlet in php script RRS feed

  • Question

  • Hi friends,

    I need to call get-vmnetworkadapterextendedacl command from PHP page. so, with the help of http://theboywonder.co.uk/2012/07/29/executing-powershell-using-php-and-iis/ web page i wrote following 2 scripts.

    PHP page (index.php)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Testing PowerShell</title>
    </head>
    <body>
    <?php
     
    // If there was no submit variable passed to the script (i.e. user has visited the page without clicking submit), display the form:
    if(!isset($_POST["submit"]))
    {
        ?>
        <form name="testForm" id="testForm" action="index.php" method="post" />
            Your name: <input type="text" name="username" id="username" maxlength="20" /><br />
            <input type="submit" name="submit" id="submit" value="Do stuff" />
        </form>
        <?php   
    }
    // Else if submit was pressed, check if all of the required variables have a value:
    elseif((isset($_POST["submit"])) && (!empty($_POST["username"])))
    {
        // Get the variables submitted by POST in order to pass them to the PowerShell script:
        $username = $_POST["username"];
        // Best practice tip: We run out POST data through a custom regex function to clean any unwanted characters, e.g.:
        // $username = cleanData($_POST["username"]);
             
        // Path to the PowerShell script. Remember double backslashes:
        $psScriptPath = "F:\\webbased\\TheSite\\get-list.ps1";
     
        // Execute the PowerShell script, passing the parameters:
     $query = shell_exec("powershell -command $psScriptPath -username '$username'< NUL");
    
      echo $query;  
    
    }
    // Else the user hit submit without all required fields being filled out:
    else
    {
        echo "Sorry, you did not complete all required fields. Please go back and try again.";
    }
    ?>
    </body>
    </html>

    Get-list.ps1 script



    param(
    [string]$username
    )
    
    # Increase buffer width/height to avoid PowerShell from wrapping the text before
    # sending it back to PHP (this results in weird spaces).
    $pshost = Get-Host
    $pswindow = $pshost.ui.rawui
    $newsize = $pswindow.buffersize
    #$newsize.height = 30000
    #$newsize.width = 40000
    $pswindow.buffersize = $newsize
     
    
    Write-Output "Hello $username <br />"
     
    # Get a list of running existing ACL:
    $processes = get-vmnetworkadapterextendedacl
    
    # Write them out into a table with the columns you desire:
    Write-Output "<table>"
    Write-Output "<thead>"
    Write-Output "  <tr>"
    Write-Output "      <th>ParentAdapter</th>"
    Write-Output "      <th>Direction</th>"
    Write-Output "      <th>Action</th>"
    #Write-Output "      <th>LocalIPAddress</th>"
    #Write-Output "      <th>RemoteIPAddress</th>"
    #Write-Output "      <th>LocalPort</th>"
    #Write-Output "      <th>RemotePort</th>"
    #Write-Output "      <th>Protocol</th>"
    #Write-Output "      <th>Weight</th>"
    #Write-Output "      <th>Stateful</th>"
    
    Write-Output "  </tr>"
    Write-Output "</thead>"
    Write-Output "<tfoot>"
    Write-Output "  <tr>"
    Write-Output "      <td>&nbsp;</td>"
    Write-Output "      <td>&nbsp;</td>"
    Write-Output "      <td>&nbsp;</td>"
    #Write-Output "      <td>&nbsp;</td>"
    #Write-Output "      <td>&nbsp;</td>"
    #Write-Output "      <td>&nbsp;</td>"
    #Write-Output "      <td>&nbsp;</td>"
    #Write-Output "      <td>&nbsp;</td>"
    #Write-Output "      <td>&nbsp;</td>"
    #Write-Output "      <td>&nbsp;</td>"
    Write-Output "  </tr>"
    Write-Output "</tfoot>"
    Write-Output "<tbody>"
    foreach($process in $processes)
    {
    Write-Output "  <tr>"
    Write-Output "      <td>$($process.ParentAdapter)</td>"
    Write-Output "      <td>$($process.Direction)</td>"
    Write-Output "      <td>$($process.Action)</td>"
    #Write-Output "      <td>$($process.LocalIPAddress)</td>"
    #Write-Output "      <td>$($process.RemoteIPAddress)</td>"
    #Write-Output "      <td>$($process.LocalPort)</td>"
    #Write-Output "      <td>$($process.RemotePort)</td>"
    #Write-Output "      <td>$($process.Protocol)</td>"
    #Write-Output "      <td>$($process.Weight)</td>"
    #Write-Output "      <td>$($process.Stateful)</td>"
    Write-Output "  </tr>"
    }
    Write-Output "</tbody>"
    Write-Output "</table>"
    #*=============================================================================
    #* END SCRIPT BODY
    #*=============================================================================
     
    #*=============================================================================
    #* END OF SCRIPT
    #*=============================================================================

    if i execute this powershell script in powershell ISE, it will correctly gives the out put in html table but not in PHP page. most weird issue is if i replace different powershell cmdlet instead of get-vmnetworkadapterextendedacl (eg. Get-process) then the output is correctly displaying in php page. I used php 5.5 installed by MS WPI on windows server 2012 R2

    1. Where does i miss the point ?

    2. Do we need additional security config to execute get-vmnetworkadapterextendedacl cmdlet ?

    Its a big help experts...

    Regards

    Gayan


    Gayan attygala

    Monday, May 19, 2014 7:45 PM

All replies

  • I don't know if this has anything to do with why the output isn't correct, but you can more easily convert the data to HTML like this:

    Write-Output (get-vmnetworkadapterextendedacl | select ParentAdapter,Direction,Action,LocalIPAddress,RemoteIPAddress,LocalPort,RemotePort,Protocol,Weight,Stateful | ConvertTo-HTML)

    Using that line will eliminate everything from $processes = to the end of the script.


    I hope this post has helped!

    Monday, May 19, 2014 8:19 PM
  • Hi

    Appreciate your reply. I also tried with convertto-html cmdlet  but .... same result. This error happens only for get-vmnetworkadapter* cmdlets.

    Regards

    Gayan


    Gayan attygala


    • Edited by gayanati Tuesday, May 20, 2014 11:36 AM
    Tuesday, May 20, 2014 11:21 AM