简体   繁体   中英

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. 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. And be able to just have an open PS window which will track the progress within the script itself.

Expected results would be starting a script in powershell.exe session and passing 3 arguments - 2 strings and 1 boolean value. Which are provided by the user.

So in 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.

Where test.ps1 is

$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.

And a different approach which also nets in the script starts but not passing the 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. 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

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

Start-Process powershell.exe -ArgumentList $testarg

Where test.ps1 is:

param(
$ComputerName,
$UserName,
$citrixtest
)

$ComputerName 
$UserName 
$CitrixTest 
pause

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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