-1

so this is my first post here on stackoverflow :-) hm,... would think i operate with powershell as an "experienced beginner", written some nice-working scripts for OS-automation in the last 2 years. no programming-skills, only little bit of scripting to make my life as sysadmin easier.

currently i have a tricky problem - excuse me in case this was already solved in another thread, but didn´t find a solution to it.

here the purpose of the code:

enable a normal windows-user (not member of local admin-group) to execute some scripts with administrative rights.

  1. switch current user to a user with admin-rights >> solved. created a pscredential-object ($pscreds). works good.
  2. start an elevated powershell-session to have the ability to do admin-stuff >> solved. powershell.exe -verb RunAs bla bla. works good.
  3. launch a fix-named script with admin-rights >> solved.

problem itself:

script-name is not always the same one.... i want to enable the code to use the path and name of current running script ($Myinvocation.Mycommand.Definition).

$x = $myinvocation.MyCommand.Definition
      Start-Process powershell.exe -Credential $pscreds -ArgumentList {
          Start-Process powershell.exe -Verb RunAs -ArgumentList {
            start-process powershell.exe -ArgumentList (-file "$x")
            }
        }

how to modify the code that the variable $x at -file "$x" takes the value defined in first line?

i found a workaround by myself write the path & scriptname to a temp-file and read it back to $x with "get-content". but i didn´t like this ugly solution and hoped solve this in a better way.

thanks for help and best greetings from germany

Ralph

mralph
  • 1
  • 1

1 Answers1

0

I think that without getting into a whole host of Windows Auth issues doing that inside one script, I'd stick to what you are already talking about as a kludge - Try letting the non-admin edit that $x value in a file they have access to. Then run a script on schedule with an admin account that reads the file.

Faye Smelter
  • 131
  • 9