从Excel运行powershell脚本,路径中有空格



我在这里找到了几个答案,它们打破了Powershell和脚本之间的空白。

Sub Test()
Dim scriptlocation As String
scriptlocation = "C:REDACTEDPowershell ScriptsConmonInfoFinder-V7.ps1"
Dim x As Variant
x = Shell("""POWERSHELL.exe"" ""C:REDACTEDPowershell ScriptsConmonInfoFinder-V7.ps1""", vbNormalFocus)
End Sub

Sub Test()
Dim scriptlocation As String
scriptlocation = "C:REDACTEDPowershell ScriptsPowershell ScriptsConmonInfoFinder-V7.ps1"
strCommand = "Powershell -File" & scriptlocation
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
End Sub

这两个都打开了Powershell,但它们不会运行脚本。第一个在Powershell脚本之间的空间中死亡,第二个在没有错误的情况下闪烁窗口,但没有运行脚本。

编辑:我做了一些改变。

Sub Test()
Dim scriptlocation As String
scriptlocation = "C:REDACTEDPowershell Scriptstrial.ps1"
strCommand = "Powershell -NoExit -File """ & scriptlocation & """"
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)

窗户仍然闪烁着,然后就消失了。我制作了一个《你好世界》的样本脚本,以确保我的脚本中没有任何奇怪的东西,我也得到了同样的结果。

编辑:

现在已修复。

Sub Test2()
Dim scriptlocation As String
scriptlocation = "C:REDACTEDPowershell ScriptsConmonInfoFinder-V7.ps1"
strCommand = "Powershell -ExecutionPolicy Bypass -NoExit -File """ & scriptlocation & """"
Set WshShell = CreateObject("WScript.Shell")
WshShell.Exec (strCommand)
End Sub
strCommand = "Powershell -File" & scriptlocation

应该是

strCommand = "Powershell -File """ & scriptlocation & """"

如果你想保持窗口打开,可以使用-noexit标志,这样你就可以看到发生了什么。

未设置执行策略,因此脚本不会在默认的ExecutionPolicy设置中运行。命令应为:

strCommand = "powershell -ep Bypass -F " & scriptlocation

最新更新