Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 异步启动PowerShell scriptblock,而不依赖于父进程_Multithreading_Powershell_Asynchronous_Process_Parent - Fatal编程技术网

Multithreading 异步启动PowerShell scriptblock,而不依赖于父进程

Multithreading 异步启动PowerShell scriptblock,而不依赖于父进程,multithreading,powershell,asynchronous,process,parent,Multithreading,Powershell,Asynchronous,Process,Parent,下面是一个小的PowerShell脚本,它异步运行一些PowerShell代码以显示一个对话框(用于演示我的问题) 如果关闭父PowerShell进程,子进程也将关闭,对话框将消失。是否有任何方法可以异步启动PowerShell脚本块(包含函数和参数),并且不依赖父PowerShell进程 $testjob = [PowerShell]::Create().AddScript({ $a = new-object -comobject wscript.shell $b = $a.p

下面是一个小的PowerShell脚本,它异步运行一些PowerShell代码以显示一个对话框(用于演示我的问题)

如果关闭父PowerShell进程,子进程也将关闭,对话框将消失。是否有任何方法可以异步启动PowerShell脚本块(包含函数和参数),并且不依赖父PowerShell进程

    $testjob = [PowerShell]::Create().AddScript({ $a = new-object -comobject wscript.shell
    $b = $a.popup('This is a test',5,'Test Message Box',1) })
    $result = $testJob.BeginInvoke()
更新#2

我试图执行一个脚本块,而不是一个外部脚本。脚本块应使用父脚本中的函数和变量。问题是,我无法将这些函数或变量传递给新进程,除非它们直接包含在脚本块中。你知道这是否可行吗

    Function Show-Prompt {
        Param ($title,$message)
            $a = new-object -comobject wscript.shell
            $b = $a.popup($message,5,$title,1)
    }  

    $scriptContent = {Show-Prompt -Message 'This is a test' -Title 'Test Message Box'}  
    $scriptBlock = [scriptblock]::Create($scriptContent)
    Start-process powershell -argumentlist "-noexit -command &{$scriptBlock}"

您可以使用中间PowerShell进程。必须有一个直接的父级来处理异步脚本的返回值。例如,将脚本放在一个名为popup.ps1的文件中,然后尝试按如下方式执行:

Start-Process PowerShell -Arg c:\popup.ps1

您可能希望将超时时间从5秒提高到10秒。您可以关闭原始PowerShell,弹出窗口将保持不变。关闭弹出窗口(或超时)时,辅助PowerShell窗口将消失。

您可以使用中间PowerShell进程。必须有一个直接的父级来处理异步脚本的返回值。例如,将脚本放在一个名为popup.ps1的文件中,然后尝试按如下方式执行:

Start-Process PowerShell -Arg c:\popup.ps1

您可能希望将超时时间从5秒提高到10秒。您可以关闭原始PowerShell,弹出窗口将保持不变。当您关闭弹出窗口(或超时)时,辅助PowerShell窗口将消失。

您可以使用WMI执行此操作。如果使用Win32_进程创建该进程,则它将在关闭PowerShell后继续存在

例如:

invoke-wmimethod -path win32_process -name create -argumentlist calc

可以使用WMI执行此操作。如果使用Win32_进程创建该进程,则它将在关闭PowerShell后继续存在

例如:

invoke-wmimethod -path win32_process -name create -argumentlist calc

谢谢Keith,请参阅我问题中的更新#2-我正在尝试执行脚本块而不是外部脚本。@位和可以将脚本块作为参数传递给PowerShell.exe的字节(请参阅PowerShell.exe上的-Command参数)。无论您如何以异步方式运行脚本块,父PowerShell进程都会挂起以从脚本块中获取结果和错误。现在我想您可以使用Invoke命令-indconnectedsession,但是UI没有出现在您的桌面上。我不得不使用script方法-无法让scriptblock工作。谢谢您的帮助。谢谢Keith,请参阅我问题中的更新#2-我正在尝试执行脚本块而不是外部脚本。@可以将脚本块作为参数传递给PowerShell.exe的位和字节(请参阅PowerShell.exe上的-Command参数)。无论您如何以异步方式运行脚本块,父PowerShell进程都会挂起以从脚本块中获取结果和错误。现在我想您可以使用Invoke命令-indconnectedsession,但是UI没有出现在您的桌面上。我不得不使用script方法-无法让scriptblock工作。谢谢你的帮助。