header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Compare arrays with -contain or -match
Last Post 29 Jun 2009 07:43 AM by edm365f31. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
JKavUser is Offline
New Member
New Member
Posts:70
Avatar

--
26 Jun 2009 07:03 AM

    Newb question, seemed easy enough but I think I am coding something wrong.  Basically there is a list of services we want to stop and disable, the list of services are in a text file... then with the gwmi win32_service results I want to do a line by line check to see if the returned wmi .Caption matches an entry in the array of defined services.  I just can't get it to work.

    $defsvcs = gc "\\someserver\d$\scripts\ShutdownServices.txt"

    $actsvcs = gwmi win32_service -computerName "servername" | Select Name, Caption

    foreach ($svc in $actsvcs){

    if ($_.Caption -match $defsvcs){

    "Found " + $_.Caption }

    }

    JKavUser is Offline
    New Member
    New Member
    Posts:70
    Avatar

    --
    26 Jun 2009 11:53 AM
    For the record this is for V1. I have an existing script that works (aside from stopping Telephony and Remote Access Communication manager). After getting a working version which does a gwmi query foreach of the 27 services in the array the gc creates I went back and said okay lets do an all inclusive win32_service query and then process each of the returned services and if the returned .Caption equals one of the array elements perform the set of actions to stop and disable the service......
    tojo2000User is Offline
    New Member
    New Member
    Posts:45
    Avatar

    --
    26 Jun 2009 12:12 PM
    -match will take the value on the left and see if it matches the regular expression on the right. If the strings contained in $defsvcs can be used as regular expressions, then you can do something like this:

    foreach ($svc in $actsvcs){
    foreach ($match in $defsvcs) {
    if ($svc.Caption -match $defsvcs){
    "Found " + $svc.Caption
    }
    }
    }

    or if $defsvcs is a list of the actual names as opposed to an expression to match, you can use -contains like this:

    foreach ($svc in $actsvcs){
    if ($defsvcs -contains $svc.Caption){
    "Found " + $svc.Caption
    }
    }

    JKavUser is Offline
    New Member
    New Member
    Posts:70
    Avatar

    --
    26 Jun 2009 12:24 PM
    Ugggh so I had the comparison (-contains) backwards... ugggh. The array is a list of the known Captions (i.e. Telephony) so the contains is easy enough. Thanks alot!
    edm365f31User is Offline
    Basic Member
    Basic Member
    Posts:100
    Avatar

    --
    29 Jun 2009 07:43 AM
    Is there a way to replicate this when comparing columns in an Excel spreedsheet or csv file. Like diffing what exist and exist on sheets being compared.
    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