如何从Start-Process捕获W​​indows命令结果?

时间:2017-07-06 16:01:56

标签: powershell

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

如何将结果返回到我的$feature变量?

1 个答案:

答案 0 :(得分:1)

快速&脏解决方法:您可以使用临时clixml文件来存储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
    }
}