如何通过Excel VBA运行Powershell脚本 [英] how to run powershell script through excel VBA

查看:164
本文介绍了如何通过Excel VBA运行Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果满足特定条件,我将尝试使用Excel VBA宏启动PowerShell脚本.运行该代码时,我没有出现任何错误,并且能够从Windows资源管理器窗口中手动运行PowerShell脚本,而不会出现错误.但是,当我执行下面的VBA代码时,PowerShell无法运行,我不确定为什么.

I am trying to use an Excel VBA macro to launch a PowerShell script if a certain condition is met. I'm not getting any errors in the code when it runs and I am able to manually run the PowerShell script from the Windows Explorer window without errors. However, when I execute the below VBA code, the PowerShell does not run and I am not sure why.

Dim x as Variant
If rs.RecordCount = 0 Then
    x = Shell("POWERSHELL.exe " & "H:\MyFolder\MyFile.ps1", 1)
End If

我无法确定VBA代码中是否有错误,因为我没有遇到任何运行时错误,但PowerShell脚本实际上并未运行

I can't tell if something in the VBA code is wrong because I'm not getting any run time errors but the PowerShell script is not actually running

更新:使用额外的引号,我现在可以看到错误消息,该错误消息正在cmd窗口中弹出,但是即使使用额外的引号,它仍然存在空间问题.我已经将脚本移动到了没有空格的其他文件路径,但是现在看到了正在运行的脚本被禁用的错误.看来这不再是基于代码的问题.谢谢大家!

UPDATE: with the extra quotes I am able to see the error message now that's popping up in the cmd window but it is still having issues with the space even with the extra quotes. I have moved my script to a different file path that doesn't have spaces but I now seeing errors that running scripts are disabled. This seems like it is no longer a code based problem. thank you all!

推荐答案

如果文件路径中有空格,则需要在其两边加上引号.

If your file path has spaces you need to put quotes around it.

x = Shell("POWERSHELL.exe -noexit " & _
           """H:\Operations\REPORTS\Reports2018\Balance Sheet\SLmarginJE.ps1""", 1) 

这篇关于如何通过Excel VBA运行Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆