Sha256: e2130cc42e6134252fa9a4f781ae63041106d6df2abacdb05aff6754e9ed9b26

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

function ps-runas ([String] $user, [String] $password, [String] $cmd, [String] $arguments)
{
  $secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
  $process = New-Object System.Diagnostics.Process
  $setup = $process.StartInfo
  $setup.FileName = $cmd
  $setup.Arguments = $arguments
  $setup.UserName = $user
  $setup.Password = $secpasswd
  $setup.Verb = "runas"
  $setup.UseShellExecute = $false
  $setup.RedirectStandardError = $true
  $setup.RedirectStandardOutput = $true
  $setup.RedirectStandardInput = $false
  
  $errEvent = Register-ObjectEvent -InputObj $process `
    -Event "ErrorDataReceived" `
    -Action `
    {
      param([System.Object] $sender, [System.Diagnostics.DataReceivedEventArgs] $e)
      if ($e.Data)
      {
        Write-Host $e.Data
      }
      else
      {
        New-Event -SourceIdentifier "LastMsgReceived"
      }
    }

  $outEvent = Register-ObjectEvent -InputObj $process `
    -Event "OutputDataReceived" `
    -Action `
    {
      param([System.Object] $sender, [System.Diagnostics.DataReceivedEventArgs] $e)
      Write-Host $e.Data
    }
  
  $exitCode = -1
  if ($process.Start())
  {
    $process.BeginOutputReadLine()
    $process.BeginErrorReadLine()
  
    $process.WaitForExit()
    $exitCode = [int]$process.ExitCode
    Wait-Event -SourceIdentifier "LastMsgReceived" -Timeout 60 | Out-Null
  
    $process.CancelOutputRead()
    $process.CancelErrorRead()
    $process.Close()
  }
  return $exitCode
}

exit ps-runas "<%= options[:user] %>" "<%= options[:password] %>" "<%= options[:cmd] %>" "<%= options[:arguments] %>"

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vagrant-windows-1.0.3 lib/vagrant-windows/scripts/ps_runas.ps1.erb
vagrant-windows-1.0.1 lib/vagrant-windows/scripts/ps_runas.ps1.erb
vagrant-windows-1.0.0 lib/vagrant-windows/scripts/ps_runas.ps1.erb