Skip to content

Instantly share code, notes, and snippets.

@ismits
Created May 1, 2019 19:41
Show Gist options
  • Save ismits/510f2784b716f0290c0a3a650dac795a to your computer and use it in GitHub Desktop.
Save ismits/510f2784b716f0290c0a3a650dac795a to your computer and use it in GitHub Desktop.
PowerShell invoke shell command
function Invoke-ShellCommand
{
param (
$Command,
$Arguments,
$WorkingDirectory
)
$psi = New-object System.Diagnostics.ProcessStartInfo
$psi.CreateNoWindow = $true
$psi.UseShellExecute = $false
$psi.RedirectStandardOutput = $true
$psi.RedirectStandardError = $true
if ($WorkingDirectory)
{
$psi.WorkingDirectory = $WorkingDirectory
}
$psi.FileName = $Command
$psi.Arguments = $Arguments
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $psi
[void]$process.Start()
$stdout = $process.StandardOutput.ReadToEnd()
$stderr = $process.StandardError.ReadToEnd()
$process.WaitForExit()
$process.Close()
return $stdout, $stderr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment