header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
A count in a trap?
Last Post 26 Jul 2010 06:34 AM by Joel "Jaykul" Bennett. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
jiffyluebUser is Offline
New Member
New Member
Posts:1
Avatar

--
23 Jul 2010 02:17 PM
    Hey all, I need some help.

    I wrote a simple powershell to add/remove a user in local groups. I'm giving this to other techs in the area to use as well so I included a simple error trap to let them know if an error occurred or not. Basically what I'm doing is I define a "count" variable = 0 and if it enters the trap, count increases by 1. Only problem is it runs the other commands in the trap, but never increases count? I'm a total noob when it comes to powershell so I figured I'd ask for help. Here's my code:

    $Computers = Read-Host 'Please enter computer name'
    $domain = Read-Host 'Please enter domain name'
    $group = Read-Host 'Please enter group name (ie Administrators, Remote Desktop Users)'
    $username = Read-Host 'Please enter username.'
    $check = 0

    foreach ($computer in $Computers) { 
       $addcomp = [ADSI]("WinNT://" + $computer + ",computer") 
       $group2 = $addcomp.psbase.children.find("$group") 
       $group2.Add("WinNT://" + $domain + "/" + $username)

    trap [Exception] { 
    $check++ 
    $("ERROR on $computer " + $_.Exception.GetType().FullName) | Add-Content add-errors.txt
    $("Message: " + $_.Exception.Message) | Add-Content add-errors.txt
    continue; } }

    if ($check -eq 0 ) {
    Write-Host ("The command completed successfully $check") }
    else {
    Write-Host ("Errors were detected. See add-errors.txt") }

    Check always stays at 0 but it writes the error file successfully. Thanks in advance for any help. Probably simple.

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    26 Jul 2010 01:23 AM
    Change $check to $global:check

    Also, you don't need to specify [Exception], since that is the base type of all exceptions. You only need to do this:

    trap
    {
       # trap stuff...
    }
    Marco Shaw (MVP)User is Offline
    Veteran Member
    Veteran Member
    Posts:1643
    Avatar

    --
    26 Jul 2010 04:48 AM
    I just saw "global"... Be very careful with global because the check value will be set on a console basis, so each subsequent run of the script will continue to count up check until a new session/console is started.

    $script:check may be more appropriate/less troublesome.

    Jiffy: see:
    PS> help about_scopes
    Marco

    *Microsoft MVP - Windows PowerShell
    https://mvp.support.microsoft.com/profile/Marco.Shaw
    *Co-Author - Sams Windows PowerShell Unleashed 2nd Edition
    *Blog - http://marcoshaw.blogspot.com
    Joel "Jaykul" BennettUser is Offline
    Basic Member
    Basic Member
    Posts:112
    Avatar

    --
    26 Jul 2010 06:34 AM
    Posted By marco.shaw on 26 Jul 2010 04:48 AM

    $script:check may be more appropriate/less troublesome.

    I second that. If you must use global, make sure that at the top of your script (or in a begin { ... } block) you reset it to zero ;-)
    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