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?
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.
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).
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.
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.)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With