Skip to content

Instantly share code, notes, and snippets.

@h1romas4
Last active June 26, 2017 07:02
Show Gist options
  • Save h1romas4/7bb2a54e463dce7208aaf54ac219ab8a to your computer and use it in GitHub Desktop.
Save h1romas4/7bb2a54e463dce7208aaf54ac219ab8a to your computer and use it in GitHub Desktop.
PowerShell から .bat ファイルを管理者ユーザで実行し標準出力とリターンコードを取得
$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