【发布时间】:2019-03-24 02:59:55
【问题描述】:
我想从 Excel VBA 调用 Powershell 脚本并传递 1 个参数。如果没有 VBA,脚本可以完美运行并执行它应该做的事情。一旦调用工作,我想添加 1 个参数,但首先是成功调用...
但我无法在 Excel VBA 中调用它。首先向您展示我的 Powershell 脚本:
#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 中我创建了这个代码:
strCommand = "Powershell.exe -ExecutionPolicy ByPass -NoExit -File """ & BasicPath & """, 1"
Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
strCommand 看起来像这样:
Powershell.exe -ExecutionPolicy Unrestricted -NoExit -File "E:\Temp\Registry\EXCEL_ALLE\Version_AllFromExcel_Aktuell\IISReport\InvokeCommand.ps1", 1
我不知道我可以改变什么了,我阅读了很多论坛帖子并改变了不同的东西,但没有任何效果!
我尝试使用 strCommand 没有 "" 像这样:
strCommand = "Powershell.exe -ExecutionPolicy ByPass -NoExit -File " & BasicPath & ", 1"
我试过了
-ExecutionPolicy Unrestricted AND -ExecutionPolicy ByPass
我尝试了有无
-NoExit
我也试过
Shell (strCommand)
正如我所提到的,脚本可以在没有 VBA 的情况下完美运行! 有人可以帮忙吗?
【问题讨论】:
-
我不是很喜欢 powershell,但是当你在 PS 脚本中将 #param([string]$server) 更改为 #param([$server) 会发生什么?
-
是的,我可以试试这个,但由于脚本不执行,我无法测试它是否有效。首先我们必须让执行工作...
-
为什么最后有个
", 1"? -
它为WindowStyle,1代表vbNormalFocus --> 窗口有焦点,恢复到原来的大小和位置。
-
我找到了解决方案,检查我的答案,你引导我找到答案!非常感谢..
标签: excel vba powershell