从VBA调用Powershell脚本(带参数) [英] Call Powershell Script from VBA(with Parameter)
问题描述
我想从Excel VBA调用Powershell脚本并传递1参数.如果没有VBA,则脚本可以正常运行,并且可以执行它应做的事情..一旦调用成功,我想添加1个参数,但首先要成功调用...
I want to call a Powershell-Script from Excel VBA and pass 1 Parameter. Without VBA the Script works perfectly and does what it supposed to do.. As soon as the Call works, i would like to add 1 parameter, but first the successfull call...
但是我无法在Excel VBA中调用它.首先,请告诉我我的Powershell脚本:
But i cant manage to call it within Excel VBA. First ill show you my Powershell-Script:
#param([string]$server)
$server = "chvmes01"
$BasicPath = Split-Path $script:MyInvocation.MyCommand.Path
write-host $BasicPath
Invoke-Command -FilePath $BasicPath\IISReport.ps1 -ComputerName $server
Read-Host "Return drücken..."
在VBA中,我创建了以下代码:
In VBA I created this Code:
strCommand = "Powershell.exe -ExecutionPolicy ByPass -NoExit -File """ & BasicPath & """, 1"
Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
strCommand看起来像这样:
strCommand looks like this:
Powershell.exe -ExecutionPolicy不受限制-NoExit-文件"E:\ Temp \ Registry \ EXCEL_ALLE \ Version_AllFromExcel_Aktuell \ IISReport \ InvokeCommand.ps1",1
Powershell.exe -ExecutionPolicy Unrestricted -NoExit -File "E:\Temp\Registry\EXCEL_ALLE\Version_AllFromExcel_Aktuell\IISReport\InvokeCommand.ps1", 1
就像这样,我得到以下错误:
Like this i get the following Error:
我不知道我可以更改什么,我读了很多论坛帖子并更改了不同的内容,但是没有任何效果!
I have no idea what i can change anymore, i read so many forum posts and changed different things, but nothing worked!
我尝试了不带"的strCommand,如下所示:
I tried with strCommand without "" like this:
strCommand = "Powershell.exe -ExecutionPolicy ByPass -NoExit -File " & BasicPath & ", 1"
我尝试了
-ExecutionPolicy Unrestricted AND -ExecutionPolicy ByPass
我尝试了有无
-NoExit
我也尝试过
Shell (strCommand)
正如我所提到的,在没有VBA的情况下,脚本可以完美运行!有人可以帮忙吗?
As i mentioned, the Scripts work perfectly without VBA! Can anyone help here?
推荐答案
我找到了解决方案!
实际上@Pᴇʜ的评论将我引向了答案!
Actually the comment from @Pᴇʜ lead me to the answer!
我把
Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
使用
Shell (strCommand)
,1"仅用于Shell命令.
The ", 1" is only for the Shell Command.
我只需要将strCommand更改为
I only had to change my strCommand to
strCommand = "Powershell.exe -ExecutionPolicy ByPass -NoExit -File " & BasicPath
Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
及其工作!
现在,我可以尝试将参数传递给powershell!非常感谢!
Now i can try to pass the parameter to powershell! Thanks a lot!
这篇关于从VBA调用Powershell脚本(带参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!