curmudgeonly-grump Posted September 15, 2014 Share Posted September 15, 2014 Hi I am trying to pass a csv file into a powershell script and then return the output and display it to the user. The file is a firewall checker. Any advice or solutions on where I am going wrong would be most appreciative, Thanks. <?php //phpinfo(); ini_set('display_errors', 'On'); error_reporting(E_ALL);//E_ALL echo '<html> <style> body { font-family:Calibri,Helvetica,sans-serif; font-size:100%; } </style> <head> <link rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript"> function upload(){ String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); }; var textAreaValue=document.getElementById(\'ServerList\').value; var trimmedTextAreaValue=textAreaValue.trim(); if(trimmedTextAreaValue!="") { document.myForm.submit(); } else{ alert("Server List Text Area is Empty"); } }; var input=document.getElementById(\'fileSelect\').value; if(input!="") { document.myForm.submit(); } else{ alert("You have not selected a file, please select one to proceed."); } var handleFileSelect = function(e) { var files = e.target.files; if(files.length === 1) { document.forms.myForm.filecsv.value = files[0].name; } } }; </script> </head>'; #Start of scripts to check Servers in textbox if(isset($_POST['ServerList'])) { //$arry=explode( "\r\n", $_POST['ServerList'] ); $txttrim = trim($_POST['ServerList']); //$textAr = explode("\n", $text); //$textAr1 = array_filter($text, 'trim'); // remove any extra \r characters left behind $txtarea = explode("\n",$txttrim); $txtarea = array_filter($txtarea,'trim'); //$proxyserver = 'smcdlabdev1'; $proxyserver = 'smd1utweadm0001'; $masterserver = 'smd1utweadm0001'; foreach ($txtarea as $line => $servername) { //$line => $servername; ##check if querying itself.... } if(isset($_FILES["filecsv"])) { $csv= $_FILES["filecsv"]; $output=shell_exec("powershell -executionPolicy Unrestricted D:/Web/scripts/phpfwrules.ps1".$csv."< NUL"); echo '<pre>'; echo '<h6>'; if ( $output== "The destination is reachable on port using the protocol") { echo "Destination IS reachable on port using protocol"; } else { echo "Destination is NOT reachable on port using protocol"; } echo '</h6>'; echo' <link rel=stylesheet href=dhtmlwindow.css type=text/css /> <link rel=stylesheet type=text/css href=style.css> <script src=js/dhtmlwindow.js></script> <h3>Firewall Implementation </h3> <hr /> <table class=results> <tr> <th>Server Name</th> <th>Firewall Acceptance</th> </tr> <tr> <td>'.$servername.'</td> <td>'.$output.'</td> <td></td> '; print_r($output); } } echo '</pre>'; echo ' <h3>Firewall Implementation </h3> <h4>Please enter the server below, you can only select one server at a time. </h4> <!--The form--> <form action="fw2.php" method="post" name="myForm" id="myForm"> <textarea name=ServerList id=ServerList> </textarea> <h5>Please select a CSV file.</h5> <input type="file" id="filecsv"> <input type="hidden" name="filecsv"> <br> <button onclick=\'upload()\'> Submit </button </html> ' ; ?> And here is the powershell script: #### Set Parameter for the input filename #### Param( [Parameter( # Mandatory = $true, ParameterSetName = '', ValueFromPipeline = $true)] [array]$Filename ) #### Check if files already exist #### if (test-path c:\etc\company\ports\working.txt) { Remove-item c:\etc\company\ports\working.txt } if (test-path c:\etc\company\ports\NOTworking.txt) { Remove-item c:\etc\company\ports\NOTworking.txt } #### Create Directory if it does not exist #### if ((test-path c:\etc\company\ports) -eq $false) { New-Item -ItemType directory -Path C:\etc\company\Ports } #### Output filenames #### #### Declaring the failure variable as an array #### $failure = @() $computer = gc env:computername $outputfileworking = "C:\etc\company\ports\working.txt"; $outputfileNOTworking = "C:\etc\company\ports\NOTworking.txt"; #### Output servername to the output file #### echo "ServerName:$computer" | out-file -filepath $outputfileworking $path = "$Filename" $csv = Import-csv -path $path ForEach($line in $csv) { $destination = $line.destination $protocol = $line.protocol $port = $line.port $result = ./portqry.exe -n $destination -e $port -p $protocol if ($result -like "*: LISTENING*") { Echo "The destination is reachable on port using the protocol" | out-file -filepath $outputfileworking -append } else { $failure +="The destination is not reachable on port using the protocol" } } if ($failure) { echo "ServerName:$computer" | out-file -filepath $outputfileNOTworking echo $failure | out-file -filepath $outputfileNOTworking -append } Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 15, 2014 Share Posted September 15, 2014 I just did a quick scan, and noticed a couple of major problems off the start. 1. You need to add the enctype="multipart/form-data" in your form element. 2. $_FILES is a multi-dem associative array. To see this, include: echo '<pre>' . print_r($_FILES,true) . '</pre>'; at the top of your script. You will then see that passing the variable $csv as you have, then the following call: $output=shell_exec("powershell -executionPolicy Unrestricted D:/Web/scripts/phpfwrules.ps1".$csv."< NUL"); will read the string as: "powershell -executionPolicy Unrestricted D:/Web/scripts/phpfwrules.ps1Array< NUL" PS. Please use [ code ] ... [ / code ] tags when posting code, it keeps the indentation, and makes it easier to read. Quote Link to comment Share on other sites More sharing options...
curmudgeonly-grump Posted September 16, 2014 Author Share Posted September 16, 2014 (edited) Thanks for the advice, sorry for the code indentation new to the forum and new to PHP. The issue is that it does not display $output. Upon asking it to print_r($output); I receive errors like Access to the path 'C:\etc\company\ports\NOTworking.txt' is denied. AND Import-Csv : Cannot bind argument to parameter 'Path' because it is an empty string. Am I to assume it is an empty string as the file was not passed correctly. Edited September 16, 2014 by curmudgeonly-grump Quote Link to comment Share on other sites More sharing options...
curmudgeonly-grump Posted September 16, 2014 Author Share Posted September 16, 2014 Update I made a change to the code so that It uploads the file (which works) then I use $csv= $_FILES["uploaded"]; $output=shell_exec("powershell -executionPolicy Unrestricted D:/Web/scripts/PHPfwrules.ps1".$csv."< NUL"); echo '<pre>'; print_r($output); echo '<h6>'; if ( $output== "destination is reachable on port using protocol") { echo "Destination IS reachable on the port using the protocol"; } else { echo "Destination is NOT reachable on the port using the protocol"; } echo '</h6>'; Now I receive: The term 'D:/Web/scripts/PHPfwrules.ps1Array' is not recognized as the name ofa cmdlet, function, script file, or operable program. Check the spelling of thename, or if a path was included, verify that the path is correct and try again. jcbones said the string would be read "powershell -executionPolicy Unrestricted D:/Web/scripts/phpfwrules.ps1Array< NUL" Am I receiving this error as I am passing it as an array does the file need to be read and the output stored as a string? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.