Search the Community
Showing results for tags 'csv powershell'.
-
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 }