使用 VBS 运行 PowerShell
Use VBS to run PowerShell
所以我有一个创建表单的 .ps1 文件。
该表格需要 10-20 秒,具体取决于首次加载时的 PC 性能和连接。
现在我正在使用 VBS 加载一个简单的 .gif 文件作为加载屏幕,同时 运行 .ps1 文件紧随其后。
我现在的问题是,我想在弹出表单时关闭加载屏幕。我试图通过流程确定,但由于加载 powershell.exe 而失败,但表格需要 10 秒...
这可能吗?
你们有更好的主意吗?
Dim i
Dim strComputer
Dim FindProc
Dim counter
counter = 0
strComputer = "."
FindProc = "powershell.exe"
'Load the gif file
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.Visible = 1
.Document.Title = "Show Image"
.Toolbar=False
.Statusbar=False
.Top=400
.Left=400
.Height=355
.Width=435
.Document.Body.InnerHTML = "<img src='\10.10.67.173\Templates$\Scripts\Resources\loadingtest.gif'>"
End With
'Run the PS script
Set objShell = CreateObject("Wscript.shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -file \10.10.67.173\Templates$\Scripts\FormSignature-V0.9.5.ps1", 0, False
'Determine when to close Loading screen
Do While counter < 3
wscript.Sleep 2000
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 then
'Quit the process if it finds its running
WScript.Echo("found")
'objExplorer.quit
else
'Do nothing
WScript.Echo("not found")
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
counter = counter + 1
Loop
Set objShell = Nothing
在您的 VB 脚本中:
- 在 PS 脚本可以访问的文件夹中创建一个文件。
- 启动您的加载图像。
- 启动您的 PS 脚本。
- 在你的等待循环中,每隔一秒左右,检查文件是否仍然存在。
如果是,请关闭 IE 实例并退出脚本。
在您的 PS 脚本中:
- 完成表单初始化代码或加载表单后的第一个操作后,找到并删除文件。
所以我有一个创建表单的 .ps1 文件。 该表格需要 10-20 秒,具体取决于首次加载时的 PC 性能和连接。
现在我正在使用 VBS 加载一个简单的 .gif 文件作为加载屏幕,同时 运行 .ps1 文件紧随其后。 我现在的问题是,我想在弹出表单时关闭加载屏幕。我试图通过流程确定,但由于加载 powershell.exe 而失败,但表格需要 10 秒... 这可能吗? 你们有更好的主意吗?
Dim i
Dim strComputer
Dim FindProc
Dim counter
counter = 0
strComputer = "."
FindProc = "powershell.exe"
'Load the gif file
Set objExplorer = CreateObject("InternetExplorer.Application")
With objExplorer
.Navigate "about:blank"
.Visible = 1
.Document.Title = "Show Image"
.Toolbar=False
.Statusbar=False
.Top=400
.Left=400
.Height=355
.Width=435
.Document.Body.InnerHTML = "<img src='\10.10.67.173\Templates$\Scripts\Resources\loadingtest.gif'>"
End With
'Run the PS script
Set objShell = CreateObject("Wscript.shell")
objShell.Run "CMD /C START /B " & objShell.ExpandEnvironmentStrings("%SystemRoot%") & "\System32\WindowsPowerShell\v1.0\powershell.exe -executionpolicy bypass -file \10.10.67.173\Templates$\Scripts\FormSignature-V0.9.5.ps1", 0, False
'Determine when to close Loading screen
Do While counter < 3
wscript.Sleep 2000
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select Name from Win32_Process WHERE Name LIKE '" & FindProc & "%'")
If colProcessList.count>0 then
'Quit the process if it finds its running
WScript.Echo("found")
'objExplorer.quit
else
'Do nothing
WScript.Echo("not found")
End if
Set objWMIService = Nothing
Set colProcessList = Nothing
counter = counter + 1
Loop
Set objShell = Nothing
在您的 VB 脚本中:
- 在 PS 脚本可以访问的文件夹中创建一个文件。
- 启动您的加载图像。
- 启动您的 PS 脚本。
- 在你的等待循环中,每隔一秒左右,检查文件是否仍然存在。
如果是,请关闭 IE 实例并退出脚本。
在您的 PS 脚本中:
- 完成表单初始化代码或加载表单后的第一个操作后,找到并删除文件。