Last active
June 26, 2017 07:02
-
-
Save h1romas4/7bb2a54e463dce7208aaf54ac219ab8a to your computer and use it in GitHub Desktop.
PowerShell から .bat ファイルを管理者ユーザで実行し標準出力とリターンコードを取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$command = "dir C:\Windows\System32" | |
$info = New-Object System.Diagnostics.ProcessStartInfo | |
$info.FileName = [System.Environment]::GetEnvironmentVariable("ComSpec") | |
$info.Arguments = "/c " + $command | |
$info.UseShellExecute = $false | |
$info.RedirectStandardOutput = $true | |
$info.CreateNoWindow = $true | |
$info.Verb ="RunAs" | |
$proc = [System.Diagnostics.Process]::Start($info) | |
while (!$proc.HasExited) { | |
Write-Host $proc.StandardOutput.ReadLine() | |
} | |
$lastline = $proc.StandardOutput.ReadToEnd() | |
if($lastline.Trim() -ne '') { | |
Write-Host $lastline | |
} | |
# $proc.WaitForExit() | |
Write-Host $proc.ExitCode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment