如何在VBA中调用PowerShell命令?

时间:2018-09-18 17:24:24

标签: vba powershell access-vba

我有以下经过测试且可在PowerShell中运行的命令:

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

在Microsoft Access中,我尝试执行此PowerShell命令的位置具有以下VBA代码,但它没有按预期方式创建文本文件:

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,并且它执行无误。这使我相信PowerShell命令的格式可能不适用于VBA。

1 个答案:

答案 0 :(得分:0)

请勿混合PS和cmd。将> test918.txt替换为| Out-File test918.txt