2017-07-06 19 views
0

$features = Start-Process powershell -Verb runAs -ArgumentList "Get-WindowsOptionalFeature –Online" $features 如何從Start-Process捕獲W​​indows命令結果?

如何將結果返回到我的$feature變量中?

+0

運行'從提升窗口獲取-WindowsOptionalFeature -Online'。 –

+1

[捕獲標準輸出和錯誤與啓動過程](https://stackoverflow.com/questions/8761888/capturing-standard-out-and-error-with-start-process) –

+0

在此鏈接之後,您'需要指定'StartInfo.Verb =「runas」'來運行升級過程。 –

回答

1

快速&髒解決辦法:你可以用臨時CLIXML文件來存儲獲取-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 
    } 
}