0

$features = Start-Process powershell -Verb runAs -ArgumentList "Get-WindowsOptionalFeature –Online" $features

How can I get the result back into my $feature variable?

TTT
  • 183
  • 1
  • 10
  • Run the command `Get-WindowsOptionalFeature -Online` from an elevated window. – Bill_Stewart Jul 06 '17 at 16:03
  • 1
    Possible duplicate of [Capturing standard out and error with Start-Process](https://stackoverflow.com/questions/8761888/capturing-standard-out-and-error-with-start-process) – Bacon Bits Jul 06 '17 at 16:04
  • Following that link, you'd need to specify `StartInfo.Verb = "runas"` to run the process elevated. – Bacon Bits Jul 06 '17 at 16:06
  • @Bill_Stewart It is an auto script which will be executed by AWS CodeDeploy. I have no control of the machine. – TTT Jul 06 '17 at 16:15
  • @BaconBits Tried the solutions in that post before, seems not working for me. This is a windows command not an external process – TTT Jul 06 '17 at 16:15
  • 1
    The point is that you must start the process elevated to begin with. You can't elevate from an unelevated process without provoking the UAC prompt. This is by design. – Bill_Stewart Jul 06 '17 at 16:19
  • @Bill_Stewart What makes you think he cares about the UAC prompt? – Bacon Bits Jul 06 '17 at 17:05
  • I added the part about the UAC prompt because it gets asked about repeatedly in these kinds of questions. – Bill_Stewart Jul 06 '17 at 17:06

1 Answers1

1

Quick & dirty workaround: you can use temporary clixml file to store results of Get-WindowsOptionalFeature cmdlet:

$tempFile = [System.IO.Path]::GetTempFileName()
try
{
    Start-Process powershell -Wait -Verb runAs -ArgumentList "-Command Get-WindowsOptionalFeature -Online | Export-Clixml -Path $tempFile"

    $features = Import-Clixml -Path $tempFile

    # Use $features
}
finally
{
    if (Test-Path $tempFile)
    {
        Remove-Item -Path $tempFile -Force -ErrorAction Ignore
    }
}
Giorgi Chakhidze
  • 3,351
  • 3
  • 22
  • 19