We help IT Professionals succeed at work.

Quest Powershell logon script count command

Joseph Daly
Joseph Daly used Ask the Experts™
on
We recently aquired another company and as part of the domain migration we also migrated their user logon scripts to our netlogon folder. They operated a lot messier than I like and have loads of logon scripts. What I would like to do is figure out which logon scripts are not being used by anyone and delete them.

Using the quest cmdlets I have figured out a way to count the number of users assigned each batch file.

get-qaduser -logonscript scriptname.bat | measure-object | select-object count

This does work and returns the number of users with the login script. However as I said there are lots of scripts in the folder. I have a list of them as a CSV and I would like a way to script this to return something similar to below.

logonscript name        number of users


So Im still pretty new at powershell but what I was thinking was something along the lines of this

import-csv c:\file.csv | foreach {get-qaduser -logonscript $_.Name}

This appears to work as I can see it echoing the user names from each logonscript and then pausing as it moved to the next one. A few problems though

1. When this command is running there is no way to differentiate when one group is finished and the next started it just outputs a long list of names
2. I dont really need the names as much as the count of users assigned each logonscript. I know I can use the measure-object command to get this but just cant seem to get it right.

Ideally I would like an output file similar to below.
Logonscript              Count
file1.bat                 3
file2.bat                 29
file3.bat                 65

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hi xxdcmast,

This should do the trick:

import-csv "c:\file.csv" | Select-object Name, @{L="Count";E={(get-qaduser -logonscript $_.Name | measure-object).count}} | Sort-object Count | Format-Table -auto

Usually with this sort of job I prefer to write a short script (like the one attached) rather than a one-liner, as it's easier to see what's going on if you come back to it a few months later.  It's also easier to adapt the script to do different things, for instance if you uncomment the bottom line of the attached code, it will output to CSV as well as to the screen.

Hope this helps!

D
$scripts = import-csv "c:\file.csv" 
foreach ($script in $scripts) {
    $count = (get-qaduser -logonscript $script.Name | measure-object).count
    Add-Member -inputobject $script -membertype noteproperty -name Count -value $count
}
$scripts | sort-object Count | ft Name, Count -auto
#$scripts | select-object Name, Count | Export-CSV "c:\scriptfrequency.csv"

Open in new window

I am not at work until tomorrow so cant really test. I kind of lose what your doing here

@{L="Count";E={(get-qaduser -logonscript $_.Name | measure-object).count}} |

Can you explain what is going on here? Trying to learn powershell better as well as solving this problem.
Chris DentPowerShell Developer
Top Expert 2010
Commented:

It adds a named property with a specific value to the output object.

@{} defines a Hash, and inside that Hash you have two values. The first, L or N (l= or n=) is Label or Name, that's the name of the property you're adding. The second, E (e=) is Expression, how the value should be calculated.

In this case it adds a property called Count to the output. It finds the value by taking the name from your CSV file ($_.Name), finding all users who use that script then counting them to get a total.

HTH

Chris

Commented:
Thanks Chris, I couldn't have explained it better myself.

xxdcmast, if you want to find out more about calculated properties, I can recommend this guide:
http://www.microsoft.com/technet/scriptcenter/resources/pstips/apr08/pstip0425.mspx

Let us know if you have any more questions, and have fun learning more about Powershell.

Cheers,

D

Commented:
Thanks for the quick accept!

Cheers, D