RE: UAC via ShellExecute - can't capture stdout
"Scott Zionic" <Scott.Zionic-jo7H/[email protected]>
| Newsgroups | gmane.comp.windows.off-topic |
|---|---|
| Message-ID | <[email protected]> |
The problem is that RedirectStandardOutput doesn't work with ShellExecute. The only way to get the console output is to pass credentials in the ProcessStartInfo - but that means saving credentials in a plain-text script file. On Mon, July 13, 2009 12:41 pm, Ryan Rinaldi (TT) wrote: > Wow. I completely got hung up on you saying "ShellExecute" and didn't > read the code you posted. Reading FAIL. > > On $psi you should be able to set RedirectStandardOutput to true then > read the output from the StandardOutput stream of the process object > returned from Process.Start(). > > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of Ryan Rinaldi > (TT) > Sent: Monday, July 13, 2009 11:28 AM > To: [email protected] > Subject: RE: [OT] UAC via ShellExecute - can't capture stdout > > Since PS is based on .NET can't you use Process.Start()[1]? > Process.Start() takes in a ProcessStartInfo[2] which allows you to > redirect stout. > > 1: http://msdn.microsoft.com/en-us/library/0w4h05yb.aspx > 2: > http://msdn.microsoft.com/en-us/library/system.diagnostics.processstarti > nfo_members.aspx > > > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of Scott Zionic > Sent: Monday, July 13, 2009 11:21 AM > To: [email protected] > Subject: [OT] UAC via ShellExecute - can't capture stdout > > > I love PowerShell - but I don't love this message: > > "The requested operation requires elevation." > > For example, "ipconfig /flushdns" requires elevated privileges. I could > use "runas" like so: > > runas /user:username "cmd /k ipconfig /flushdns" > > But now I'm doing a lot of unnecessary typing and I even have to enter a > password even though the username I pass is the currently logged on > user. > > I did some googling and came up with what initially appeared to be a > solution: > > http://www.peterprovost.org/blog/post/Shifting-Powershell-Arrays.aspx > > function elevate > { > $file, [string]$arguments = $args; > $psi = new-object System.Diagnostics.ProcessStartInfo $file; > $psi.Arguments = $arguments; > $psi.Verb = "runas"; > [System.Diagnostics.Process]::Start($psi); > } > > This function uses ShellExecute with the runas verb in order to achieve > much the same result as above but it's much nicer since it doesn't > require > all of the extraneous typing. One major problem, though: there is no way > to redirect stdout when launching a process via ShellExecute. So even > though I can use this function with "ipconfig /flushdns", there is no > way > to know for sure that the command succeeded since the output is written > to > a console window that immediately disappears. > > Is there any why to get the functionality of the runas verb without > using > ShellExecute? > > Scott > > > > > > > ------------------------------------ > > Yahoo! Groups Links > > > > > > ------------------------------------ > > Yahoo! Groups Links > > > >