简体   繁体   English

如何通过 VBS 将变量从一个 PS 脚本传递到另一个脚本?

[英]How can I pass a variable from one PS script to another via VBS?

I am working on a PowerShell script with a GUI with 3 files:我正在处理带有 3 个文件的 GUI 的 PowerShell 脚本:

  • window1.ps1 - input into variable and starts start.vbs window1.ps1 - 输入变量并启动start.vbs
  • start.vbs - starts window2.ps1 start.vbs - 启动window2.ps1
  • window2.ps1 - displays the value entered in window1 window2.ps1 - 显示在 window1 中输入的值

In window1, I enter value1 into textbox1 and click the button .在 window1 中,我在textbox1输入value1并单击button

图形界面 1

By clicking it, value1 should be stored into a global variable , so that window2 can pop up (via VBS) and use value1 .通过点击它, value1应该被存储到一个global variable ,这样window2就可以(通过 VBS)弹出并使用value1

code for button in window1 : window1 button代码:

$button_OnClick= 
{
    $global:value1 = $textBox1.Text
    start-process .\start.vbs
}

start.vbs:开始.vbs:

Set objShell = CreateObject("Wscript.Shell")
objShell.run("powershell.exe -nologo -file .\window2.ps1"), 0, True

So far so good.到现在为止还挺好。 From what I understand, value1 is a global variable and can be used in window2.ps1 .据我了解, value1是一个全局变量,可以在window2.ps1

So window2 is an empty window with only a text that should display value1 .所以window2是一个空窗口,只有一个文本应该显示value1

$label1.Text = "$value1"

But for some reason, the variable is empty and window2 is empty.但由于某种原因,变量为空, window2为空。

图形界面 2

What am I missing?我错过了什么? I'm still learning PowerShell and I might be missing a detail.我仍在学习 PowerShell,我可能会遗漏一个细节。

note: I know the way I open up window2 via .vbs is weird.注意:我知道我通过 .vbs 打开window2的方式很奇怪。 But that way, the GUI opens up without any PowerShell window in the background.但是这样一来,GUI 就会在后台打开而没有任何 PowerShell 窗口。

A "global variable" is not something that is magically available to every process all over your computer. “全局变量”并不是您计算机上的每个进程都可以神奇地使用的东西。 You're starting two completely separate PowerShell processes.您正在启动两个完全独立的 PowerShell 进程。 None of their stuff is shared.他们的东西都没有共享。

You need to give the value as a command line argument to the second PowerShell process if you want to use it there.如果要在第二个 PowerShell 进程中使用该值,则需要将该值作为命令行参数提供。

And that means you have to give the value your first PowerShell script generates as a command line argument to your intermediary VBScript, because that's a completely separate process, too.这意味着您必须将第一个 PowerShell 脚本生成的值作为命令行参数提供给中间 VBScript,因为这也是一个完全独立的过程。

The first PowerShell script calls wscript.exe with two arguments (script name and value):第一个 PowerShell 脚本使用两个参数(脚本名称和值)调用 wscript.exe:

$button_OnClick= 
{
    $value1 = $textBox1.Text
    Start-Process wscript.exe "window2.vbs","`"$value1`""
}

Assuming the user has entered Hello World!假设用户已经进入Hello World! , this will execute the command line (note the quotes, they make sure that values with spaces are not seen as separate arguments): ,这将执行命令行(注意引号,它们确保带空格的值不被视为单独的参数):

wscript.exe window2.vbs "Hello World!"

The VBS intermediary can then read Hello World!然后 VBS 中介可以读取Hello World! from it first command line argument (the quotes will have been stripped, so we need to add them back for the next call):来自它的第一个命令行参数(引号将被删除,因此我们需要将它们添加回下一次调用):

value1 = WScript.Arguments.Unnamed(0)

Set objShell = CreateObject("Wscript.Shell")
objShell.run("powershell.exe -nologo -file .\window2.ps1 """ & value1 & """"), 0, True

now the following command line is executed:现在执行以下命令行:

powershell.exe -nologo -file .\window2.ps1 "Hello World!"

The second PowerShell script can then pull Hello World!然后第二个 PowerShell 脚本可以拉出Hello World! from its first command line argument, just as the VBScript did (the quotes will have been stripped again):从它的第一个命令行参数开始,就像 VBScript 一样(引号将再次被删除):

$label1.Text = $args[0]

I would recommend that you make a VBScript windowless.vbs that can run just about any command windowlessly:我建议您制作一个 VBScript windowless.vbs ,它可以windowless.vbs地运行几乎任何命令:

' windowless.vbs
For Each arg In WScript.Arguments
    If InStr(arg, " ") > 0 Then
        cmd = cmd & """" & arg & """ "
    Else
        cmd = cmd & arg & " "
    End If
Next
CreateObject("WScript.Shell").Run(Trim(cmd)), 0, True

and then use it in your PowerShell script然后在你的 PowerShell 脚本中使用它

$value1 = "Hello World!"
Start-Process wscript.exe "windowless.vbs","powershell.exe","-nologo","-file window2.ps1","`"$value1`""

which would create this command line这将创建此命令行

powershell.exe -nologo -file .\window2.ps1 "Hello World!"

which is exactly what we need here, and it can be reused easily for other cases.这正是我们在这里所需要的,并且可以轻松地在其他情况下重用。

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

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