header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
using
Last Post 31 Aug 2010 07:48 AM by thanosazlin. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
thanosazlinUser is Offline
New Member
New Member
Posts:19
Avatar

--
30 Aug 2010 01:58 PM
    powerhsell nube here, i can't figure out why my if statement works but the switch statement does not? aren't they both doing the same thing?
    the $input variable has some lines in it that contain smiliar to the following:

    Pseudo name=harddisk8

    i'm trying to strip away the "Pseudo name=" part of the string. it works for my if statement but i tried it in a switch statement and it just outputs the line with the "Pseudo name="

    foreach ($line in $input)
    {
    [string]$test = $line
    switch ($test)
    {
    $test.Contains("Pseudo name"){$test = $test -replace "Pseudo name=",""} }
    $test

    #if ($test.Contains("Pseudo name")){
    #$test = $test -replace "Pseudo name=",""
    #}
    #$test

    }
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1642
    Avatar

    --
    30 Aug 2010 04:24 PM
    I'm sorry, I think you aren't using switch properly. Consider this example:

    $test="Pseudo name=foo"
    switch -regex ($test){
    "Pseudo name=*" {$_.replace("Pseudo name=","")};;
    }

    I think that does what you want. You might be over-complicating things by using a switch unless you're going to be doing several conditions.
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    thanosazlinUser is Offline
    New Member
    New Member
    Posts:19
    Avatar

    --
    31 Aug 2010 07:48 AM
    thanks that worked . yeah i was just testing switch with 1 entry first :). what are the 2 ;; for by the way?
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer