?
Solved

problem with if condition

Posted on 2012-09-13
21
Medium Priority
?
244 Views
Last Modified: 2012-09-30
Hello,


How is it possible to modify this script because It ignores the parameter $v because they are -or in the if command :

if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and $N -or $G -or $p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp )
                              {
                              cd $sql2k8
                              .\sql_bin.ps1 $a $v $E $t $I $H $D $N $G $p $sc $scp $ac $acp $asc $asp
                              cd $workdir
                              exit
      elseif ($a -eq "install" -and $v -contains "12" -and $E -and $t -and $I -and $H -and $D -and $N -or $G -or $p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp)
                              {
                              cd $sql2k12
                              .\sql_bin.ps1 $a $v $E $t $I $H $D $N $G $p $sc $scp $ac $acp $asc $asp
                              cd $workdir
                              exit
                              }
                              }

Thanks

bibi
0
Comment
Question by:bibi92
  • 12
  • 9
21 Comments
 
LVL 3

Expert Comment

by:jhheider
ID: 38395156
Bibi,

We'd have to know logically what you're trying to accomplish, but I suspect parenthesization is the answer. I'm going to guess it should be:

if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and ($N -or $G -or $p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp)) 
                              {
                              cd $sql2k8
                              .\sql_bin.ps1 $a $v $E $t $I $H $D $N $G $p $sc $scp $ac $acp $asc $asp
                              cd $workdir
                              exit
      elseif ($a -eq "install" -and $v -contains "12" -and $E -and $t -and $I -and $H -and $D -and ($N -or $G -or $p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp)) 
                              {
                              cd $sql2k12
                              .\sql_bin.ps1 $a $v $E $t $I $H $D $N $G $p $sc $scp $ac $acp $asc $asp
                              cd $workdir
                              exit
                              }
                              }

Open in new window

0
 

Author Comment

by:bibi92
ID: 38395549
Hello,

I change the script with your solution, but now it is not possible to execute the script :
powershell .\sql_bin.ps1 -a install -v 2008R2SP2 -E standard -t SQL -I test8  -H i:\test8\ -D i:\test8\ -N French_CI_AS -G bibi -p 12345678 -sc bibi -scp 12345678 -ac bibi -acp 12345678
Thanks
bibi
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38395668
That runs just fine for me. I assume you're populating those variables from the contents of $args somehow, correct? Can we see the entire script?
0
What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

 

Author Comment

by:bibi92
ID: 38395688
# -----------------------------------------
# PARAMETERS SETTING
# -----------------------------------------

Param 	(   
		
			[string]$a = "",
			[string]$v = "",
            		[string]$E = "",
			[string]$t = "",
			[string]$I = "",
			[string]$H = "",
			[string]$D = "",
			[string]$N = "",
			[string]$G = "",
			[string]$p = "",
            		[string]$sc = "",
            		[string]$scp = "",
            		[string]$ac = "",
            		[string]$acp = "",
			[string]$asc = "",
			[string]$ascp = "",
            		[switch]$help,
			[switch]$verbose,
			[switch]$debug
		)

	$windowsUse = New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())
	if(!$windowsUse.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)) 
	{
	   return
	}


# -----------------------------------------
# ENVIRONMENT SETTING
# -----------------------------------------
$repository=$path
$workdir=(get-location).providerpath 
$rdbmsdir=$path + "/sqlserver/rdbms"
##$env=$workdir + "/sqlserver/env"
$libhome=$workdir + "/sqlserver/library"
$sql2k8=$workdir + "/sqlserver/sql2008"
$sql2k12=$workdir + "/sqlserver/sql2012"
##-------------------------------------------

##-------------------------------------------
## Load Script Libraries, local and client variables
##-------------------------------------------
##$sqlvar = gci -Recurse $env -in *.ps1 
##foreach ( $item in $sqlvar) { . $item.FullName }
$libraries = gci -Recurse $libhome -in *.ps1 
foreach ( $item in $libraries) { . $item.FullName }
## End load Script Libraries and variables
##-------------------------------------------



	if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and $N -and $G -or ($p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp))
					{
					cd $sql2k8
					.\sql_bin.ps1 $a $v $E $t $I $H $D $N $G $p $sc $scp $ac $acp $asc $ascp
					cd $workdir
					}

	elseif  ($a -eq "install" -and $v -contains "12" -and $E -and $t -and $I -and $H -and $D -and $N -and $G -or ($p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp))
					{
					cd $sql2k12
					.\sql_bin.ps1 $a $v $E $t $I $H $D $N $G $p $sc $scp $ac $acp $asc $acsp
					cd $workdir
					exit
					}

                                  }

Open in new window

     
Thanks
bibi
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38395720
Do you have a library you're loading that populates your variables? Put another way, if you put an:
echo "X" $a "X"

Open in new window

before line 59, does it display anything?
0
 

Author Comment

by:bibi92
ID: 38395747
even if the version is 2012, the script executes cd $ sql2k8
thanks
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38395770
Looking at your entire script, your grouping ( is a term too late (again, unless I'm misunderstanding your logic). You want to make sure you're doing an "and" of all your terms, with the last "and"ed term being the multiple disjuction ("or" clause). So, instead of:
	if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and $N -and $G -or ($p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp))

Open in new window

it should be:
	if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and $N -and ($G -or $p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp))

Open in new window

Your version will trigger if any of p/sc/scp/ac/acp/asc/ascp is true. Mine will trigger if *ALL* of the others are true, *AND* any one of the above (or G). Note that your original version had "$N -or $G", which would be:
	if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and ($N -or $G -or $p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp))

Open in new window

So, you need to properly group your logic to make the correct decision.
0
 

Author Comment

by:bibi92
ID: 38395819
Butr with       if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and $N -and $G -or ($p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp)), I can execute the script
but with
if ($a -eq "install" -and $v -contains "08" -and $E -and $t -and $I -and $H -and $D -and ($N -or $G -or $p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp)), when I execute the script, nothing happend
Thanks
bibi
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38395835
place this before that line, and we should see why:

echo $a ":" $v ":" $E ":" $t ":" $I ":" $H ":" $D ":" $N ":" $G ":" $p ":" $sc ":" $scp ":" $ac ":" $acp ":" $asc ":" $ascp

Open in new window

0
 

Author Comment

by:bibi92
ID: 38396125
The problem is with if or conditions so it does not make a difference between the v$ 2008 and 2012 and the script execute cd $sql2008 for sql2012. Thanks
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38396135
Are you able to run the script with the echo in place to verify that you're passing the correct variable values to the point we reach the "if" statement? Once we know that's working as intended, we can visually evaluate the logic clause and see what's happening.
0
 

Author Comment

by:bibi92
ID: 38396138
I have checked That ne cause the script sql_bin are différent for 2008 and 2012 and only one script is executed but the version are different.
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38396153
You're testing a string ($v) not a collection, right? Shouldn't that test be -match?

http://www.computerperformance.co.uk/powershell/powershell_conditional_operators.htm
0
 

Author Comment

by:bibi92
ID: 38396154
The correct values are passed but the command $sql2008 is executed in any case
0
 

Author Comment

by:bibi92
ID: 38396179
Yes v$=SQL2008R2 or v$=SQL2012RTM
0
 

Accepted Solution

by:
bibi92 earned 0 total points
ID: 38396193
Before I add -or the test be match
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38396202
So, changing -contains to -match doesn't alter the behavior? Can you post the output from that echo statement with your current 2 sets of test input, as well as the "if" statements as they currently appear in your script?
0
 

Author Comment

by:bibi92
ID: 38396555
Ok here the output from the echo for v$=SQL2008R2SP2 or v$=SQL2012RTM
install
:
2008R2SP2
:
enterprise
:
AS
:
SWQTEST5
:
i:\swqtest5\
:
i:\swqtest5\
:
French_CI_AS
:
bibi
:

:

:

:

:

:
bibi
:
password12

or

install
:
2012RTM
:
enterprise
:
AS
:
SWQTEST5
:
i:\swqtest5\
:
i:\swqtest5\
:
French_CI_AS
:
bibi
:

:

:

:

:

:
bibi
:
password12
0
 
LVL 3

Expert Comment

by:jhheider
ID: 38396582
And what is the current version of the if() test you're using? If it's the one from #38395688 it will always evaluate to TRUE because
-or ($p -or $sc -or $scp -or $ac -or $acp -or $asc -or $ascp)

Open in new window

is always TRUE because you've got both $asc and $ascp set. Assuming that's all the variables that need to be disjunct, then you should swap their preceeding -or for a -and.
0
 

Author Comment

by:bibi92
ID: 38396662
I have changed the script like this and it works for changing directory before executing the script, maybe is it possible to optimize
if ($v -match "08")
                              {
                              cd $sql2k8
                              }
elseif  ($v -match "12")
                              {
                              cd $sql2k12
                              }
and it works
Thanks
bibi
0
 

Author Closing Comment

by:bibi92
ID: 38448301
Thanks, regards bibi
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question