首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Windows 10中从VBA调用PowerShell脚本文件

在Windows 10中从VBA调用PowerShell脚本文件
EN

Stack Overflow用户
提问于 2020-03-19 12:12:55
回答 1查看 1K关注 0票数 0

从一段时间以来,我一直在使用这段代码,而且它以前也起过作用。

代码语言:javascript
运行
复制
Sub Call_PowerShell_Script_File_From_Excel_VBA()
'Windows PowerShell ISE >> New Script >> Create 'Hello World.ps1'
'    echo "Hello World"
'    $x = 1 + 1
'    echo $x
'----------------------------------------------------------------
Dim wshShell        As Object
Dim wshShellExec    As Object
Dim strCommand      As String
Dim strOutput       As String

strCommand = "Powershell.exe -File ""C:\Users\Future\Desktop\Hello World.ps1"""
Set wshShell = CreateObject("WScript.Shell")
Set wshShellExec = wshShell.Exec(strCommand)
strOutput = wshShellExec.StdOut.ReadAll

MsgBox strOutput
End Sub

我当时在Windows 7上工作,代码没有问题。安装Windows 10后,我发现代码不工作,没有输出任何想法的空白消息??

**在测试Tim的代码时,我收到了以下消息

代码语言:javascript
运行
复制
StdOut:       
StdErr:       File C:\Users\Future\Desktop\Hello World.ps1 cannot be loaded 
because running scripts is disabled on this system. For 
more information, see about_Execution_Policies at 
https:/go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo          : SecurityError: (:) [], 
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-19 16:38:36

试试这个,看看你得到了什么:

代码语言:javascript
运行
复制
Sub Call_PowerShell_Script_File_From_Excel_VBA()
    'Windows PowerShell ISE >> New Script >> Create 'Hello World.ps1'
    '    echo "Hello World"
    '    $x = 1 + 1
    '    echo $x
    '----------------------------------------------------------------
    Dim wshShell        As Object
    Dim wshShellExec    As Object
    Dim strCommand      As String
    Dim strOutput

    strCommand = "Powershell.exe -File ""C:\Users\Future\Desktop\Hello World.ps1"""
    Set wshShell = CreateObject("WScript.Shell")
    Set wshShellExec = wshShell.Exec(strCommand)
    strOutput = wshShellExec.StdOut.ReadAll()
    Debug.Print "StdOut:", strOutput

    strOutput = wshShellExec.StdErr.ReadAll()
    Debug.Print "StdErr:", strOutput


End Sub
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60757025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档