1

I've got a VBA script I wrote in Excel and it launches a .ps1 powershell script that's located elsewhere. The VB script takes input from a cell in the document and uses that cells contents as a string to run a command in a powershell script. For example, if cell A1 contains the string "whoami", it will run the whoami command in powershell, simple as that.

Now when I launch a powershell manually and type "nmap 127.0.0.1" the behavior is as I expect, it runs nmap on localhost. My issue is when I try to run nmap using my VB script, the VB-launched powershell doesn't recognize the nmap command, returning this error:

The term 'nmap' is not recognized as the name of a cmdlet, function, script file, or operable program.

The only noticable difference in the manually launched shell and the one from excel is the background, the VB-launched shell has a black background and the manually launched shell has a blue background. Other than that the paths to the exe's are the same.

Why can't my VisualBasic script launch a powershell that can run nmap?

I'll come back and edit this post with source code when I return to the PC that has all the code on it.

EDIT1: Here's the source for the VBA script:

a = Range("A1")
b = Range("A2")
Call Shell("powershell -noexit -file ""C:\...\test.ps1"" -cmd " + a + " -ipaddr " + b, vbMaximizedFocus)

This simply gets the values from A1 (the command) and A2 (the IP address).

Here is the powershell script that gets called above (test.ps1):

param (
[string]$cmd = "ERRORNOCMD",
[string]$ipaddr = "0.0.0.0"
)
[string]$execute = $cmd + " " + $ipaddr
invoke-expression $execute

This uses the command "nmap" with localhost IP. So as I've said above it should execute properly, however it dosen't recognize nmap.

Here is a link to a screenshot of the results of running the command "nmap 127.0.0.1" on a manually started shell and the VBA shell (blue is manual). Don't worry about what's under the black boxes, not important stuff!

screenshot

4

0 回答 0