Register
: :
Login
Home
News
Forums
Scripts
User Groups
Resources
About
Forums
Search
Members
Unanswered
Active Topics
Forums
>
Using PowerShell
>
General PowerShell
A count in a trap?
Last Post 26 Jul 2010 06:34 AM by
Joel "Jaykul" Bennett
. 3 Replies.
Sort:
Oldest First
Most Recent First
Prev
Next
You are not authorized to post a reply.
Author
Messages
jiffylueb
New Member
Posts:1
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 Howarth
Basic Member
Posts:360
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)
Veteran Member
Posts:1643
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" Bennett
Basic Member
Posts:112
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.
Using PowerShell
--General PowerShell
--Books, Tools, and Videos
--Exchange Server
--Active Directory
--System Center Family
--Non-Microsoft Products
--SharePoint
--SQL Server
--Working with .NET
--Peer Review
--Testing, Testing...
PowerShell Development
--Cmdlet Development
--PSDrive Provider Development
--Hosting the Shell
Looking Ahead
--Using PowerShell v2.0
--Developing for PowerShell v2.0
PowerShellCommunity.org
--Community Announcements and Assistance
--Completely Unrelated
--User Groups
--Community Business
----Suggestion Box
Forums
>
Using PowerShell
>
General PowerShell
Active Forums 4.3
Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2