header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Reporting Account Types
Last Post 16 Nov 2008 10:59 AM by Shay. 10 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
11 Nov 2008 10:50 AM  

Hello

I have created a script and I wondered if anyone could suggest any improvements (not hard i'm sure).

Basically, all my AD accouts have either the type of accounts they are (System, Departmental etc...) or the users job title in the Title field. My script goes through AD and basicaly counts up how many there are of each account and then displays this to the screen.

The problem I am finding is that this takes up a massive amount of memory and my pc is not usable whilst PowerShell runs the script.

Any suggestion on how to improve this woud be most welcomed.

# Script to check for various types of accounts

$Departmental = get-qaduser -title Departmental -sizelimit 0
$Training = Get-QADUser -title Training -sizelimit 0
$System = Get-QADUser -title System -sizelimit 0
$Service = Get-QADUser -title Service -sizelimit 0
$Users = Get-QADUser -sizelimit 0 | where { $_.title -ne "Training" -AND "Departmental" -And "Service"}
$Total = Get-QADUser -Sizelimit 0

write-host "There are a total of" $Total.count "Accounts"
write-host "There are a total of" $Users.count "User Accounts"
write-host "There are a total of" $System.count "System Accounts"
write-host "There are a total of" $Departmental.count "Departmental Accounts"
write-host "There are a total of" $Service.count "Service Accounts"
write-host "There are a total of" $Training.count "Training Accounts"

 

Thanks

Lee 

 

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Nov 2008 10:59 AM  
Try this:

Get-QADUser -sizeLimit 0 | group title -noElement

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
11 Nov 2008 12:11 PM  
Thanks Shay. that is quicker but it doesn't perform the function that I created the script to perform, which is count up system, service, departmental and training accounts. Anything else is a user which I also need a total of.

Any other ideas?

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Nov 2008 12:48 PM  

Since I can't work on your AD ;-), here's an example using the file system:


PS > $acct = dir | group extension -NoElement
PS > $acct

Count Name
----- ----
74
138 .ps1
6 .vbs
1 .bat
3 .xml
8 .csv
2 .rar
2 .htm
1 .InstallLog
1 .JPG
1 .fdf
1 .wav
6 .txt
2 .cmd
1 .html
1 .config


Get total count:

PS > $total = $acct | measure-object -property count -sum
PS > $total.sum
248

This is the equivalent of:
# $Users = Get-QADUser -sizelimit 0 | where { $_.title -ne "Training" -AND "Departmental" -And "Service"}

#$acct | where { $_.name -ne ".txt" -and $_.name -ne ".cmd" -and $_.name -ne ".ps1" }
$acct | where { $_.name -notmatch "\.(txt|cmd|ps1)" }

Count Name
----- ----
   74
    6 .vbs
    1 .bat
    3 .xml
    8 .csv
    2 .rar
    2 .htm
    1 .InstallLog
    1 .JPG
    1 .fdf
    1 .wav
    1 .html
    1 .config

Makes sense?
 

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
11 Nov 2008 03:24 PM  
Getting there. I now have this which is working great:

$OU = "domain.co.uk/ou"
get-qaduser -searchroot $OU -sizelimit 0 | where { $_.title -match "(service|training|system|departmental|Leaver)"} | group title | select Count,Name | ft -autosize

But i still need a way to give me a count of what is left over (i.e standard users). Do you know if that is possible?
ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
11 Nov 2008 03:49 PM  

Maybe this way. I would remove the select-object, adding -noElement to group-object leaves you with just the columns you're after. Notice the first foreach-object clause (begin), it defines a variable to hold the standard users count.


get-qaduser -searchroot $OU -sizelimit 0 | foreach {$leftOver=0} {
   if( $_.title -match "(service|training|system|departmental|Leaver)") {
      $_
   } else { 
      $leftOver++
   }
} | group title -noElement | ft -autosize

$leftOver



 

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
11 Nov 2008 04:46 PM  

I'll try that.  What I came up with in the meantime which sort of works is:

 

$user = get-qaduser -sizelimit 0 | where { $_.title -notmatch "(Service|Training|System|Departmental|Leaver)"}; write-host "User Accounts:" $user.count; get-qaduser -sizelimit 0 | where { $_.title -match "(Service|Training|System|Departmental|Leaver)"} | group title | select Count,Name | ft -autosize

This works alot faster than what my origional script did but.......... if the user account has a title such as "Departmental Trainer"  it gets counted seperately.  I think its because of the -match operator but after trying the others such as -eq and -like nothing works. If i can get that ironed out then I'm basically there.

 

 

ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
13 Nov 2008 09:20 AM  
You want "Departmental Trainer" to be counted as Departmental ?
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
SynJunkieUser is Offline
New Member
New Member
Posts:97
Avatar

--
14 Nov 2008 11:04 AM  
No, because that is a Job Title rather than an account type. I'm using the Title field on Service accounts, Training accounts, Departmental Accounts (shared mailboxes) etc....

like I said, All i ideally would like to do is count how many departmental Accounts, Training accounts, System Accounts and Service Accounts I have. Anything else I class as a standard user which I also want to total up.

what is strange is that the -match operator allows me to use the | as an OR:

$OU = "doman.co.uk/ou"; get-qaduser -searchroot $OU -sizelimit 0 | where { $_.title -match "(service|training|system|departmental|Leaver)" }

but the -eq doesn't


$OU = "doman.co.uk/ou"; get-qaduser -searchroot $OU -sizelimit 0 | where { $_.title -eq "(service|training|system|departmental|Leaver)" }


if it did this would be alot easier I think.
aleksandarUser is Offline
New Member
New Member
Posts:11
Avatar

--
15 Nov 2008 08:32 PM  
> what is strange is that the -match operator allows me to use the | as an OR:

The -match operator does regular expression comparison (case insensitive). Alternation is the regular expression equivalent of "or" and a vertical bar or pipe symbol is an alternation operator.

-aleksandar
http://powershellers.blogspot.com
ShayUser is Offline
Basic Member
Basic Member
Posts:281
Avatar

--
16 Nov 2008 10:59 AM  

I would go this way:

# query all user objects ONCE only
$users = get-qaduser -searchroot $OU -sizelimit 0

# Group them by title on an *offline* object
$titles = $users | group title -noElement

Now filter the ones you want either by matching titles or whatever.

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer