Wrapper over runas utility
Runas is a useful Windows utility that allows to run programs as different users. But this utility can not be executed from batch files (password cannot be piped). Hence I comeup with my own program which is a wrapper over runas and it is scriptable.
This is achived by utilizing another C++ class SendKeys, which I downloaded from http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp . This is a useful piece of code that allows to "send keys" to another active application.
Here is the code sample how I achieve this:
CSendKeys sk;
//create runas process
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( !CreateProcess( NULL, // No module name (use command line).
(LPTSTR)(LPCTSTR)runas , // Command line. runas is like "c:\\windows\\system32\\runas.exe /env /user:user command"
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
CREATE_NEW_CONSOLE,// Openup process in a new console
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure. ) {
cerr << "CreateProcess failed:" <<>
return -1;
}
WaitForInputIdle(pi.hProcess,INFINITE);
//activate it using sk
sk.AppActivate((LPCTSTR)runasPath);
//pass password (I pick it up from command line)
pass = pass + "{ENTER}";
sk.SendKeys(pass);
// Wait until child process exits. (make it an argument?)
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
This program can be called (from a .bat file, for example) RunAsAnotherUser username password command.
Cheerz,
--Uzi
This is achived by utilizing another C++ class SendKeys, which I downloaded from http://www.codeproject.com/cpp/sendkeys_cpp_Article.asp . This is a useful piece of code that allows to "send keys" to another active application.
Here is the code sample how I achieve this:
CSendKeys sk;
//create runas process
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( !CreateProcess( NULL, // No module name (use command line).
(LPTSTR)(LPCTSTR)runas , // Command line. runas is like "c:\\windows\\system32\\runas.exe /env /user:user command"
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
CREATE_NEW_CONSOLE,// Openup process in a new console
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure. ) {
cerr << "CreateProcess failed:" <<>
return -1;
}
WaitForInputIdle(pi.hProcess,INFINITE);
//activate it using sk
sk.AppActivate((LPCTSTR)runasPath);
//pass password (I pick it up from command line)
pass = pass + "{ENTER}";
sk.SendKeys(pass);
// Wait until child process exits. (make it an argument?)
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
This program can be called (from a .bat file, for example) RunAsAnotherUser username password command.
Cheerz,
--Uzi
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home