header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Conditional menu -- Did I do this right?
Last Post 21 Sep 2010 07:23 AM by basketcase. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
basketcaseUser is Offline
New Member
New Member
Posts:15
Avatar

--
19 Sep 2010 07:29 PM
    I wrote this earlier. Essentially looking to see if a file type exists. If it does exist, prompt to delete. If it doesn't, tell me. First time doing this, it does work. Just not sure if it follows best practice. Any advice would be greatly appreciated! Thanks!

    http://pastebin.com/VVttQj1C

    Sorry for the link. Pasting in the < code > tags didn't format well.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    20 Sep 2010 01:18 PM

    I altered it slightly (left some comments in):

    $a = @('*.bat', '*.txt')
    $b = Get-ChildItem -Filter $a

    # Try naming functions in the format 'Verb-Noun' as this will keep it consistent with existing PowerShell naming conventions
    function Delete-Items
    {
        $title = "Delete Files"
        $message = "Do you want to delete the files in the folder?"

        $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Deletes all files in the folder." 
        $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Don't delete the files in the folder."
     
        $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

        $result = $Host.UI.PromptForChoice($title, $message, $options, 0)

     switch ($result)
        {
      0 { Write-Host "Hot Damn!" }
      1 { Write-Host "Darn!" }
     }
    }

    # 'if ($b)' can be misleading. I would only do this if $b were a Boolean
    # 'if ($b.Length -gt 0)' is clearer as it is a better indication of what you mean
    if ($b.Length -gt 0)
    {
        Delete-Items
    }
    else
    {
        Write-Host "Nope...Nothing there"
    }

    Other than that, seems fine.

    basketcaseUser is Offline
    New Member
    New Member
    Posts:15
    Avatar

    --
    21 Sep 2010 07:23 AM
    Much appreciated for the reply.

    This was my first time ever doing it, so it was very much a "can I do this?".

    I had read on technet or msdn someone had replied regarding if ($b) always goes to TRUE or FALSE. So to shorten it down you could do as I did, but everything else I had done, I had thrown something in there much like you did.

    When I originally started doing this script, I was having a problem where it would run the menu even if it weren't true. Only after I put the menu in a function and called the function did it work.

    Still learning so looking for did I do this right? best practices, etc. etc.

    Thanks again!
    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