0

I am trying to return the state of Hyper-V into a VBScript. In order to do this, I need to execute the following command:

Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online

If I set this as a variable, then I can echo the state and get Enabled or Disabled. e.g.

PS> $hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online
PS> echo $hyperv.State
Enabled

PS>

In order to run this though, I need to have escalated privileges. So, by running the following, that will get me into escalated mode.

PS> Start-Process powershell -Verb runAs

or from cmd.exe

c:\> powershell -noexit -command Start-Process powershell -Verb runAs

I see problems that I have to overcome; the first being that I need to get a powershell in escalated mode, and the second is getting the command to run and return to a variable. Where I am lost is how do I return this state as variable to the VBScript.

Update 1:

This command:

powershell.exe Start-Process powershell.exe -ArgumentList '-command $hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online; echo $hyperv.State' -verb RunAs

returns the state of HyperV in a powershell. Now I just need to get this variable into a VBScript.

** Update 2: **

Here is the gist of my attempt to set the variable with the return value.

set shell = CreateObject("WScript.Shell")

HyperVStateCommand = "powershell.exe Start-Process powershell.exe -ArgumentList '-command $hyperv = Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online; echo $hyperv.State' -verb RunAs"

set HyperVCheck = shell.Exec(HyperVStateCommand)
HyperVStatus = HyperVCheck.StdOut.ReadAll

msgBox HyperVStatus

The PowerShell opens and displays "Enabled" before closing. If you add -noexit in the ArgumentList (before -command), the powershell will stay open so that you can see the value.

The message box that comes up is blank though. HyperVStatus isn't being assigned the value from powershell.

  • 1
    Where is the relationship to [[tag:vbscript]] in your question? I see PowerShell commands entered directly in a Windows PowerShell window, and a command entered directly into a Windows Command Prompt window. How do you return the state of the variable to the VBScript when you're not running elevated? and how is that different to running it elevated? You need to [edit] your question to provide the required clarity, because currently the code you require us to assist you to fix a specific problem with, does not appear to do what your question body is explaining. – Compo Sep 07 '21 at 14:57
  • I think the question is clear. I am trying to get the results of a poweshell command into a variable in a VBScript. That variable will be set to "Enabled" or "Disabled" depending on the command run. In order to get that result though, I have to run that specific command in an escalated powershell. Should I spell out the declaration of a variable.... Results = 'Get-WindowsOptionalFeature -FeatureName Microsoft-Hyper-V-All -Online' so that you know that I am also working with VBS? – Will Roberts Sep 07 '21 at 15:25
  • When passing data between scripts running with different privileges (i.e. different accounts), typically, an HKLM registry value (if passing script is elevated) or temp file is used. – LesFerch Sep 07 '21 at 15:32
  • 1
    Does this answer your question? [VBscript code to capture stdout, without showing console window](https://stackoverflow.com/questions/4919573/vbscript-code-to-capture-stdout-without-showing-console-window) – user692942 Sep 07 '21 at 15:52
  • 1
    Your question @WillRoberts, has absolutely nothing to do with a problem with your provided code then, has it? This site is here to provide assistance with a single specific and reproducible issue with your provided code. As your code does not in any way include a VBScript you are not seeking on topic assistance with a VBScript. Please [edit] your question to include the VBScript code you're using and explain where exactly the specific issue is being exhibited. Without any VBScript code, your question is a reference information only exercise, and should probably therefore reside on Super User. – Compo Sep 07 '21 at 16:10
  • @WillRoberts, have you researched using the search facility at the top of the page how to run a command and get the outut from it as the value of a variable. Where is that code? How does that code work for you when you decide to run as Administrator / Elevated? What is the specific issue you are experiencing when doing that? I can assure you that you are not the first person who has ever wanted to run a command, and retrieve the result from doing so as a variable in a VBScript, so what I need to understand is exactly how your code, which you haven't given us, is failing to work as intended. – Compo Sep 07 '21 at 16:37
  • @Compo, I have updated the post and added the VBS at the bottom. The results are populating in powershell, but they aren't coming over into VBS. – Will Roberts Sep 07 '21 at 16:50
  • 2
    It appears to me as if your issue is because you are launching an elevated instance of PowerShell from another, so whilst the elevated instance returns the string you require, that is returned to the invoking PowerShell instance, which upon exiting has no StdOut, and therefore returns nothing for your VBScript message box. Why are you not trying to launching an elevated PowerShell instance directly from VBScript instead? – Compo Sep 07 '21 at 18:18

0 Answers0