![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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. 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||