【问题标题】:How to spawn new process in same script如何在同一脚本中生成新进程
【发布时间】:2013-10-14 09:29:32
【问题描述】:

我了解您无法提升现有进程,但您可以创建具有提升权限的新进程。

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

# 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_Function,通过$startInfo.Arguments启动,即$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

我得到以下错误,而不是“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

有什么想法吗???

【问题讨论】:

  • 我认为$startInfo.Arguments = New_Function 可能会将$startInfo.Arguments 设置为“Hello World”,因此生成的命令行是powershell.exe hello world。尝试在 New_Function 中设置断点并查看它何时被调用
  • 另外,您可能会发现新功能在新的 Powershell 进程中不可用,因为它不属于您当前的 powershell 范围。
  • @JohnL 我放置了断点并在执行 $startInfo.Arguments 后调用了 New_Function ...

标签: powershell process elevated-privileges


【解决方案1】:

这一行:

 $startInfo.Arguments = New_Function

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

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-11-25
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 2014-02-18
相关资源
最近更新 更多