header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
NotinArray
Last Post 25 Aug 2010 02:20 PM by 0ptikGhost. 7 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
BasSUser is Offline
New Member
New Member
Posts:3
Avatar

--
24 Aug 2010 07:58 AM
    Hello,


    I have the following array:
    $e = @("user*","te*")

    I want something like “Dir | ? {$_.name -notcontains $e}“ to work.
    If I fill the array with “$e = @("users")” then it works fine.
    I have the following function that does the trick but I still find its must be easier…

    function notinarray
    {
    param($str,$arr)
    $found = $true
    foreach ($val in $arr) {if ($str -like $val) {$found = $false}}
    return $found
    }

    PowerShell JediUser is Offline
    Basic Member
    Basic Member
    Posts:410
    Avatar

    --
    24 Aug 2010 01:04 PM
    If you have a working function go with it.

    I believe the syntax your looking for is...

    Dir | ? {$_.name -notlike $e[0] -and $_.name -notlike $e[1] -and $_.name -notlike $e[2]} etc...
    PoSH is a Automation Technology surfaced as a scripting language, not a "spice" ;-)
    BasSUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    24 Aug 2010 11:43 PM
    I don't want a long list of -notlikes but just 1 array to fill.
    This so I have a more readable script and easier to maintain.

    But thanks for you reply.
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    25 Aug 2010 07:35 AM
    You've got it backwards, it should be:

    $e = "user*","te*"
    Dir | ?{$e -notcontains $_.name} | ...do something...

    BasSUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    25 Aug 2010 07:50 AM
    I see I forgot the most important requirement...
    The array is filled with something* (so with an asterisk)
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    25 Aug 2010 01:35 PM
    Ok then use a function to clean up the code and then your one-liner will look cleaner:
    function Test-FileName($filename){            
    $e = "user","te"
    $Test = $False
    $e | %{if($filename -match $_){$Test = $true}}
    return $Test
    }

    Dir | ?{Test-FileName $_.name} | #...do something...



    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    25 Aug 2010 01:44 PM
    I'm not sure why you are using an asterisk in your check, but if it is because you are used to cmd prompt to indicate (e.g. "user*" ) if the name has user and anything following user then it is not needed with the -match operator. The -match uses regular expressions so as long as 'user' exist in the string you are checking it will pass. If you want to duplicate a regex with cmd * then it would be "user.*' (dot being any character and * indicating 0 to any number of previous character)

    However if you are looking for a literal name with an asterisk (which is unlikely as it is an illegal character in a filename) you could now use asterisk in your array and get the desired results.
    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    25 Aug 2010 02:20 PM

    You could use the -like operator to use wildcards:

    function Test-FileName($filename){            
    $e = "user*","te*"
    $Test = $False
    $e | %{if($filename -like $_){$Test = $true}}
    return $Test
    }

    Dir | ?{Test-FileName $_.name} | #...do something...

    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