简体   繁体   English

启动进程和 powershell.exe 与飞溅

[英]Start-Process and powershell.exe with splatting

I've been trying for a couple of days now to multi-thread a WPF GUI which will run a PS3.0 script once the button has been clicked.几天来,我一直在尝试多线程 WPF GUI,一旦单击按钮,它将运行 PS3.0 脚本。 I cannot use start-job as that I would have to track (multiple sessions at once), however, I would like to just run the script in a separate process of PS- as if I were to open multiple instances of the script from a shortcut.我不能使用 start-job,因为我必须跟踪(一次多个会话),但是,我想只在 PS 的单独进程中运行脚本 - 就好像我要从一个捷径。 And be able to just have an open PS window which will track the progress within the script itself.并且能够只拥有一个打开的 PS window,它将跟踪脚本本身的进度。

Expected results would be starting a script in powershell.exe session and passing 3 arguments - 2 strings and 1 boolean value.预期的结果将是在 powershell.exe session 中启动一个脚本并传递 3 个 arguments 值 - 2 个字符串和 1 个 Z824E2C64AB538CDAF。 Which are provided by the user.由用户提供。

So in ISE:所以在 ISE 中:

C:\temp\test.ps1 -argumentlist $computername $username $citrixtest

Works fine.工作正常。 I've spent a few hours scouring through the internet only to find a thread where a start-job was recommended or a way to use a background worker- this is not what I want from the script.我花了几个小时在互联网上搜索,只是为了找到一个推荐开始工作的线程或使用后台工作人员的方法——这不是我想要的脚本。

So I would guess the invocation from a button click would be something of the like (some of the things I have tried)所以我猜想点击按钮的调用会是类似的(我尝试过的一些事情)

$ComputerName = "testtext1"
$UserName = "testtext2"
$CitrixTest = $True

$command = "c:\temp\test.ps1"
$arg = @{
    Computername = "$computername";
    Username = "$username";
    CitrixTest = "$citrixtest"
}

#$WPFStartButton.Add_Click({
    Start-Process powershell -ArgumentList "-noexit -command & {$command} -argumentlist $arg"
#})

Does not pass arguments to test.ps1- it is, however, getting to the "pause" - so the script successfully launches.没有通过 arguments 到 test.ps1 - 但是,它正在进入“暂停” - 所以脚本成功启动。

Where test.ps1 is test.ps1 在哪里

$ComputerName 
$UserName 
$CitrixTest 
pause

Caller:呼叫者:

function Caller {
Param (
    $ScriptPath = "c:\temp\test.ps1"
)

$Arguments = @()
$Arguments += "-computername $ComputerName"
$Arguments += "-UserName $UserName"
$Arguments += "-citrixtest $citrixtest"
$StartParams = @{   
    ArgumentList = "-File ""$ScriptPath""" + $Arguments
}
    Start-Process powershell @StartParams
}
Caller

Does not start the script altogether- PS window just closes- possibly a path to.ps1 script not being found.完全不启动脚本 - PS window 刚刚关闭 - 可能找不到指向.ps1 脚本的路径。

And a different approach which also nets in the script starts but not passing the arguments而另一种方法也在脚本中启动但不通过 arguments

$scriptFile = '"C:\temp\test.ps1"'
[string[]]$argumentList = "-file"
$argumentList +=  $scriptFile
$argumentlist += $computername
$argumentlist += $UserName
$argumentlist += $CitrixTest

$start_Process_info =  New-Object System.Diagnostics.ProcessStartInfo
$start_Process_info.FileName = "$PSHOME\PowerShell.exe"
$start_Process_info.Arguments = $argumentList


$newProcess = New-Object System.Diagnostics.Process
$newProcess.StartInfo = $start_Process_info

$newProcess.Start() | Out-Null

Is there a way to make this work as I want it to?有没有办法按照我的意愿进行这项工作? Or should I just dig deeper into runspaces and try with that?还是我应该深入挖掘运行空间并尝试一下?

@Bill_Stewart I just realized I did not put the param(args) in my script... And that's why it would not pull those variables as I would like them to. @Bill_Stewart 我刚刚意识到我没有将 param(args) 放在我的脚本中......这就是为什么它不会像我希望的那样提取这些变量。 I will have to check when I'm back in the office if it's just that what I was missing.我将不得不检查当我回到办公室时是否正是我所缺少的。

Checked on my laptop that's running PS 5.1 and this seems to be working as intended检查运行 PS 5.1 的笔记本电脑,这似乎按预期工作

$testarg = @(
    '-File'
    "C:\temp\test.ps1"
    "$computername"
    "$username"
    "$citrixtest"
)

Start-Process powershell.exe -ArgumentList $testarg

Where test.ps1 is:其中 test.ps1 是:

param(
$ComputerName,
$UserName,
$citrixtest
)

$ComputerName 
$UserName 
$CitrixTest 
pause

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM