Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

powershell and if statement

Posted on 2011-05-02
2
Medium Priority
?
768 Views
Last Modified: 2012-06-27
I have made this kind of PS script. What is wrong with it?

Depending of variable $type I would like to set value of variable $value. What is wrong with this and how I could correct it?

$type = Read-Host "Write the type"
   if ($type -eq 1) {$value -eq "A"}
     elseif ($type -eq 2) {$value -eq "B"}
     elseif ($type -eq 3) {$value -eq "C"}
     else {"You typed incorrectly"}


Kind regards,
Timo
0
Comment
Question by:thaapavuori
2 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 2000 total points
ID: 35505851
"-eq" is only when it is a comparison operator. "=" when setting a value of an object.


$type = Read-Host "Write the type"
   if ($type -eq 1) {$value = "A"}
     elseif ($type -eq 2) {$value = "B"}
     elseif ($type -eq 3) {$value = "C"}
     else {"You typed incorrectly"}

0
 
LVL 12

Expert Comment

by:mwochnick
ID: 35505935
your using the comparison operator eq to do the assignment - try this

$type = Read-Host "Write the type"
   if ($type -eq 1) {$value = "A"}
     elseif ($type -eq 2) {$value = "B"}
     elseif ($type -eq 3) {$value = "C"}
     else {"You typed incorrectly"}

Open in new window

0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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 antispam), the admini…
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…

579 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