While you can do almost everything in native PowerShell code, there may be occasions when it is more convenient to use an existing (external) console-mode program and utilize PowerShell as a "wrapper" to control flow and collect results. The "Invoke-Expression" cmdlet (shortcut alias "iex") make this easy.

Let's try this with the Microsoft PortQry utility. PortQry is used to query a host to determine if it is actively listening on a particular TCP/IP port. Port 80 is the default port used for the http service, so if we find a host listening on it, it's a fairly safe bet that there is a web-server process running on that host.

Moving forward, we will use invoke-expression (aka iex) to run the external program portqry.exe to determine if a host is listening on port 80 and capture results into a variable called $result.

?View Code POWERSHELL
PS C:\PowerShell> $result = iex "cmd.exe /c `"portqry -n localhost -o 80`""

We can view the result by simply typing the $result variable name

PS C:\PowerShell> $result
Querying target system called:
 localhost
Attempting to resolve name to IP address...
Name resolved to 127.0.0.1
querying...
TCP port 80 (http service): LISTENING
PS C:\PowerShell>

Notice that the output is spread over multiple lines? That's fine for a single query, but if we wanted to run multiple iterations of PortQry against several hosts, this format could become difficult to read. We can use PowerShell to run the PortQry program, collect the output, and create a single-line result in CSV-format,  giving us much greater flexibility to analyze and use the data at a later time.

Here is a simple script that will do just that – we will call it Check-Port.ps1.

?View Code POWERSHELL
function RunPortQry($hostname)
    {
    $result = iex "cmd.exe /c `"portqry -n $hostname -o 80`""
    $ip =""
    $status=""
    foreach ($line in $result)
        {
        $line=$line.trim()
        if ($line.contains("Name resolved to"))
            {$ip = $line.replace("Name resolved to","").trim()}
        if ($line.contains("port 80"))
            {$status = $line.split(":")[1].trim()}
        }
    "$hostname,$ip,$status"
    }
"Host,IP Address,Port 80 Status"
RunPortQry "localhost"

Let's try out the script and see what happens:

PS C:\powershell> ./check-port
Host,IP Address,Port 80 Status
localhost,127.0.0.1,LISTENING
PS C:\powershell>

It's that simple. To make this really useful, simply plug the RunPortQry function into the script template posted earlier and now you have a full-featured port query script.

  One Response to “Run External Commands through PowerShell”

  1. Hi

    Is that a backtick before the number 80 ?? What is it needed for ?

    TIA

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
© 2011 Posh TipsSuffusion theme by Sayontan Sinha

Page optimized by WP Minify WordPress Plugin