1

As an exercise, I am trying to make a web control panel for Hyper-V. It will use Powershell to interact with Hyper-V.

First thing I tried to do was to use the Get-VM command to create a table with the status of all the VM's on the local machine. I ran Get-VM from PHP:

$output = `powershell Get-VM`;

$output became NULL. After a lot of troubleshooting, I realized that Get-VM doesn't give output unless Powershell is running as an administrator (I was running the Powershell instance that I was testing with as administrator, but the one that was created by PHP was not run as administrator).

Then, I went to the powershell.exe file, right-clicked on it, went to Properties -> Compatibility -> Change settings for all users, and checked "Run as administrator". This should make it run as administrator no matter where it is started from, and it appears that it does exactly that in this case as well. However, once Powershell is running as an administrator, it does not output to a file, even if told to do so by the > operator.

If I run powershell Get-VM > out.txt, while powershell is running as an administrator, it creates the file out.txt, but it is 0kb and contains nothing. powershell Get-Process > out.txt has the same result, a 0kb file.

Running powershell Get-Process > out.txt with a non-elevated powershell.exe yields the expected result, namely the output from Get-Process in out.txt. However, you can't do this with powershell Get-VM > out.txt, since Get-VM returns no data unless powershell is run as administrator.

To wrap it up, my question is why does Powershell refuse to redirect its STDOUT to a file if it is running as administrator, but not if it is running as a normal user?

Bjonnfesk
  • 298
  • 2
  • 13
  • I am unable to replicate the issue using PowerShell v3 or v4. I pulled up an elevated CMD window, and ran `powershell get-process > c:\temp\out.txt` and got the expected results in the file. – TheMadTechnician Apr 25 '14 at 22:23
  • When you run `powershell Get-Process > out.txt` what are you running it from? Another instance of PowerShell? Cmd.exe? The redirection is handled by whatever environment you are running from. Try `powershell -command "& {Get-VM > c:\out.txt}"`? – Keith Hill Apr 26 '14 at 01:57
  • Also, are you sure you haven't set the "Run as administrator" property on a shortcut? From FileExplorer, when I right-click PowerShell.exe down in the C:\Windows\System32\WindowsPowerShell\v1.0 dir, I don't see any Compatibility tab or a way to way to set always run as admin. – Keith Hill Apr 26 '14 at 02:03
  • TheMadTechnician: I get that too. The problem arises when you call it from a non-elevated CMD prompt, but powershell.exe itself is set to run as administrator. This is "correct" in relation to the usecase, because PHP's `shell_exec` is not elevated. Keith Hill: It doesn't seem to matter where I run it from, whether it's PHP `shell_exec`, PHP `exec`, the backtick operator, CMD or Powershell itself. – Bjonnfesk Apr 26 '14 at 10:19
  • About the shortcut stuff: No, it's not set on a shortcut. it seems that Windows has some sort of check in place to prevent you from setting it to run as administrator. If you copy the file to another directory, though, it will let you do it, and then you can copy it back (if you have write permissions to its folder). I presume it has to do with permissions. Interestingly, whenever I call the powershell command, whereever it's from, I can see the output on the screen (on loading the page, the prompt is visible for a second our two), but it doesn't go into the file. – Bjonnfesk Apr 26 '14 at 10:26
  • I'm pretty sure Windows also won't allow you to redirect standard input/output/error across the admin/non-admin security boundary. You'll have to find a different way to get output from the program running as admin - Reference: http://stackoverflow.com/a/8690661 Any final solution with full source code sample application ? IMHO, better samples for minimize learning curve are real applications with full source code and good patterns. – Kiquenet Aug 28 '14 at 06:44

1 Answers1

0

My diligent father has solved the problem, by not using redirection operators, but rather piping the output into Powershell's own out-file or export-csv, so that the command becomes powershell Get-VM | export-csv out.csv. I then parse this in PHP and convert it to a table. Works like a charm.

Bjonnfesk
  • 298
  • 2
  • 13