Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Using pipe in Powershell

Reply
 
Old 07-28-2009   #1 (permalink)


windows xp x86 Corp
 
 

Using pipe in Powershell

Hi,

I'm writing a simple script to pipe data from one command to a text file. Here is the script.

$strpaths="drives.txt"
$path=Get-Content$strpaths
$i
=0
do {
$P= [WmiClass]"\\servername\ROOT\CIMV2:Win32_Process"
$P.create("nmap -p 17988 -oG - 10.72.252.0/24 ")| "findstr /C:"1 20" > "$(($path[$i]).substring(72, 6))$(($path[$i]).substring(74, 1)).txt"
$i ++
} until ($i -eq $path.count)



When I run the script, I get below message:

Expressions are only permitted as the first element of a pipeline.
At :line:6 char:99
+ $P.create("nmap -p 17988 -oG - 10.72.252.0/24 ")| "findstr /C:"1 <<<< 20" > "$(($path[$i]).substring(72, 6))$(($path[$i]).substring(74, 1)).txt"

Is there any other way I can pipe the data to "findstr" command if I can't use the pipeline? Thanks for any help.

My System SpecsSystem Spec
Old 07-28-2009   #2 (permalink)
Alex K. Angelopoulos


 
 

Re: Using pipe in Powershell

As it showed up in post, this script won't work at all - I see \\\ sequences
in several places. Assuming that I can just knock those out, the problem
comes down to your line beginning with $P.Create(...). I see possible syntax
problems in the process you're trying to create, but assuming that it
worked, Win32_Process' Create method returns an integer value indicating
either process creation success. You _could_ pipe this integer into findstr,
but it looks like you're piping it into a string, since you have "" marks
around the findstr C:\.. bit.

If you're attempting to retrieve literal output from a remotely executed
command, that won't work this way. Could you describe what you're trying to
accomplish? And also, repost the original script; I'm not sure what
happened, but it's not legal PowerShell as it was posted.

"jonab" <guest@xxxxxx-email.com> wrote in message
news:ddea4de05e64f246e2f71520489fc8e1@xxxxxx-gateway.com...
Quote:

>
> Hi,
>
> I'm writing a simple script to pipe data from one command to a text
> file. Here is the script.
>
> $strpaths = \\\"drives.txt\\\"
> $path = *Get-Content* $strpaths
> $i=0
> do {
> $P = [WmiClass]\\\"\\servername\ROOT\CIMV2:Win32_Process\\\"
> $P.create(\\\"-nmap -p 17988 -oG - 10.32.252.0/24 | findstr open-\\\")|
> \\\"findstr /C:\\\"1 20\\\" > \\\"$(($path[$i]).substring(72,
> 6))$(($path[$i]).substring(74, 1)).txt\\\"
> $i ++
> } until ($i -eq $path.count)
>
>
>
> When I run the script, I get below message:
>
> Expressions are only permitted as the first element of a pipeline.
> At :line:6 char:99
> + $P.create(\\\\\"-nmap -p 17988 -oG - 10.32.252.0/24 |findstr
> open-\\\\\")| \\\\\"findstr /C:\\\\\"1 <<<< 20\\\\\" >
> \\\\\"$(($path[$i]).substring(72, 6))$(($path[$i]).substring(74,
> 1)).txt\\\\\"
>
> Is there any other way I can pipe the data to \\\\\"findstr\\\\\"
> command if I can't use the pipeline? Thanks for any help.
>
>
> --
> jonab
My System SpecsSystem Spec
Old 07-28-2009   #3 (permalink)


windows xp x86 Corp
 
 

Re: Using pipe in Powershell

Strange. Looking at my original post, I don't see the /// sequence but I do see them in your post. Here is the original code. Hopefully this one is better.


$strpaths="drives.txt"
$path=Get-Content$strpaths
$i=0
do {
$P= [WmiClass]"\\servername\ROOT\CIMV2:Win32_Process"
$P.create("nmap -p 17988 -oG - 10.72.252.0/24 ")| "findstr /C:"1 20" > "$(($path[$i]).substring(72, 6))$(($path[$i]).substring(74, 1)).txt"
$i ++
} until ($i -eq $path.count)


What I'm trying to achieve is to use nmap and search for a specific subnet for any machines using HP iLO. iLO uses port number 17988. Output from nMap included all devices on the subnet. I want to use "finstr" to search for lines that contains "1 20".

I see what your saying that my command isn't giving me literal output from nMap. What do I need to use to have the literal output? When I do have the literal output, how do I pipe it to findstr so I can search for lines with specific criteria?

Thank you for your help with this.

My System SpecsSystem Spec
Old 07-28-2009   #4 (permalink)
Alex K. Angelopoulos


 
 

Re: Using pipe in Powershell

It still comes through munged (using a newsreader) and to make things even
worse, when I try to find your post via
http://social.technet.microsoft.com/...rverpowershell
it doesn't even show up.
Could you post the web URL you use to find your post? It sounds like this is
a problem with how Microsoft translates from web display. Sorry for the
inconvenience.

"jonab" <guest@xxxxxx-email.com> wrote in message
news:d75f2f4f8d775d95b7f5660384ae1d87@xxxxxx-gateway.com...
Quote:

>
> Strange. Looking at my original post, I don't see the /// sequence but
> I do see them in your post. Here is the original code. Hopefully this
> one is better.
>
>
> $strpaths=\\"drives.txt\\"
> $path=Get-Content$strpaths
> $i=0
> do {
> $P= [WmiClass]\\"'\\servername\ROOT\CIMV2:Win32_Process'
> (file://\\servername\ROOT\CIMV2:Win32_Process)\\"
> $P.create(\\"nmap -p 17988 -oG - 10.72.252.0/24 \\")| \\"findstr
> /C:\\"1 20\\" > \\"$(($path[$i]).substring(72,
> 6))$(($path[$i]).substring(74, 1)).txt\\"
> $i ++
> } until ($i -eq $path.count)
>
>
> What I'm trying to achieve is to use nmap and search for a specific
> subnet for any machines using HP iLO. iLO uses port number 17988.
> Output from nMap included all devices on the subnet. I want to use
> \\"finstr\\" to search for lines that contains \\"1 20\\".
>
> I see what your saying that my command isn't giving me literal output
> from nMap. What do I need to use to have the literal output? When I do
> have the literal output, how do I pipe it to findstr so I can search for
> lines with specific criteria?
>
> Thank you for your help with this.
>
>
> --
> jonab
My System SpecsSystem Spec
Old 07-28-2009   #5 (permalink)
Alex K. Angelopoulos


 
 

Re: Using pipe in Powershell

I think I have it; it looks like your post is getting mangled on its way to
the Microsoft site - in particular, " gets escaped to \\" for some reason.
I've got this mostly demangled and I'm testing.

"jonab" <guest@xxxxxx-email.com> wrote in message
news:d75f2f4f8d775d95b7f5660384ae1d87@xxxxxx-gateway.com...
Quote:

>
> Strange. Looking at my original post, I don't see the /// sequence but
> I do see them in your post. Here is the original code. Hopefully this
> one is better.
>
>
> $strpaths=\\"drives.txt\\"
> $path=Get-Content$strpaths
> $i=0
> do {
> $P= [WmiClass]\\"'\\servername\ROOT\CIMV2:Win32_Process'
> (file://\\servername\ROOT\CIMV2:Win32_Process)\\"
> $P.create(\\"nmap -p 17988 -oG - 10.72.252.0/24 \\")| \\"findstr
> /C:\\"1 20\\" > \\"$(($path[$i]).substring(72,
> 6))$(($path[$i]).substring(74, 1)).txt\\"
> $i ++
> } until ($i -eq $path.count)
>
>
> What I'm trying to achieve is to use nmap and search for a specific
> subnet for any machines using HP iLO. iLO uses port number 17988.
> Output from nMap included all devices on the subnet. I want to use
> \\"finstr\\" to search for lines that contains \\"1 20\\".
>
> I see what your saying that my command isn't giving me literal output
> from nMap. What do I need to use to have the literal output? When I do
> have the literal output, how do I pipe it to findstr so I can search for
> lines with specific criteria?
>
> Thank you for your help with this.
>
>
> --
> jonab
My System SpecsSystem Spec
Old 07-28-2009   #6 (permalink)


windows xp x86 Corp
 
 

Re: Using pipe in Powershell

Quote  Quote: Originally Posted by Alex K. Angelopoulos View Post
Could you post the web URL you use to find your post? It sounds like this is
a problem with how Microsoft translates from web display.
Thank you Alex for the help. I'm posting from vistax64.com. Here is the post.

Using pipe in Powershell
My System SpecsSystem Spec
Old 07-28-2009   #7 (permalink)
Alex K. Angelopoulos


 
 

Re: Using pipe in Powershell

This is difficult for me to test without installing nmap, etc., but given
what I see you trying to do you're running into trouble with quote escapes
and possibly with how you try to associate items. Try this, and see if
$P.Create(...) has a ReturnCode of 0.

What I'm doing here is first breaking out your code for generating a
filename, since that is complicated enough that it might have problems (and
writing it to your console window so you can see for sure if it's ok). This
gives us a more readable process creation line as well. So these 3 lines go
where your current $P.Create(...) is:

$f = ($path[$i]).substring(72,6))$(($path[$i]).substring(74, 1)) + ".txt"
Write-Host target filepath is $f
$P.create("nmap -p 17988 -oG - 10.72.252.0/24 | findstr/C:`"1 20`" >
`"$f`"")

If the final command has a non-zero ReturnCode, we've still got an issue
somewhere. Please post back what ReturnCode it gives you, and try the
following alternative Create(...) commands to see if one works. First:
$P.create("nmap -p 17988 -oG - 10.72.252.0/24 | findstr/C:`"1 20`"")
If that works, then we definitely had problem with the filename or filepath.
If it still doesn't work, try this:
$P.Create("nmap -p 17988 -oG - 10.72.252.0/24")
If that fails, then we encounter an error trying to start nmap.


"jonab" <guest@xxxxxx-email.com> wrote in message
news:d75f2f4f8d775d95b7f5660384ae1d87@xxxxxx-gateway.com...
Quote:

>
> Strange. Looking at my original post, I don't see the /// sequence but
> I do see them in your post. Here is the original code. Hopefully this
> one is better.
>
>
> $strpaths=\\"drives.txt\\"
> $path=Get-Content$strpaths
> $i=0
> do {
> $P= [WmiClass]\\"'\\servername\ROOT\CIMV2:Win32_Process'
> (file://\\servername\ROOT\CIMV2:Win32_Process)\\"
> $P.create(\\"nmap -p 17988 -oG - 10.72.252.0/24 \\")| \\"findstr
> /C:\\"1 20\\" > \\"$(($path[$i]).substring(72,
> 6))$(($path[$i]).substring(74, 1)).txt\\"
> $i ++
> } until ($i -eq $path.count)
>
>
> What I'm trying to achieve is to use nmap and search for a specific
> subnet for any machines using HP iLO. iLO uses port number 17988.
> Output from nMap included all devices on the subnet. I want to use
> \\"finstr\\" to search for lines that contains \\"1 20\\".
>
> I see what your saying that my command isn't giving me literal output
> from nMap. What do I need to use to have the literal output? When I do
> have the literal output, how do I pipe it to findstr so I can search for
> lines with specific criteria?
>
> Thank you for your help with this.
>
>
> --
> jonab
My System SpecsSystem Spec
Old 07-28-2009   #8 (permalink)
Alex K. Angelopoulos


 
 

Re: Using pipe in Powershell

I can't even find vistax64.com right now; it sounds like there's a problem
with domain lookup right now, which _might_ explain why it doesn't show up
in the Microsoft web forums either. And that probably explains why no one
else is responding; I may be the only person looking through NNTP today. (Or
possibly the only person who wanted to tackle a remote-run-nmap-plus-parsing
problem ).

"jonab" <guest@xxxxxx-email.com> wrote in message
news:9723990f20d0096fcc3f5d0eec16d1ac@xxxxxx-gateway.com...
Quote:

>
> Alex K. Angelopoulos;1100791 Wrote:
Quote:

>> Could you post the web URL you use to find your post? It sounds like
>> this is
>> a problem with how Microsoft translates from web display.
>
> Thank you Alex for the help. I'm posting from vistax64.com. Here is
> the post.
>
> Using pipe in Powershell
>
>
> --
> jonab
My System SpecsSystem Spec
Old 07-28-2009   #9 (permalink)


windows xp x86 Corp
 
 

Re: Using pipe in Powershell

Quote  Quote: Originally Posted by Alex K. Angelopoulos View Post

$f = ($path[$i]).substring(72,6))$(($path[$i]).substring(74, 1)) + ".txt"
Write-Host target filepath is $f
$P.create("nmap -p 17988 -oG - 10.72.252.0/24 | findstr/C:`"1 20`" >
`"$f`"")

If the final command has a non-zero ReturnCode, we've still got an issue
somewhere. Please post back what ReturnCode it gives you
If I ran this against my PC,
$P
= [WmiClass]\\.\ROOT\CIMV2:Win32_Process, after the screen with retrun code of 0, a new command prompt pops up and runs the actual nMap command.

If I run it against a remote machine,
$P
= [WmiClass]\\remoteMachine\ROOT\CIMV2:Win32_Process, nothing happens after the screen with return code and return code is 8.
My System SpecsSystem Spec
Old 07-28-2009   #10 (permalink)
Alex K. Angelopoulos


 
 

Re: Using pipe in Powershell

We know it connects to the remote machine successfully, otherwise it would
fail when creating $P. Can you try this final test case after creating $P
and see what return value you get?

$P.Create("cmd.exe /c")

That's the simplest possible remote command we can run. Unfortunately, the 8
return value when creating a process means "unknown failure" - a
less-than-helpful result...

"jonab" <guest@xxxxxx-email.com> wrote in message
news:cd35d3e017d4ed36cff7ec484f966d87@xxxxxx-gateway.com...
Quote:

>
> Alex K. Angelopoulos;1100816 Wrote:
Quote:

>>
>>
>> $f = ($path[$i]).substring(72,6))$(($path[$i]).substring(74, 1)) +
>> ".txt"
>> Write-Host target filepath is $f
>> $P.create("nmap -p 17988 -oG - 10.72.252.0/24 | findstr/C:`"1 20`" >
>> `"$f`"")
>>
>> If the final command has a non-zero ReturnCode, we've still got an
>> issue
>> somewhere. Please post back what ReturnCode it gives you
>
> If I ran this against my PC,
> $P = [WmiClass]'\\.\ROOT\CIMV2:Win32_Process'
> (file://\\.\ROOT\CIMV2:Win32_Process), after the screen with retrun code
> of 0, a new command prompt pops up and runs the actual nMap command.
>
> If I run it against a remote machine,
> $P = [WmiClass]'\\remoteMachine\ROOT\CIMV2:Win32_Process'
> (file://\\remoteMachine\ROOT\CIMV2:Win32_Process), nothing happens after
> the screen with return code and return code is 8.
>
>
> --
> jonab
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Powershell Pipe to Foreach to get indiviudal elements in a string collection. PowerShell
pipe to non-powershell process PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell
PowerShell: how to read Pipe.Input from a script? PowerShell
strange PowerShell pipe semantics PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46