从VBA调用Powershell脚本(带参数)

Luc*_*caS 5 excel powershell vba

我想从Excel VBA调用Powershell-Script并传递1个参数.如果没有VBA,脚本可以完美运行并完成它应该做的事情.一旦Call工作,我想添加1个参数,但首先成功调用...

但我无法在Excel VBA中调用它.第一次看到我的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..."
Run Code Online (Sandbox Code Playgroud)

在VBA中,我创建了此代码:

strCommand = "Powershell.exe -ExecutionPolicy ByPass -NoExit -File """ & BasicPath & """, 1"
Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

我试过了

-ExecutionPolicy Unrestricted AND -ExecutionPolicy ByPass 
Run Code Online (Sandbox Code Playgroud)

我有没有尝试过

-NoExit
Run Code Online (Sandbox Code Playgroud)

我也试过了

Shell (strCommand)
Run Code Online (Sandbox Code Playgroud)

正如我所提到的,Scripts在没有VBA的情况下完美运行!有人可以帮忙吗?

Luc*_*caS 5

我找到了解决方案!!

实际上来自@Pᴇʜ的评论引导我找到答案!

我混淆了

Set WsShell = CreateObject("WScript.Shell")
WsShell.Run (strCommand)
Run Code Online (Sandbox Code Playgroud)

Shell (strCommand)
Run Code Online (Sandbox Code Playgroud)

",1"仅适用于Shell命令.

我只需要改变我的strCommand

    strCommand = "Powershell.exe -ExecutionPolicy ByPass -NoExit -File " & BasicPath
    Set WsShell = CreateObject("WScript.Shell")
    WsShell.Run (strCommand)
Run Code Online (Sandbox Code Playgroud)

和它的工作!

现在我可以尝试将参数传递给powershell!非常感谢!