Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one execute the windows runas command from inside cygwin

If I try to use the Windows runas command from inside Cygwin the Enter password line prints to standard out but it doesn't wait for me to type my password into System.in

Is there a way around this?

like image 265
James Robinson Avatar asked Sep 16 '13 08:09

James Robinson


People also ask

How do you execute a Runa command?

To use runas at the command line, open a command prompt, type runas with the appropriate parameters, and then press ENTER. In the user interface for Windows Vista, the Run as… command has been changed to Run as administrator.

How do I pass a username and password in runas command?

The runas command does not allow a password on its command line. This is by design (and also the reason you cannot pipe a password to it as input).

How do you use runas in PowerShell?

Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key. Step 2: Now, the command prompt will turn to Windows PowerShell. Step 3: Type the command start-process PowerShell -verb runas and press "enter" key. Step 4: It will bring up an elevated Windows PowerShell as an administrator.


2 Answers

Instead of starting a "Cygwin Terminal", you can start a "Cygwin Console". You can do this by executing

c:\cygwin\bin\bash.exe --Login

Then, you should be able to run native Windows commands like runas.

(However, you lose some POSIX compatibility, because the shell will run in a Windows console window and not inside a MinTTY terminal.)

like image 34
ManuelAtWork Avatar answered Sep 24 '22 17:09

ManuelAtWork


I figured I could use 2 nested Command Prompt window pop-ups to run a password-changing script,

cygstart cmd /c "runas /user:DOMAIN\\USER \"powershell %cd%\\changepw.ps1\" & pause"

The changepw.ps1 script follows,

# http://serverfault.com/questions/570476/how-can-a-standard-windows-user-change-their-password-from-the-command-line
# cygstart cmd /c "runas /user:DOMAIN\\USER \"powershell %cd%\\changepw.ps1\" & pause"

param (
    [string]$oldPassword = $( Read-Host "Old password"),
    [string]$newPassword = $( Read-Host "New password")
)

$ADSystemInfo = New-Object -ComObject ADSystemInfo
$type = $ADSystemInfo.GetType()
$user = [ADSI] "LDAP://$($type.InvokeMember('UserName', 'GetProperty', $null, $ADSystemInfo, $null))"
try {
    $user.ChangePassword($oldPassword, $newPassword)
} catch [Exception] {
    # echo $_.Exception.GetType().FullName, $_.Exception.Message
    echo $_.Exception | format-list -force
}
write-host "Press any key to continue..."
[void][System.Console]::ReadKey($true)
like image 159
eel ghEEz Avatar answered Sep 25 '22 17:09

eel ghEEz