如何在VBA中调用PowerShell命令?

问题描述 投票:2回答:1

我有以下命令在PowerShell中测试并运行:

(new-object -com shell.application).windows() 
    | where {$_.Type  -eq ""HTML Document"" -and $_.LocationURL -match ""$locationurmatch""} 
    | select -last 1 > test918.txt

在Microsoft Access中,我有以下VBA代码,我尝试执行此PowerShell命令,但它不会按预期创建文本文件:

pscmd = "PowerShell.exe (new-object -com shell.application).windows() | where {$_.Type  -eq ""HTML Document"" -and $_.LocationURL -match ""$locationurmatch""} | select -last 1 > test918.txt"
Shell(pscmd)

但是,以下代码确实使用打印文本创建输出文件:

pscmd = "PowerShell.exe 'test' > thisisatest.txt"
Shell(pscmd)

我已将'pscmd'打印到调试器,并将结果复制并粘贴到PowerShell,并且执行时没有错误。这让我相信,对于VBA,PowerShell命令可能没有正确格式化。

vba powershell access-vba
1个回答
0
投票

不要混用PS和cmd。用> test918.txt替换| Out-File test918.txt

© www.soinside.com 2019 - 2024. All rights reserved.