I am kind of new to both PHP and Powershell. I made a website to run a powershell script , which checks multiple server file directory and returns the items in that directory. On the website they display on a organized data table.
My issue is that when php exec() calls on the script, it runs but the data output is blank. Why does this happen when php runs the script?
Here is the powershell file. I even added some credential sessions, thinking it must be a credentials issue.
Remove-PSDrive target: -ErrorAction SilentlyContinue
$global:username = "domain\user"
$global:password = ConvertTo-SecureString –String "MyPassword" –AsPlainText -Force
$global:cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $global:username, $global:password
$output_Table = @()
$today = get-date
$html_output =@()
$tableData=@()
New-PSDrive -Name target -PSProvider FileSystem -Credential $global:cred -Root "\\serverName\filedir" | Out-Null
$tableData+= '<table><tr><th>First Feed</th><th>Updates:DAILY</th></tr> <tr><th>FileName</th><th>FileDate</th></tr>'
foreach ($file in (ls target:))
{GetFeed(-7)}
$tableData +='</table>'
Remove-PSDrive -Name target -Force
$html_output += $tableData
$html_output += '</div>'
$html_output | Out-File "C:\xampp\htdocs\script_results.txt" -Encoding utf8
$today | Out-file "C:\xampp\htdocs\script_TimeStamp.txt" -Encoding utf8
The script_result.txt has the files with the HTML tags. PHP will later read this and echo it into the html page.
The PHP code that calls on this script is the following.
<?php
if(isset($_POST['submit'])){
set_time_limit (300);
$output = array();
$return_code = 0;
$last_line = exec('powershell.exe C:\xampp\htdocs\script.ps1 2>&1 ', $output, $return_code);
} ?>
later on theres this piece of code to display the results. Once again, if i run the powershell script myself, it returns the script_result with the desired output. If PHP runs it, the script runs, but the output has no file data in it.
What is causing this?