Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
Excel NMap赢得';无法在Visual Basic Powershell中运行_Excel_Vba_Powershell - Fatal编程技术网

Excel NMap赢得';无法在Visual Basic Powershell中运行

Excel NMap赢得';无法在Visual Basic Powershell中运行,excel,vba,powershell,Excel,Vba,Powershell,我在Excel中编写了一个VBA脚本,它启动了一个位于别处的.ps1 powershell脚本。VB脚本从文档中的单元格中获取输入,并将该单元格的内容用作字符串来运行powershell脚本中的命令。例如,如果单元格A1包含字符串“whoami”,它将在powershell中运行whoami命令,就这么简单 现在,当我手动启动powershell并键入“nmap 127.0.0.1”时,其行为与我预期的一样,它在本地主机上运行nmap。我的问题是,当我尝试使用VB脚本运行nmap时,VB启动的p

我在Excel中编写了一个VBA脚本,它启动了一个位于别处的.ps1 powershell脚本。VB脚本从文档中的单元格中获取输入,并将该单元格的内容用作字符串来运行powershell脚本中的命令。例如,如果单元格A1包含字符串
“whoami”
,它将在powershell中运行
whoami
命令,就这么简单

现在,当我手动启动powershell并键入
“nmap 127.0.0.1”
时,其行为与我预期的一样,它在本地主机上运行
nmap
。我的问题是,当我尝试使用VB脚本运行
nmap
时,VB启动的powershell无法识别
nmap
命令,返回以下错误:

术语“nmap”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。

手动启动的shell与excel中的shell唯一明显的区别是背景,VB启动的shell有黑色背景,手动启动的shell有蓝色背景。除此之外,exe的路径是相同的

为什么我的VisualBasic脚本不能启动可以运行
nmap
的powershell

当我回到有所有代码的电脑上时,我会回来用源代码编辑这篇文章

编辑1: 以下是VBA脚本的源代码:

a = Range("A1")
b = Range("A2")
Call Shell("powershell -noexit -file ""C:\...\test.ps1"" -cmd " + a + " -ipaddr " + b, vbMaximizedFocus)
这只是从A1(命令)和A2(IP地址)获取值

下面是上面调用的powershell脚本(test.ps1):

这将使用带有本地主机IP的命令“nmap”。正如我上面所说的,它应该正确执行,但是它不识别nmap

下面是一个链接,指向在手动启动的shell和VBA shell上运行命令“nmap 127.0.0.1”的结果屏幕截图(蓝色为手动)。不要担心黑匣子下面是什么,不是重要的东西


如果您在PS脚本中给出nmap的完整路径怎么办?@EBGreen我想了一下。。。但是我想知道为什么一个普通的powershell可以找到它,而从excel中的vba启动的powershell却找不到?@AndreSilva很抱歉在我得到源代码之前发布了这篇文章。我整个上午都在想这个问题,想尽快开始讨论。希望我能找到某种解决方案,因为问题中有更多细节。感谢您的回复!您是否尝试过将
a
b
用双引号括起来,例如
-cmd“+Chr$(34)+a+Chr$(34)+”-ipaddr
等等?
param (
[string]$cmd = "ERRORNOCMD",
[string]$ipaddr = "0.0.0.0"
)
[string]$execute = $cmd + " " + $ipaddr
invoke-expression $execute