Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA中调用PowerShell命令?_Powershell_Ms Access_Vba - Fatal编程技术网

如何在VBA中调用PowerShell命令?

如何在VBA中调用PowerShell命令?,powershell,ms-access,vba,Powershell,Ms 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命令时使用了以下V

我有以下在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。

不要混合使用PS和cmd。将
>test918.txt
替换为
| Out文件test918.txt

Shell(pscmd,vbNormalFocus)
执行PowerShell脚本并将结果存储到
RetVal
中,我假定这是某个局部变量,您正在测试错误代码,以了解脚本是否成功。如果没有
Shell
语句,它就无法“正常运行”-如果没有
Shell
语句,您所做的一切就是为某个字符串变量分配字符串文字值。实际上,Shell()语句在分配给变量时执行。[pscmd=“PowerShell.exe”asdas'>thisistest.txt“]确实按预期创建了文件。如果我删除赋值并将代码更改为Shell(pscmd),我仍然会遇到同样的问题。使用VBNormalFocust时需要赋值如果不捕获返回值,则不需要参数
pscmd=“foobar”
不创建或执行任何内容,它只是分配一个字符串文本。这让人很困惑。你能不能请你的问题来澄清到底是什么代码不起作用?只是更新了它-我不相信Shell调用有什么问题,因为我已经用其他语句测试过它(parens没有什么区别)。我很确定问题出在PowerShell命令本身,因为这里只涉及一个参数,所以parens没有什么区别。如果您查看VBE中的实际代码,它是
Shell(pscmd)
而不是
Shell(pscmd)
(注意空格)-这是VBE告诉您paren不是参数列表的一部分,而是其中的表达式将作为值进行计算,然后,该值将被传递给被调用的过程,所以是的,它是有效的。但不,它并不是什么都不做,它可能会引入难以诊断的错误,并且在有多个参数的情况下,会导致编译错误。避免过程调用中的无关参数。很好,谢谢。不幸的是,这并没有解决我的问题