Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Using the "like" operator

 
Logged in as: Guest
arrSession:exec spGetSession 2,5,53754
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Windows PowerShell >> Using the "like" operator
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Using the "like" operator - 11/23/2007 4:46:54 AM   
  gabrie

 

Posts: 2
Score: 0
Joined: 11/23/2007
Status: offline
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
 
 
Post #: 1
 
 RE: Using the "like" operator - 11/25/2007 10:38:38 PM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
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

(in reply to gabrie)
 
 
Post #: 2
 
 RE: Using the "like" operator - 11/25/2007 10:58:35 PM   
  gabrie

 

Posts: 2
Score: 0
Joined: 11/23/2007
Status: offline
Hi

Thx, I solved it through another forum... seems that instead of -like I should've used -match

Gabrie

(in reply to SAPIENScripter)
 
 
Post #: 3
 
 RE: Using the "like" operator - 11/26/2007 12:02:59 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
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

(in reply to gabrie)
 
 
Post #: 4
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Windows PowerShell >> Using the "like" operator Page: [1]
Jump to:





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
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts