Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Powershell not available on localHost #53

Open
oriceon opened this issue Jun 1, 2021 · 8 comments
Open

Powershell not available on localHost #53

oriceon opened this issue Jun 1, 2021 · 8 comments

Comments

@oriceon
Copy link

oriceon commented Jun 1, 2021

$shellObj = \MTS\Factories::getDevices()->getLocalHost()->getShell('powershell');

Throw Exception

MTS\Common\Devices\Actions\Local\Host\Shell::execute>> Powershell not available on localHost

How to fix that?

@merlinthemagic
Copy link
Owner

merlinthemagic commented Jun 1, 2021

Hi,

What is the return when you execute (as PHP user) on cmd?
where powershell

Just use exec(), that will mimic the execution done by the lib:
exec("where powershell", $data, $status);
Whats in $data and $status after execution?

@oriceon
Copy link
Author

oriceon commented Jun 1, 2021

On cmd i get:

λ where powershell
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

And on browser:

[]
1

@merlinthemagic
Copy link
Owner

merlinthemagic commented Jun 2, 2021

Hi,

Return code 1 means the command encountered an error. exec() is not super verbose when it comes to returning error messages.

Chances are your php user is locked to a subset of commands, or unable to traverse up the filesystem. Both pretty good practices for any public facing service.

Try to determine if this is a general exec() permissions issue or a power shell specific issue what does each of the following commands return if triggered through php exec()? if no return please provide return straight from cmd.

whoami
wmic OS get Name
wmic OS get OSArchitecture
cd
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command "Get-Host | Select-Object Version"

@oriceon
Copy link
Author

oriceon commented Jun 2, 2021

1) whoami

Browser: array:1 [▼
  0 => "nt authority\system"
]
0

CMD: My\Name


2) wmic OS get Name

Browser: array:3 [▼
  0 => "Name"
  1 => "Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition4"
  2 => ""
]
0

CMD: 
Name
Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition4


3) wmic OS get OSArchitecture

Browser: array:3 [▼
  0 => "OSArchitecture"
  1 => "64-bit"
  2 => ""
]
0

CMD:
OSArchitecture
64-bit


4) cd

Browser: array:1 [▼
  0 => "C:\Dev\www\public"
]
0

CMD: C:\Dev\www


5) C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command "Get-Host | Select-Object Version"

Browser: array:6 [▼
  0 => ""
  1 => "Version"
  2 => "-------"
  3 => "5.1.19041.1023"
  4 => ""
  5 => ""
]
0

CMD: 
Version
-------
5.1.19041.1023

@merlinthemagic
Copy link
Owner

Hi,

Thats just downright strange. Appears where is not in the environment path for the server.

Return of these last two commands please:

dir C:\Windows\System32\where.exe

C:\Windows\System32\where.exe powershell

@oriceon
Copy link
Author

oriceon commented Jun 7, 2021

Hi.

1)

Browser:
    Directory: C:\Windows\System32

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        07.12.2019     11:09          43008 where.exe


CMD:
 Volume in drive C is Windows
 Volume Serial Number is A243-E3BB

 Directory of C:\Windows\System32

07.12.2019  12:09            43.008 where.exe
               1 File(s)         43.008 bytes


2)

Browser:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

CMD:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

@merlinthemagic
Copy link
Owner

merlinthemagic commented Jun 8, 2021

Hi,

Turns out i have let us on a bit of a wild goose chase, mostly because exec() is so vague in its return. The help for "where" states:

  NOTE: The tool returns an error level of 0 if the search is
        successful, of 1 if the search is unsuccessful and
        of 2 for failures or errors.

Since you received a return code 1 when running "where powershell" in your first reply to this issue, it appears "where" is available in your environment, but it was unsuccessful in locating "powershell.exe".

Now this issue gets really wonky, because you just confirmed "where" is able to locate "powershell.exe", but only when you use the absolute path for "where". I have zero idea why that is.

Are you 100% sure you are executing using the same user/environment when running MTS and when triggering the exec() commands above? Is it possible MTS is running on a server and the exec() commands are executed on your local dev box? Its a silly question, but none of this makes sense because by default "where" will search the current directory and the environment path. There is no indication that behavior changes depending on if "where" is triggered with or without an absolute path.

Back to the issue at hand, your PHP user needs the powershell directory path in the PATH variable for the environment. You can check if it is by running:

exec("echo %PATH%", $data, $status);

OR just:

exec("path", $data, $status);

Somewhere in the $data return you should see:

C:\Windows\System32\WindowsPowerShell\v1.0\

If it is not there you can either append it to the system path (best option) or do it at runtime before executing MTS:

exec("setx PATH \"C:\Windows\System32\WindowsPowerShell\v1.0\;%PATH%\"", $data, $status);

There is a chance your path variable is larger than 1024 chars, in that case read this.

Good luck.

@plonknimbuzz
Copy link

Hi,

Turns out i have let us on a bit of a wild goose chase, mostly because exec() is so vague in its return. The help for "where" states:

  NOTE: The tool returns an error level of 0 if the search is
        successful, of 1 if the search is unsuccessful and
        of 2 for failures or errors.

Since you received a return code 1 when running "where powershell" in your first reply to this issue, it appears "where" is available in your environment, but it was unsuccessful in locating "powershell.exe".

Now this issue gets really wonky, because you just confirmed "where" is able to locate "powershell.exe", but only when you use the absolute path for "where". I have zero idea why that is.

Are you 100% sure you are executing using the same user/environment when running MTS and when triggering the exec() commands above? Is it possible MTS is running on a server and the exec() commands are executed on your local dev box? Its a silly question, but none of this makes sense because by default "where" will search the current directory and the environment path. There is no indication that behavior changes depending on if "where" is triggered with or without an absolute path.

Back to the issue at hand, your PHP user needs the powershell directory path in the PATH variable for the environment. You can check if it is by running:

exec("echo %PATH%", $data, $status);

OR just:

exec("path", $data, $status);

Somewhere in the $data return you should see:

C:\Windows\System32\WindowsPowerShell\v1.0\

If it is not there you can either append it to the system path (best option) or do it at runtime before executing MTS:

exec("setx PATH \"C:\Windows\System32\WindowsPowerShell\v1.0\;%PATH%\"", $data, $status);

There is a chance your path variable is larger than 1024 chars, in that case read this.

Good luck.

in some case we need to restart our windows to get some Environment
restart apache not gonna help
but in my experience the probability is just 5%

@merlinthemagic answer will gonna help a lot to debug the environment, so i think this issue can be marked as solved, because this is not MTS issue, but only local system issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants