Question

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???

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top