【问题标题】:Call PowerShell script from VBScript but windows close从 VBScript 调用 PowerShell 脚本但窗口关闭
【发布时间】:2016-07-17 02:52:50
【问题描述】:

这个 VBScript 运行一些 PowerShell 脚本。我将notepad.exe 的调用放在那里以确保它以管理员身份运行。

我的 PowerShell 脚本会打开控制台,但随后会立即关闭。
我想让它们保持打开状态。

怎么了?

这是我的 VBScript:

RunAsAdmin()

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file .\Source\RemoveWindows10Apps-2.0.ps1"), 1 , True

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file .\Source\ChangeWin10StartLayout.ps1"), 1 , True

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file .\Source\NewFolder.ps1"), 1 , True

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("notepad.exe"), 1 , True


Function RunAsAdmin()
  Dim objAPP
  If WScript.Arguments.length = 0 Then
    Set objAPP = CreateObject("Shell.Application")
    objAPP.ShellExecute "wscript.exe", """" & _
    WScript.ScriptFullName & """" & " RunAsAdministrator",,"runas", 1
    WScript.Quit
  End If
End Function

【问题讨论】:

标签: powershell vbscript


【解决方案1】:

我猜是因为您使用相对路径,所以当您的脚本将自己称为管理员时,您的当前目录将更改为 ...\WINDOWS\system32

尝试构建绝对路径并像这样将它们传递给 PowerShell

RunAsAdmin()

strScriptPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strScriptPath )
strScriptFolder = objFSO.GetParentFolderName(objFile) 

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file " & strScriptFolder & "\Source\RemoveWindows10Apps-2.0.ps1"), 1 , True

...

【讨论】:

  • 谢谢你修复它。
猜你喜欢
  • 2016-10-22
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多