none
Powershell Looping issue RRS feed

  • Question

  • Hi , I am learning PowerShell and newbie to PowerShell.

    Can I get some help on below.

    I created three files :

    1. Testfile
    2. BB-Thisisfile1223 ****?????
    3. CC-ThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile

    4. CC-ThisisthelongestfileThisisthelongestf &&& *** ileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile

    5. Testfile 123 **** ??? ::

    I am using below code , to get the output:

    Ask from me:

    I am getting output as expected for first 2 files as below

    Output1 : File is not in defined format

    Output2 : File has wild card operators

    But even for 3rd file , I am getting below output , please help by correcting it.

    Output3 : File has wild card operators

    Also: I would like to get outputs exactly matching below:

    File has wild card characters & more than 50 characters

    File has Invalid format & has wild card operators

    $q = "select * from table1"

    $exec_q = Invoke-Sqlcmd -serverinstance abc -database new -Query $q $min = $exec_q | select name -ExpandProperty name foreach($n in $min){ if( $n -notmatch 'AA-' -and $n -notmatch 'BB-' -and $n -notmatch 'CC-'` ){ Write-Host "File is not in defined format" } Elseif( $n -match 'AA-' -or $n -match 'BB-' -or $n -match 'CC-' -or $n -match @('[,/\:*?"<>&;]') ){ Write-Host "File has wild card operators" } Elseif( $n -match 'AA-' -or $n -match 'BB-' -or $n -match 'CC-' -or $n.length -gt 70 ){ Write-Host "File has more than 50 characters" } Elseif( $n -match 'AA-' -or $n -match 'BB-' -or $n -match 'CC-' -or $n.length -gt 70 -or $n -match @('[,/\:*?"<>&;]')){ Write-Host "File has wild card characters & more than 50 characters" } Elseif( $n -notmatch 'AA-' -and $n -notmatch 'BB-' -and $n -notmatch 'CC-' -and $n.length -gt 70 -and $n -notmatch @('[,/\:*?"<>&;]')){ Write-Host "File has Invalid format & has wild card operators" }}

    Best Regards,SQLBoy






    • Edited by Kumar GK Friday, November 15, 2019 3:26 PM
    Friday, November 15, 2019 2:35 PM

Answers

  • Your testing is faulty. Your last ELSEIF condition can never be reached.

    This is essentially the same code that JRV submitted, but with the regexes that use the "^" anchor. 

    $min = "Testfile",
    "BB-Thisisfile1223 ****?????",
    "CC-ThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile",
    "CC-ThisisthelongestfileThisisthelongestf &&& *** ileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile",
    "ZZ-ThisisthelongestfileThisisthelongestf &&& *** ileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile"
    
    
    foreach($n in $min){
        if( $n -notmatch '^(AA-|BB-|CC-)' ){
            Write-Host "File is not in defined format" 
        } Elseif( $n -match '^(AA-|BB-|CC-)' -and $n.length -le 70 -and $n -match '[,/\:*?"<>&;]' ){
            Write-Host "File has wild card operators and is not longer then 70 characters"
        } Elseif( $n -match '^(AA-|BB-|CC-)' -and $n -notmatch '[,/\:*?"<>&;]' -and $n.length -gt 70 ){
            Write-Host "File has more than 70 characters"
        } Elseif($n -match '^(AA-|BB-|CC-)' -and ($n.length -gt 70 -and $n -match '[,/\:*?"<>&;]') ){
            Write-Host "File has wild card characters & more than 70 characters"
        } Elseif($n -notmatch '^(AA-|BB-|CC-)' -and $n.length -gt 70 -and $n -notmatch '[,/\:*?"<>&;]'){
            ########################
            # NOTE: This test will never be true. A file with an invalid format will fail the first test
            ########################
            Write-Host "File has Invalid format, has more than 70 characters & has wild card operators"
        }
    }
    
    


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Kumar GK Monday, November 18, 2019 8:03 AM
    Saturday, November 16, 2019 3:21 AM

All replies

  • Your code makes no sense. It is also almost impossible to read as posted.

    To match multiple conditions we would do this:

        $n -match &;]'">'AA-|BB-|CC-|[,/\:*?"<>&;]'

    The "|" is the OR operator for RegEx patterns.

    The following two lines make no sense and will produce nothing.

    $q = "select * from table1"
    $min
    = $q | select name -ExpandProperty name


    \_(ツ)_/


    Friday, November 15, 2019 3:05 PM
    Moderator
  • Thanks @jrv , I did correct the first 2 lines of code. Thanks for response.

    Best Regards,SQLBoy

    Friday, November 15, 2019 3:23 PM
  • Format is better but still broken.

    \_(ツ)_/

    Friday, November 15, 2019 3:29 PM
    Moderator
  • This would be the correct and readable way to write this:

    $q = 'select * from table1'
    $exec_q = Invoke-Sqlcmd -serverinstance abc -database new -Query $q
    $min = $exec_q | select name -ExpandProperty name
    
    foreach($n in $min){
        if($n -notmatch 'AA-' -and $n -notmatch 'BB-' -and $n -notmatch 'CC-'){
            Write-Host "File is not in defined format" 
        }elseif($n -match 'AA-|BB-|CC-' -or $n -match @('[,/\:*?"<>&;]') ){
            Write-Host "File has wild card operators"
        }elseif($n -match 'AA-|CC-' -or $n.length -gt 70 ){
            Write-Host "File has more than 50 characters"
        }elseif($n -match 'AA-|BB-|CC-' -or $n.length -gt 70 -or $n -match @('[,/\:*?"<>&;]')){
            Write-Host "File has wild card characters & more than 50 characters"
        }elseif($n -notmatch 'AA-' -and $n -notmatch 'BB-' -and $n -notmatch 'CC-' -and $n.length -gt 70 -and $n -notmatch @('[,/\:*?"<>&;]')){
            Write-Host 'File has Invalid format & has wild card operators'
        }
    }
    

    I can see that your logic is quite confusing.  Try to carefully test each "test pattern" against the inputs to be sure of what you want.  From your post it is not clear what you are trying to accomplish.

    I would do fewer and simpler tests. Start by writing the simplest rules as text and don't combine rules.  YOu are trying to combine apples and gof balls with no special fruit hammer and too many tees.

    Simplify! Use the KISS principal.


    \_(ツ)_/

    Friday, November 15, 2019 3:39 PM
    Moderator
  • The following line makes no sense:

    $n -notmatch 'AA-' -and $n -notmatch 'BB-' -and $n -notmatch 'CC-'

    Are you trying to detect a line that contains all of those?


    \_(ツ)_/

    Friday, November 15, 2019 4:01 PM
    Moderator
  • I'm pretty sure your example contains an error (looks like a copy/paste mistake). And, if the intention is to detect files that don't begin with "AA-", "BB-", "CC-",  or do contain a character from a specific set, should it not have been:

       $n -notmatch '^(AA-|BB-|CC-)' -or $n -match '[,/\:*?"<>&;]'

    Without the "^" anchor, the regex can still produce false positives if there's "AA-", "BB-" or "CC-" in some position other than the leading characters of the string. E.g., a file named "XX-AA-blah", which the OP's conditions (which are incompletely stated) seem to imply that the file names must begin with the AA-, BB-, or CC- strings.

    FWIW, I agree that the OP's code is confusing. I think I know what it wants to do, but all those ELSEIF defeat the purpose.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Friday, November 15, 2019 7:18 PM
  • Your testing is faulty. Your last ELSEIF condition can never be reached.

    This is essentially the same code that JRV submitted, but with the regexes that use the "^" anchor. 

    $min = "Testfile",
    "BB-Thisisfile1223 ****?????",
    "CC-ThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile",
    "CC-ThisisthelongestfileThisisthelongestf &&& *** ileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile",
    "ZZ-ThisisthelongestfileThisisthelongestf &&& *** ileThisisthelongestfileThisisthelongestfileThisisthelongestfileThisisthelongestfile"
    
    
    foreach($n in $min){
        if( $n -notmatch '^(AA-|BB-|CC-)' ){
            Write-Host "File is not in defined format" 
        } Elseif( $n -match '^(AA-|BB-|CC-)' -and $n.length -le 70 -and $n -match '[,/\:*?"<>&;]' ){
            Write-Host "File has wild card operators and is not longer then 70 characters"
        } Elseif( $n -match '^(AA-|BB-|CC-)' -and $n -notmatch '[,/\:*?"<>&;]' -and $n.length -gt 70 ){
            Write-Host "File has more than 70 characters"
        } Elseif($n -match '^(AA-|BB-|CC-)' -and ($n.length -gt 70 -and $n -match '[,/\:*?"<>&;]') ){
            Write-Host "File has wild card characters & more than 70 characters"
        } Elseif($n -notmatch '^(AA-|BB-|CC-)' -and $n.length -gt 70 -and $n -notmatch '[,/\:*?"<>&;]'){
            ########################
            # NOTE: This test will never be true. A file with an invalid format will fail the first test
            ########################
            Write-Host "File has Invalid format, has more than 70 characters & has wild card operators"
        }
    }
    
    


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Kumar GK Monday, November 18, 2019 8:03 AM
    Saturday, November 16, 2019 3:21 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, November 18, 2019 6:44 AM
    Moderator
  • I did it , thanks

    Best Regards,SQLBoy

    Monday, November 18, 2019 8:04 AM