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 - problems with $var | select-string -pattern $string -q

Reply
 
Old 02-07-2008   #1 (permalink)
Ben Christian


 
 

problems with $var | select-string -pattern $string -q

i'm trying to improve my existing code that fuctions as desired...here's the
snippet of code my current code...

foreach ($item in $list)
{get-childitem \\$item\share > $tempdir\$item.txt 2>&1}

foreach ($item in $list)
{
$variable = (get-childitem $tempdir\$item.txt | select-string -pattern
"$pattern" -q)
if (!$variable) { add-content $updatefile $item}
}

what i want to do is not have to redirect the gci command to a txt file
first...why not just assign the result to a variable...

foreach ($item in $list)
{
$gci = (get-ChildItem $path 2>&1)
$aVar = ($gci | select-string -pattern "$pattern" -q)
$err = ($gci | select-string -pattern "path not found" -q)
if (!$aVar) { add-content $file $item}
if ($err) {add-content $missingsharesfile $item}
}

but...when i do the select-string on the variable, it's giving me this...
PS C:\ps> $gci | Select-String -pattern $pattern -q
False
False

when i run the same SS on the the txt file that is the redirected gci output
(the code i use currently), it returns a True...1st, i'm not sure why i'm
getting a false...2nd, i'm not sure why it's returning 2 booleans isntead of
just 1...

also, here's the purpose of the script, should you have better ideas
I need to check network shares of a bunch of machines (in a text file -
$list) to: a) make sure they exist (hence the 2>&1 redirect of errors), and
b) check if the shares contain a specific file.
the desired output is:
if missing, put add-content to missing.txt,
if specific file IS found, then add-content to the found.txt
if file is NOT found, then add-content to notfound.txt

and...in the long run, i actually want this to update cells in an excel
spreadsheet instead of creating 3 text files, but i'll save that for another
post since it's pretty different from my situation here

Thanks a lot!

My System SpecsSystem Spec
Old 02-07-2008   #2 (permalink)
Oisin (x0n) Grehan [MVP]


 
 

Re: problems with $var | select-string -pattern $string -q

On Feb 7, 6:01*pm, Ben Christian <Ben
Christ...@xxxxxx> wrote:
Quote:

> i'm trying to improve my existing code that fuctions as desired...here's the
> snippet of code my current code...
>
> foreach ($item in $list)
> {get-childitem \\$item\share > $tempdir\$item.txt 2>&1}
>
> foreach ($item in $list)
> {
> * $variable = (get-childitem $tempdir\$item.txt | select-string -pattern
> "$pattern" -q)
> * if (!$variable) { add-content $updatefile $item}
>
> }
>
> what i want to do is not have to redirect the gci command to a txt file
> first...why not just assign the result to a variable...
>
> foreach ($item in $list)
> {
> * $gci = (get-ChildItem $path 2>&1)
> * $aVar = ($gci | select-string -pattern "$pattern" -q)
> * $err = ($gci | select-string -pattern "path not found" -q)
> * if (!$aVar) { add-content $file $item}
> * if ($err) {add-content $missingsharesfile $item}
>
> }
>
> but...when i do the select-string on the variable, it's giving me this...
> PS C:\ps> $gci | Select-String -pattern $pattern -q
> False
> False
>
> when i run the same SS on the the txt file that is the redirected gci output
> (the code i use currently), it returns a True...1st, i'm not sure why i'm
> getting a false...2nd, i'm not sure why it's returning 2 booleans isntead of
> just 1...
>
> also, here's the purpose of the script, should you have better ideas
> I need to check network shares of a bunch of machines (in a text file -
> $list) to: a) make sure they exist (hence the 2>&1 redirect of errors), and
> b) check if the shares contain a specific file.
> the desired output is:
> if missing, put add-content to missing.txt,
> if specific file IS found, then add-content to the found.txt
> if file is NOT found, then add-content to notfound.txt
>
> and...in the long run, i actually want this to update cells in an excel
> spreadsheet instead of creating 3 text files, but i'll save that for another
> post since it's pretty different from my situation here
>
> Thanks a lot!
Hi!

You could keep things a lot simpler by using the Test-Path cmdlet:

ps> test-path \\machine\existingshare
True

ps> test-path \\machine\nonexistant
False

ps> test-path \\machine\existingshare\existingfile.txt
True

ps> test-path \\machine\existingshare\nonexistantfile.txt
False

Additionally, test-path accepts wildcards * and ?.

Hope this helps,

- Oisin
My System SpecsSystem Spec
Old 02-08-2008   #3 (permalink)
Keith Hill [MVP]


 
 

Re: problems with $var | select-string -pattern $string -q

"Ben Christian" <Ben Christian@xxxxxx> wrote in message
news:439E3140-032A-4A16-A55F-9B3377DA4A4C@xxxxxx
Quote:

> i'm trying to improve my existing code that fuctions as desired...here's
> the
> snippet of code my current code...
>
> foreach ($item in $list)
> {get-childitem \\$item\share > $tempdir\$item.txt 2>&1}
>
> foreach ($item in $list)
> {
> $variable = (get-childitem $tempdir\$item.txt | select-string -pattern
> "$pattern" -q)
> if (!$variable) { add-content $updatefile $item}
> }
>
> what i want to do is not have to redirect the gci command to a txt file
> first...why not just assign the result to a variable...
>
> foreach ($item in $list)
> {
> $gci = (get-ChildItem $path 2>&1)
> $aVar = ($gci | select-string -pattern "$pattern" -q)
> $err = ($gci | select-string -pattern "path not found" -q)
> if (!$aVar) { add-content $file $item}
> if ($err) {add-content $missingsharesfile $item}
> }
So given Oisin suggestion, that would give you something like:

foreach ($share in $shares)
{
if (!(Test-Path $share)) {
Add-Content missingShares.txt $share
continue
}
if (Get-ChildItem $share | where {$_.Name -match '^theFileName\.txt$'}) {
Add-Content found.txt $share
}
else {
Add-Content notFound.txt $share
}
}
Quote:

>
> but...when i do the select-string on the variable, it's giving me this...
> PS C:\ps> $gci | Select-String -pattern $pattern -q
> False
> False
>
> when i run the same SS on the the txt file that is the redirected gci
> output
> (the code i use currently), it returns a True...1st, i'm not sure why i'm
> getting a false...2nd, i'm not sure why it's returning 2 booleans isntead
> of
> just 1...
First in this scenario you wouldn't want to mix up ErrorRecord objects (by
using 2>&1) with normal GCI output (FileInfo/DirectoryInfo). Second,
select-string is searching the contents of each file in your share looking
for a pattern match which in this case I'm guessing there a two items (file
or dir) in the share dir. In your first example you output the share's
contents (file and dir names) to a temp file. Then you essentially do a dir
of a file (kind of a no-op) and then select-string search that temp file so
your pattern matches appropriately.

BTW this stuff can be a bit overwhelming at first so I encourage you to
continue asking questions.

--
Keith

My System SpecsSystem Spec
Old 02-08-2008   #4 (permalink)
Ben Christian


 
 

Re: problems with $var | select-string -pattern $string -q

Thanks! I'll try it out!

"Keith Hill [MVP]" wrote:
Quote:

> "Ben Christian" <Ben Christian@xxxxxx> wrote in message
> news:439E3140-032A-4A16-A55F-9B3377DA4A4C@xxxxxx
Quote:

> > i'm trying to improve my existing code that fuctions as desired...here's
> > the
> > snippet of code my current code...
> >
> > foreach ($item in $list)
> > {get-childitem \\$item\share > $tempdir\$item.txt 2>&1}
> >
> > foreach ($item in $list)
> > {
> > $variable = (get-childitem $tempdir\$item.txt | select-string -pattern
> > "$pattern" -q)
> > if (!$variable) { add-content $updatefile $item}
> > }
> >
> > what i want to do is not have to redirect the gci command to a txt file
> > first...why not just assign the result to a variable...
> >
> > foreach ($item in $list)
> > {
> > $gci = (get-ChildItem $path 2>&1)
> > $aVar = ($gci | select-string -pattern "$pattern" -q)
> > $err = ($gci | select-string -pattern "path not found" -q)
> > if (!$aVar) { add-content $file $item}
> > if ($err) {add-content $missingsharesfile $item}
> > }
>
> So given Oisin suggestion, that would give you something like:
>
> foreach ($share in $shares)
> {
> if (!(Test-Path $share)) {
> Add-Content missingShares.txt $share
> continue
> }
> if (Get-ChildItem $share | where {$_.Name -match '^theFileName\.txt$'}) {
> Add-Content found.txt $share
> }
> else {
> Add-Content notFound.txt $share
> }
> }
>
Quote:

> >
> > but...when i do the select-string on the variable, it's giving me this...
> > PS C:\ps> $gci | Select-String -pattern $pattern -q
> > False
> > False
> >
> > when i run the same SS on the the txt file that is the redirected gci
> > output
> > (the code i use currently), it returns a True...1st, i'm not sure why i'm
> > getting a false...2nd, i'm not sure why it's returning 2 booleans isntead
> > of
> > just 1...
>
> First in this scenario you wouldn't want to mix up ErrorRecord objects (by
> using 2>&1) with normal GCI output (FileInfo/DirectoryInfo). Second,
> select-string is searching the contents of each file in your share looking
> for a pattern match which in this case I'm guessing there a two items (file
> or dir) in the share dir. In your first example you output the share's
> contents (file and dir names) to a temp file. Then you essentially do a dir
> of a file (kind of a no-op) and then select-string search that temp file so
> your pattern matches appropriately.
>
> BTW this stuff can be a bit overwhelming at first so I encourage you to
> continue asking questions.
>
> --
> Keith
>
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Select-String -exclude PowerShell
How to prepend a filename with a pattern string? VB Script
RE: searching for a pattern in a string!! VB Script
String pattern PowerShell
Select-String pattern quirk??? 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