我已经尝试了几天了,现在尝试多线程的WPF图形用户界面,它将运行一个PS3.0脚本,一旦按钮被点击。我不能使用start-job,因为我必须跟踪(一次多个会话),但是,我想只在PS的单独进程中运行脚本-就像我要从快捷方式打开脚本的多个实例一样。并且能够只有一个打开的PS窗口,它将跟踪脚本本身的进度。
预期的结果将是在powershell.exe会话中启动脚本并传递3个参数-2个字符串和1个布尔值。其由用户提供。
因此,在ISE中:
C:\temp\test.ps1 -argumentlist $computername $username $citrixtest工作正常。我花了几个小时在互联网上搜寻,只是为了找到一个推荐开始工作的线程,或者是一种使用后台工作者的方法--这不是我想要的脚本。
所以我猜按钮点击的调用应该是类似的东西(我尝试过的一些东西)
$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"
#})不会将参数传递给test.ps1-但是,它正在进入“暂停”-因此脚本成功启动。
test.ps1在哪里
$ComputerName
$UserName
$CitrixTest
pause呼叫者:
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不会完全启动脚本- PS窗口只是关闭-可能是找不到.ps1脚本的路径。
还有一种不同的方法,脚本中的not也会启动,但不传递参数
$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有没有办法让它像我想要的那样工作?或者,我应该更深入地挖掘runspaces并尝试它吗?
发布于 2019-10-01 01:34:26
@Bill_Stewart我刚刚意识到我没有把参数(Args)放在我的脚本中...这就是为什么它不会像我希望的那样拉出这些变量。当我回到办公室时,我必须检查一下,是否只是我遗漏了这些。
我在运行PS 5.1的笔记本电脑上进行了检查,这似乎可以正常工作
$testarg = @(
'-File'
"C:\temp\test.ps1"
"$computername"
"$username"
"$citrixtest"
)
Start-Process powershell.exe -ArgumentList $testarg其中,test.ps1是:
param(
$ComputerName,
$UserName,
$citrixtest
)
$ComputerName
$UserName
$CitrixTest
pausehttps://stackoverflow.com/questions/58171937
复制相似问题