Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Windows 7 UAC with elevated privileges

by Michael Roberts (Sexton)
on Apr 24, 2014 at 10:47 UTC ( [id://1083559]=note: print w/replies, xml ) Need Help??


in reply to Windows 7 UAC with elevated privileges

I wrote Win32::RunAsAdmin to do just that, after finding this post and others on the same topic.

Replies are listed 'Best First'.
Re^2: Windows 7 UAC with elevated privileges
by Corion (Patriarch) on May 12, 2016 at 07:19 UTC

    Thank you very much for writing Win32::RunAsAdmin. The one thing I needed which it didn't do was to wait for the elevated process to finish running. I came up with the following Powershell incantation to do that and pass the exit code of the elevated process back:

    # Run a process in an elevated window, wait for its exit sub win32_run_elevated { my( $exe, @args ) = @_; my $args = join " ", map { if(/[ "]/) { s!"!\\"!g; qq{"$_"} } else + { $_ }} @args; my $ps1 = sprintf q{powershell -NonInteractive -NoProfile -Command + "$process = Start-Process '%s' -PassThru -ErrorAction Stop -Argument +List '%s' -Verb RunAs -Wait; Exit $process.ExitCode"}, $exe, $args; $ps1 }

    I haven't dug deep enough into Application.Process to know whether it can wait for the elevated child as well, and as my target environment has Powershell enabled anyway, using another level of indirection isn't a hurdle for me.

Re^2: Windows 7 UAC with elevated privileges
by Anonymous Monk on Apr 25, 2014 at 07:34 UTC
    Hi, I notice you got Win32::RunAsAdmin::restart, it might benefit from remembering @INC and other stuff :) see Devel::PL_origargv - access to the Perl interpreter's argv

      I don't think it should matter - it's actually closing the current (non-elevated) process and starting the same thing again from the shell with elevated privileges, so it's going to reload @INC the same way anyway.

      Once you're not in elevated mode, you can't change - you can't "upgrade" a running process. You can even see this in Microsoft's own tools; the Task Manager has a button "Show processes from all users" when you start it as a non-administrative user. Click that button and you'll notice that the Task Manager window goes away and reappears, now with a checkbox instead of a button. That's because the non-elevated process had to be swapped out for an elevated one.

      It's kinda weird, but eh. Windows.

        I don't think it should matter - it's actually closing the current (non-elevated) process and starting the same thing again from the shell with elevated privileges, so it's going to reload @INC the same way anyway.

        Look again because that is what it currently isn't doing, @INC is not @ARGV

        $ perl -Ishabba -e " use Data::Dump; use Devel::PL_origargv; dd( \@INC +, \@ARGV , [ Devel::PL_origargv->get ] )" -- ro sham bo ( [ "D:\\PL_origargv_shoulndtuseinlinedirectly\\_Inline\\lib", "shabba", "C:/citrusperl/site/lib/MSWin32-x86-multi-thread", "C:/citrusperl/site/lib", "C:/citrusperl/vendor/lib", "C:/citrusperl/lib", ".", ], ["ro", "sham", "bo"], [ "C:\\citrusperl\\bin\\perl.exe", "-Ishabba", "-e", " use Data::Dump; use Devel::PL_origargv; dd( \\\@INC, \\\@ARGV , +[ Devel::PL_origargv->get ] )", "--", "ro", "sham", "bo", ], )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083559]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-29 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found