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 - Command in If statement

Reply
 
Old 12-03-2007   #1 (permalink)
casey.daniell


 
 

Command in If statement

How do I call a function within an if statement and capture the $true
or $false the statement should return? I can't seem get the following
to work. Basically, I want to see if the image, and corresponding
path, are valid before doing something with them. However, the if
statement never seems to actually call the function, nor does it
return the expected boolean value. I am used to wrapping the fuction
call in ``'s in UNIX to get the value...I can't figueout how to get
this working in PS.

Help?

++++++++++++++++++++++

$DBFileList = Get-Content "C:\Documents and Settings\casey daniell
\Desktop\image.txt"
$ImageRoot= "C:\Inetpub\"

function ValidPath {
param ([string]$image)

$pattern = "\d\d/\d\d/.*\..*"
if ( $image -match $pattern ) {
Write-Output "Match"
$false
}
else {
Write-Output "NO MATCH"
$false
}

}

foreach($image in $DBFileList) {
if ( ValidPath $image ) {

if ( ( Test-Path "$ImageRoot/Pictures/$image" ) -and ( Test-Path
"$ImageRoot/sThumbnails/$image") ) {
echo $image is USED
}
else {
echo $image is NOT USED
Write-Output "$ImageRoot+/Pictures/+$image"
}
}
}

My System SpecsSystem Spec
Old 12-03-2007   #2 (permalink)
Brandon Shell [MVP]


 
 

Re: Command in If statement

It looks to me that you only return $false in your function. Try this


function ValidPath {
param ([string]$image)
$pattern = "\d\d/\d\d/.*\..*"
if ( $image -match $pattern ) {
Write-host "Match"
$true
}
else {
Write-Host "NO MATCH"
$false
}
}

"casey.daniell" <casey.daniell@xxxxxx> wrote in message
news:76e72c92-e817-44e1-9e39-e1b52cf1dd1e@xxxxxx
Quote:

> How do I call a function within an if statement and capture the $true
> or $false the statement should return? I can't seem get the following
> to work. Basically, I want to see if the image, and corresponding
> path, are valid before doing something with them. However, the if
> statement never seems to actually call the function, nor does it
> return the expected boolean value. I am used to wrapping the fuction
> call in ``'s in UNIX to get the value...I can't figueout how to get
> this working in PS.
>
> Help?
>
> ++++++++++++++++++++++
>
> $DBFileList = Get-Content "C:\Documents and Settings\casey daniell
> \Desktop\image.txt"
> $ImageRoot= "C:\Inetpub\"
>
> function ValidPath {
> param ([string]$image)
>
> $pattern = "\d\d/\d\d/.*\..*"
> if ( $image -match $pattern ) {
> Write-Output "Match"
> $false
> }
> else {
> Write-Output "NO MATCH"
> $false
> }
>
> }
>
> foreach($image in $DBFileList) {
> if ( ValidPath $image ) {
>
> if ( ( Test-Path "$ImageRoot/Pictures/$image" ) -and ( Test-Path
> "$ImageRoot/sThumbnails/$image") ) {
> echo $image is USED
> }
> else {
> echo $image is NOT USED
> Write-Output "$ImageRoot+/Pictures/+$image"
> }
> }
> }
My System SpecsSystem Spec
Old 12-03-2007   #3 (permalink)
casey.daniell


 
 

Re: Command in If statement

On Dec 3, 6:43 pm, "Brandon Shell [MVP]" <tshell._r...@xxxxxx>
wrote:
Quote:

> It looks to me that you only return $false in your function. Try this

Sorry about that I was intentionally returning $false just while
testing. However, even with the values corrected I don't see the
output from the Wrtie-Output in the function, so I know it's not
executing. Also, with it pegged to $false, I always see the main area
of the script execute, it's never skipped as it should be.
My System SpecsSystem Spec
Old 12-03-2007   #4 (permalink)
Keith Hill [MVP]


 
 

Re: Command in If statement

"Brandon Shell [MVP]" <tshell._rem_@xxxxxx> wrote in message
news:87B87277-DE51-448A-85A7-B9B8A0704712@xxxxxx
Quote:

> It looks to me that you only return $false in your function.
Plus the function is returning more output than the OP thinks. That is, the
use of Write-Output "Match" contributes the string "Match" to the output and
then $true is also added to the output. So the output of the function is an
array. You might find this blog post helpful in understanding function
output.

http://keithhill.spaces.live.com/blo...3A97!811.entry

Brandon's version should work because he also corrects the Write-Output
calls to Write-Host calls.

--
Keith

My System SpecsSystem Spec
Old 12-03-2007   #5 (permalink)
Brandon Shell [MVP]


 
 

Re: Command in If statement

Perhaps another problem is the use of $image in the function as well as a
script variable.

In the function use $img instead

function ValidPath {
param ([string]$img)
$pattern = "\d\d/\d\d/.*\..*"
if($img -match $pattern){
Write-host "Match"
$true
}
else {
Write-Host "NO MATCH"
$false
}
}

Hello Brandon Shell [MVP],
Quote:

> It looks to me that you only return $false in your function. Try this
>
> function ValidPath {
> param ([string]$image)
> $pattern = "\d\d/\d\d/.*\..*"
> if ( $image -match $pattern ) {
> Write-host "Match"
> $true
> }
> else {
> Write-Host "NO MATCH"
> $false
> }
> }
> "casey.daniell" <casey.daniell@xxxxxx> wrote in message
> news:76e72c92-e817-44e1-9e39-e1b52cf1dd1e@xxxxxx
> ...
>
Quote:

>> How do I call a function within an if statement and capture the $true
>> or $false the statement should return? I can't seem get the following
>> to work. Basically, I want to see if the image, and corresponding
>> path, are valid before doing something with them. However, the if
>> statement never seems to actually call the function, nor does it
>> return the expected boolean value. I am used to wrapping the fuction
>> call in ``'s in UNIX to get the value...I can't figueout how to get
>> this working in PS.
>>
>> Help?
>>
>> ++++++++++++++++++++++
>>
>> $DBFileList = Get-Content "C:\Documents and Settings\casey daniell
>> \Desktop\image.txt"
>> $ImageRoot= "C:\Inetpub\"
>> function ValidPath {
>> param ([string]$image)
>> $pattern = "\d\d/\d\d/.*\..*"
>> if ( $image -match $pattern ) {
>> Write-Output "Match"
>> $false
>> }
>> else {
>> Write-Output "NO MATCH"
>> $false
>> }
>> }
>>
>> foreach($image in $DBFileList) {
>> if ( ValidPath $image ) {
>> if ( ( Test-Path "$ImageRoot/Pictures/$image" ) -and ( Test-Path
>> "$ImageRoot/sThumbnails/$image") ) {
>> echo $image is USED
>> }
>> else {
>> echo $image is NOT USED
>> Write-Output "$ImageRoot+/Pictures/+$image"
>> }
>> }
>> }

My System SpecsSystem Spec
Old 12-03-2007   #6 (permalink)
casey.daniell


 
 

Re: Command in If statement

I totally missed the Write-Host modification to the script, and didn't
realize that writing output 2xs like that would make it write out to
an array. Changing the output to a Write-Host from a Write-Output
seems to have soved my issue. Thanks. Sorry for asking a newb question
such as that. Working on the leap from Korn/Bash shell scripting to PS
now.

Casey

On Dec 3, 7:17 pm, Brandon Shell [MVP] <tshell.M...@xxxxxx> wrote:
Quote:

> Perhaps another problem is the use of $image in the function as well as a
> script variable.
>
> In the function use $img instead
>
> function ValidPath {
> param ([string]$img)
> $pattern = "\d\d/\d\d/.*\..*"
> if($img -match $pattern){
> Write-host "Match"
> $true
> }
> else {
> Write-Host "NO MATCH"
> $false
> }
>
> }
>
> Hello Brandon Shell [MVP],
>
Quote:

> > It looks to me that you only return $false in your function. Try this
>
Quote:

> > function ValidPath {
> > param ([string]$image)
> > $pattern = "\d\d/\d\d/.*\..*"
> > if ( $image -match $pattern ) {
> > Write-host "Match"
> > $true
> > }
> > else {
> > Write-Host "NO MATCH"
> > $false
> > }
> > }
> > "casey.daniell" <casey.dani...@xxxxxx> wrote in message
> >news:76e72c92-e817-44e1-9e39-e1b52cf1dd1e@xxxxxx
> > ...
>
Quote:
Quote:

> >> How do I call a function within an if statement and capture the $true
> >> or $false the statement should return? I can't seem get the following
> >> to work. Basically, I want to see if the image, and corresponding
> >> path, are valid before doing something with them. However, the if
> >> statement never seems to actually call the function, nor does it
> >> return the expected boolean value. I am used to wrapping the fuction
> >> call in ``'s in UNIX to get the value...I can't figueout how to get
> >> this working in PS.
>
Quote:
Quote:

> >> Help?
>
Quote:
Quote:

> >> ++++++++++++++++++++++
>
Quote:
Quote:

> >> $DBFileList = Get-Content "C:\Documents and Settings\casey daniell
> >> \Desktop\image.txt"
> >> $ImageRoot= "C:\Inetpub\"
> >> function ValidPath {
> >> param ([string]$image)
> >> $pattern = "\d\d/\d\d/.*\..*"
> >> if ( $image -match $pattern ) {
> >> Write-Output "Match"
> >> $false
> >> }
> >> else {
> >> Write-Output "NO MATCH"
> >> $false
> >> }
> >> }
>
Quote:
Quote:

> >> foreach($image in $DBFileList) {
> >> if ( ValidPath $image ) {
> >> if ( ( Test-Path "$ImageRoot/Pictures/$image" ) -and ( Test-Path
> >> "$ImageRoot/sThumbnails/$image") ) {
> >> echo $image is USED
> >> }
> >> else {
> >> echo $image is NOT USED
> >> Write-Output "$ImageRoot+/Pictures/+$image"
> >> }
> >> }
> >> }


My System SpecsSystem Spec
Old 12-03-2007   #7 (permalink)
Brandon Shell [MVP]


 
 

Re: Command in If statement

It is not a newb mistake. Powershell is a fundamental change in mindset.
Your doing great.

Understanding how output effects the object is one the key things to understand.

Hello casey.daniell,
Quote:

> I totally missed the Write-Host modification to the script, and didn't
> realize that writing output 2xs like that would make it write out to
> an array. Changing the output to a Write-Host from a Write-Output
> seems to have soved my issue. Thanks. Sorry for asking a newb question
> such as that. Working on the leap from Korn/Bash shell scripting to PS
> now.
>
> Casey
>
> On Dec 3, 7:17 pm, Brandon Shell [MVP] <tshell.M...@xxxxxx> wrote:
>
Quote:

>> Perhaps another problem is the use of $image in the function as well
>> as a script variable.
>>
>> In the function use $img instead
>>
>> function ValidPath {
>> param ([string]$img)
>> $pattern = "\d\d/\d\d/.*\..*"
>> if($img -match $pattern){
>> Write-host "Match"
>> $true
>> }
>> else {
>> Write-Host "NO MATCH"
>> $false
>> }
>> }
>>
>> Hello Brandon Shell [MVP],
>>
Quote:

>>> It looks to me that you only return $false in your function. Try
>>> this
>>>
>>> function ValidPath {
>>> param ([string]$image)
>>> $pattern = "\d\d/\d\d/.*\..*"
>>> if ( $image -match $pattern ) {
>>> Write-host "Match"
>>> $true
>>> }
>>> else {
>>> Write-Host "NO MATCH"
>>> $false
>>> }
>>> }
>>> "casey.daniell" <casey.dani...@xxxxxx> wrote in message
>>> news:76e72c92-e817-44e1-9e39-e1b52cf1dd1e@xxxxxx
>>> om
>>> ...
>>>> How do I call a function within an if statement and capture the
>>>> $true or $false the statement should return? I can't seem get the
>>>> following to work. Basically, I want to see if the image, and
>>>> corresponding path, are valid before doing something with them.
>>>> However, the if statement never seems to actually call the
>>>> function, nor does it return the expected boolean value. I am used
>>>> to wrapping the fuction call in ``'s in UNIX to get the value...I
>>>> can't figueout how to get this working in PS.
>>>>
>>>> Help?
>>>>
>>>> ++++++++++++++++++++++
>>>>
>>>> $DBFileList = Get-Content "C:\Documents and Settings\casey daniell
>>>> \Desktop\image.txt"
>>>> $ImageRoot= "C:\Inetpub\"
>>>> function ValidPath {
>>>> param ([string]$image)
>>>> $pattern = "\d\d/\d\d/.*\..*"
>>>> if ( $image -match $pattern ) {
>>>> Write-Output "Match"
>>>> $false
>>>> }
>>>> else {
>>>> Write-Output "NO MATCH"
>>>> $false
>>>> }
>>>> }
>>>> foreach($image in $DBFileList) {
>>>> if ( ValidPath $image ) {
>>>> if ( ( Test-Path "$ImageRoot/Pictures/$image" ) -and ( Test-Path
>>>> "$ImageRoot/sThumbnails/$image") ) {
>>>> echo $image is USED
>>>> }
>>>> else {
>>>> echo $image is NOT USED
>>>> Write-Output "$ImageRoot+/Pictures/+$image"
>>>> }
>>>> }
>>>> }

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Running a command from inside a script, command line is corrupted PowerShell
Help with SQL statement VB Script
goal Statement Vista mail
just a statement Vista General


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