2017-07-06 3 views
0

$features = Start-Process powershell -Verb runAs -ArgumentList "Get-WindowsOptionalFeature –Online" $features Start-Processからウィンドウコマンドの結果をキャプチャするには?

どうすれば結果を$feature変数に戻すことができますか?

+0

'は、Get-WindowsOptionalFeature -Online'を昇格ウィンドウからコマンドを実行します。あなたが取得-WindowsOptionalFeatureコマンドレットの結果を保存するために一時的Clixmlコマンドファイルを使用することができます。 –

+1

[開始プロセスの標準出力とエラーの取り込み]の可能な複製(https://stackoverflow.com/questions/8761888/capturing-standard-out-and-error-with-start-process) –

+0

このリンクに続いて、 'StartInfo.Verb =" runas "'を指定してプロセスを実行する必要があります。 –

答えて

1

クイック&汚い回避策:

$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 
    } 
} 
関連する問題