Powershell 如何在同一脚本中生成新进程

Powershell 如何在同一脚本中生成新进程,powershell,process,elevated-privileges,Powershell,Process,Elevated Privileges,我知道您不能提升现有流程,但可以使用提升的权限创建新流程 目前我有两个脚本,其中一个脚本创建提升权限并调用另一个脚本 # script1.ps1 $abc = $args $startInfo = $NULL $process = $NULL $standardOut = $NULL $userId = $NULL $password = get-content C:\cred.txt | convertto-securestring $startInfo = New-Object

我知道您不能提升现有流程,但可以使用提升的权限创建新流程

目前我有两个脚本,其中一个脚本创建提升权限并调用另一个脚本

# 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
起初,我想在script1.ps1中创建一个函数New_函数,并通过$startInfo.Arguments启动,即$startInfo.Arguments=New_函数

$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
屏幕上没有打印“Hello World”,而是出现以下错误:

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
术语“Hello”无法识别为cmdlet、函数、脚本的名称
le或可操作程序。检查名称的拼写,或者是否包含路径
请验证路径是否正确,然后重试。
第1行字符数:6
+您好这行:

 $startInfo.Arguments = New_Function
调用新的函数,该函数返回“Hello World”,并将其分配给$startInfo.Arguments。因此,当您运行启动进程时,命令行如下所示:

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

错误消息告诉您PowerShell找不到名为
hello
的命令(或应用程序)。我不完全清楚你想做什么。正如在命令中提到的,函数New_函数在New Powershell.exe进程中不可用,除非您将其副本放入脚本中并从那里调用它,然后将该脚本的路径传递到Powershell.exe。

我认为
$startInfo.Arguments=New_函数
可以将
$startInfo.Arguments
设置为“你好,世界,因此生成的命令行是
powershell.exe hello world
。尝试在New_函数中设置断点,并查看它何时被调用。因此,您可能会发现新函数在New Powershell进程中不可用,因为它不属于您当前的Powershell作用域。@JohnL我放置了断点,在执行$startInfo.Arguments后调用了New_函数……我当前有两个脚本,其中一个脚本调用具有提升权限的另一个脚本()。我想将这两个脚本制作成一个脚本,即原始的第二个脚本成为派生的进程,或在提升模式下运行的函数。请参阅此博客文章是否有帮助: