Search code examples
phppowershellsshshell-execcisco

Can't run powershell script from php


I use the Powershell script following to get a show run command run on a cisco device :

getconfig.ps1

$ErrorActionPreference ="Inquire"
Import-Module SSH-Sessions
$time ="$(get-date -f yyyy-MM-dd)"
$filename ="config"
$ext =".txt"
$filepath ="temp\"
$d1 ="x.x.x.x"
$u1 ="user"
$p1 ="password"
New-SshSession $d1 -Username $u1 -Password "$p1"
$c1 ="show run"
$Results = Invoke-Sshcommand -InvokeOnAll -Command "$c1" | Out-File "$filepath$filename-$time$ext"
Remove-SshSession -RemoveAll
exit

When I run this script manually by right click/run with powershell that's worlking fine. The problem come when I try to run this script via PHP.

index.php

<?php
$output = shell_exec('powershell C:\wamp\www\getconf\script\getconfig.ps1');
echo "<pre>$output</pre>";
?>

index.php loads forever and never give me an output and never create the results file.

I have tried to run another script, like test.ps1 that simply create a directory and it's working fine; I get the output and my directory is created.

My powershell script works.

The link between PHP and my PowerShell script works.

I do not understand where is the problem from.


Solution

  • As commented by jisaak the problem was that my webserver wasn't able to reach the destination directory.

    By hardcoding the path properly the problem has been fixed.