0

I understand that you cannot elevate an existing process, but you can create a new process with elevated privileges.

Currently I have two scripts, where one script creates elevated privileges and calls another.

# script1.ps1

$abc = $args
$startInfo = $NULL
$process = $NULL
$standardOut = $NULL
$userId = $NULL

$password = get-content C:\cred.txt | convertto-securestring    

$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = "C:\script2.ps1 " + $abc

$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Username = "username"
$startInfo.Domain = "DOMAIN"
$startInfo.Password = $password 

$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$userId = $process.StandardOutput.ReadToEnd() 
$process.WaitForExit()

return $userId

At first, I thought of creating a function, New_Function in script1.ps1, and launching through $startInfo.Arguments, i.e. $startInfo.Arguments = New_Function

$abc = $args
$startInfo = $NULL
$process = $NULL
$standardOut = $NULL
$userId = $NULL

Function New_Function(){  
    $foo = "Hello World"
    return $foo
}


$password = get-content C:\cred.txt | convertto-securestring    

$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = New_Function

$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Username = "username"
$startInfo.Domain = "DOMAIN"
$startInfo.Password = $password 

$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$userId = $process.StandardOutput.ReadToEnd() 
$process.WaitForExit()    

return $userId

Instead of "Hello World" being printed to the screen, I get the following error,

The term 'Hello' is not recognized as the name of a cmdlet, function, script fi
le, or operable program. Check the spelling of the name, or if a path was inclu
ded, verify that the path is correct and try again.
At line:1 char:6
+ Hello <<<<  World
    + CategoryInfo          : ObjectNotFound: (Hello:String) [], CommandNotFou 
   ndException
    + FullyQualifiedErrorId : CommandNotFoundException

Any ideas???

Glowie
  • 2,271
  • 21
  • 60
  • 104
  • I think `$startInfo.Arguments = New_Function` may set `$startInfo.Arguments` to "Hello World", so the resulting command line is `powershell.exe hello world`. Try setting a breakpoint in New_Function and seeing when it gets called – JohnL Oct 07 '13 at 17:34
  • Also, you may find that the new function is not available in the new Powershell process, because it's not part of your current powershell scope. – JohnL Oct 07 '13 at 17:36
  • @JohnL I placed the breakpoint and New_Function is called after executing $startInfo.Arguments ... – Glowie Oct 07 '13 at 18:04

1 Answers1

1

This line:

 $startInfo.Arguments = New_Function

Calls New_Function, which returns "Hello World" and assigns that to $startInfo.Arguments. So when you run start the process the command line looks like:

C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe hello world

The error message is telling you that PowerShell can't find a command (or app) named hello. I'm not entirely clear what you're trying to do. As was mentioned in the commments, the function New_Function will not be available in the new Powershell.exe process unless you put a copy of it in a script and invoke it from there and then pass that script's path to Powershell.exe.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • I currently have two scripts, where one script calls another script with elevated privileges (http://stackoverflow.com/questions/18534500/powershell-script-1-calls-script-2-how-to-return-value-from-script-2-to-scri). I would like to make these two scripts into a single script, i.e. the original second script becomes a spawned processes, or function that is run in elevated mode. – Glowie Oct 08 '13 at 12:43
  • See if this blog post helps any: http://rkeithhill.wordpress.com/2013/04/05/powershell-script-that-relaunches-as-admin/ – Keith Hill Oct 08 '13 at 15:18