Using the "like" operator

Author Message
gabrie

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 11/23/2007
  • Status: offline
Using the "like" operator Friday, November 23, 2007 5:46 AM (permalink)
0
Hi
I'm playing with the new VMware commandlet. When I run the following command I get this result:

Get-VM | Get-HardDisk

result:

Filename        : [LocalStore01] w-vcenter/w-vcenter.vmdk
ID              : VirtualMachine-vm-374/2000
Name            : Hard Disk 1

Filename        : [LocalStore02] TMP-WinXP-SP2-UK/TMP-WinXP-SP2-UK.vmdk
ID              : VirtualMachine-vm-427/2000
Name            : Hard Disk 1

Now I only want the harddisks from [LocalStore02], so I tried this:

Get-VM | Get-HardDisk | where { $_.Filename  -like  "[LocalStore02]" }

but this doesn't return any results.

Also tried with * or % but I cant get a script that works.

Who can help out?

Gabrie
 
#1
    SAPIENScripter

    • Total Posts : 283
    • Scores: 2
    • Reward points : 0
    • Joined: 11/1/2006
    • Location: SAPIEN Technologies
    • Status: offline
    RE: Using the "like" operator Sunday, November 25, 2007 11:38 PM (permalink)
    0
    Why not simply drop the brackets?  That's what is messing things up.

    Get-VM | Get-HardDisk | where { $_.Filename  -like  "*LocalStore02*" }

    Otherwise you'll need to escape the characters

    Get-VM | Get-HardDisk | where { $_.Filename  -like  "*\[LocalStore02\]*" }

    I think that should do the trick.
    Jeffery Hicks
    Windows PowerShell MVP
    SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

    Follow Me: http://www.twitter.com/JeffHicks
     
    #2
      gabrie

      • Total Posts : 2
      • Scores: 0
      • Reward points : 0
      • Joined: 11/23/2007
      • Status: offline
      RE: Using the "like" operator Sunday, November 25, 2007 11:58 PM (permalink)
      0
      Hi
       
      Thx, I solved it through another forum... seems that instead of -like I should've used -match
       
      Gabrie
       
       
      #3
        SAPIENScripter

        • Total Posts : 283
        • Scores: 2
        • Reward points : 0
        • Joined: 11/1/2006
        • Location: SAPIEN Technologies
        • Status: offline
        RE: Using the "like" operator Monday, November 26, 2007 1:02 AM (permalink)
        0
        Using -match will give you more hits without needing wildcards, which for you may be all that you need. You can still use wildcards, but be careful.  Look at this:

        PS C:\test> dir | where {$_.name -match "test?.txt"}


            Directory: Microsoft.PowerShell.Core\FileSystem::C:\test


        Mode                LastWriteTime     Length Name
        ----                -------------     ------ ----
        -a---        10/22/2007   9:59 AM         86 contest.txt
        -a---        10/22/2007  10:06 AM        136 test.txt

        Compared to:

        PS C:\test> dir | where {$_.name -like "test?.txt"}


            Directory: Microsoft.PowerShell.Core\FileSystem::C:\test


        Mode                LastWriteTime     Length Name
        ----                -------------     ------ ----
        -a---         8/29/2007  10:21 AM        126 test1.txt
        -a---         8/29/2007  10:21 AM        136 test2.txt

        If I'm looking for files that start with test and then any single character, the latter approach is better.  Powershell offers many ways to match and find strings, including regular expressions.

        Run:
        PS C:\test> help about_comparison_operators

        to learn more.
        Jeffery Hicks
        Windows PowerShell MVP
        SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

        Follow Me: http://www.twitter.com/JeffHicks
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9